Module: RubyXL::WorkbookConvenienceMethods

Included in:
Workbook
Defined in:
lib/rubyXL/convenience_methods.rb

Constant Summary collapse

SHEET_NAME_TEMPLATE =
'Sheet%d'

Instance Method Summary collapse

Instance Method Details

#[](ind) ⇒ Object

Finds worksheet by its name or numerical index



7
8
9
10
11
12
# File 'lib/rubyXL/convenience_methods.rb', line 7

def [](ind)
  case ind
  when Integer then worksheets[ind]
  when String  then worksheets.find { |ws| ws.sheet_name == ind }
  end
end

#add_worksheet(name = nil) ⇒ Object

Create new simple worksheet and add it to the workbook worksheets

Parameters:

  • The (String)

    name for the new worksheet



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubyXL/convenience_methods.rb', line 17

def add_worksheet(name = nil)
  if name.nil? then
    n = 0

    begin
      name = SHEET_NAME_TEMPLATE % (n += 1)
    end until self[name].nil?
  end

  new_worksheet = Worksheet.new(:workbook => self, :sheet_name => name)
  worksheets << new_worksheet
  new_worksheet
end

#applicationObject



53
54
55
# File 'lib/rubyXL/convenience_methods.rb', line 53

def application
  root.document_properties.application && root.document_properties.application.value
end

#application=(v) ⇒ Object



57
58
59
60
# File 'lib/rubyXL/convenience_methods.rb', line 57

def application=(v)
  root.document_properties.application ||= StringNode.new
  root.document_properties.application.value = v
end

#appversionObject



62
63
64
# File 'lib/rubyXL/convenience_methods.rb', line 62

def appversion
  root.document_properties.app_version && root.document_properties.app_version.value
end

#appversion=(v) ⇒ Object



66
67
68
69
# File 'lib/rubyXL/convenience_methods.rb', line 66

def appversion=(v)
  root.document_properties.app_version ||= StringNode.new
  root.document_properties.app_version.value = v
end

#bordersObject

Stylesheet should be pre-filled with defaults on initialize()



115
116
117
# File 'lib/rubyXL/convenience_methods.rb', line 115

def borders # Stylesheet should be pre-filled with defaults on initialize()
  stylesheet.borders
end

#cell_xfsObject

Stylesheet should be pre-filled with defaults on initialize()



103
104
105
# File 'lib/rubyXL/convenience_methods.rb', line 103

def cell_xfs # Stylesheet should be pre-filled with defaults on initialize()
  stylesheet.cell_xfs
end

#companyObject



44
45
46
# File 'lib/rubyXL/convenience_methods.rb', line 44

def company
  root.document_properties.company && root.document_properties.company.value
end

#company=(v) ⇒ Object



48
49
50
51
# File 'lib/rubyXL/convenience_methods.rb', line 48

def company=(v)
  root.document_properties.company ||= StringNode.new
  root.document_properties.company.value = v
end

#created_atObject



87
88
89
# File 'lib/rubyXL/convenience_methods.rb', line 87

def created_at
  root.core_properties.created_at
end

#created_at=(v) ⇒ Object



91
92
93
# File 'lib/rubyXL/convenience_methods.rb', line 91

def created_at=(v)
  root.core_properties.created_at = v
end

#creatorObject



71
72
73
# File 'lib/rubyXL/convenience_methods.rb', line 71

def creator
  root.core_properties.creator
end

#creator=(v) ⇒ Object



75
76
77
# File 'lib/rubyXL/convenience_methods.rb', line 75

def creator=(v)
  root.core_properties.creator = v
end

#date1904Object



35
36
37
# File 'lib/rubyXL/convenience_methods.rb', line 35

def date1904
  workbook_properties && workbook_properties.date1904
end

#date1904=(v) ⇒ Object



39
40
41
42
# File 'lib/rubyXL/convenience_methods.rb', line 39

def date1904=(v)
  self.workbook_properties ||= RubyXL::WorkbookProperties.new
  workbook_properties.date1904 = v
end

#eachObject



31
32
33
# File 'lib/rubyXL/convenience_methods.rb', line 31

def each
  worksheets.each{ |i| yield i }
end

#fillsObject

Stylesheet should be pre-filled with defaults on initialize()



111
112
113
# File 'lib/rubyXL/convenience_methods.rb', line 111

def fills # Stylesheet should be pre-filled with defaults on initialize()
  stylesheet.fills
end

#fontsObject

Stylesheet should be pre-filled with defaults on initialize()



107
108
109
# File 'lib/rubyXL/convenience_methods.rb', line 107

def fonts # Stylesheet should be pre-filled with defaults on initialize()
  stylesheet.fonts
end

#get_fill_color(xf) ⇒ Object



119
120
121
122
123
124
# File 'lib/rubyXL/convenience_methods.rb', line 119

def get_fill_color(xf)
  fill = fills[xf.fill_id]
  pattern = fill && fill.pattern_fill
  color = pattern && pattern.fg_color
  color && color.rgb || 'ffffff'
end

#modified_atObject



95
96
97
# File 'lib/rubyXL/convenience_methods.rb', line 95

def modified_at
  root.core_properties.modified_at
end

#modified_at=(v) ⇒ Object



99
100
101
# File 'lib/rubyXL/convenience_methods.rb', line 99

def modified_at=(v)
  root.core_properties.modified_at = v
end

#modifierObject



79
80
81
# File 'lib/rubyXL/convenience_methods.rb', line 79

def modifier
  root.core_properties.modifier
end

#modifier=(v) ⇒ Object



83
84
85
# File 'lib/rubyXL/convenience_methods.rb', line 83

def modifier=(v)
  root.core_properties.modifier = v
end

#modify_alignment(style_index) {|xf.alignment| ... } ⇒ Object

Yields:

  • (xf.alignment)


151
152
153
154
155
156
157
158
# File 'lib/rubyXL/convenience_methods.rb', line 151

def modify_alignment(style_index, &block)
  xf = cell_xfs[style_index || 0].dup
  xf.alignment ||= RubyXL::Alignment.new
  yield(xf.alignment)
  xf.apply_alignment = true

  register_new_xf(xf)
end

#modify_border(style_index, direction, weight) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rubyXL/convenience_methods.rb', line 168

def modify_border(style_index, direction, weight)
  xf = cell_xfs[style_index || 0].dup
  new_border = borders[xf.border_id || 0].dup

  edge = new_border.send(direction)
  new_border.send("#{direction}=", edge.dup) if edge

  new_border.set_edge_style(direction, weight)

  xf.border_id = borders.find_index { |x| x == new_border } # Reuse existing border, if it exists
  xf.border_id ||= borders.size # If this border has never existed before, add it to collection.
  borders[xf.border_id] = new_border
  xf.apply_border = true

  register_new_xf(xf)
end

#modify_border_color(style_index, direction, color) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rubyXL/convenience_methods.rb', line 185

def modify_border_color(style_index, direction, color)
  xf = cell_xfs[style_index || 0].dup
  new_border = borders[xf.border_id || 0].dup
  new_border.set_edge_color(direction, color)

  xf.border_id = borders.find_index { |x| x == new_border } # Reuse existing border, if it exists
  xf.border_id ||= borders.size # If this border has never existed before, add it to collection.
  borders[xf.border_id] = new_border
  xf.apply_border = true

  register_new_xf(xf)
end

#modify_fill(style_index, rgb) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/rubyXL/convenience_methods.rb', line 160

def modify_fill(style_index, rgb)
  xf = cell_xfs[style_index || 0].dup
  new_fill = RubyXL::Fill.new(:pattern_fill =>
               RubyXL::PatternFill.new(:pattern_type => 'solid',
                                       :fg_color => RubyXL::Color.new(:rgb => rgb)))
  register_new_xf(register_new_fill(new_fill, xf))
end

#password_hash(pwd) ⇒ Object

Calculate password hash from string for use in ‘password’ fields. www.openoffice.org/sc/excelfileformat.pdf



200
201
202
203
204
205
206
207
208
209
# File 'lib/rubyXL/convenience_methods.rb', line 200

def password_hash(pwd)
  hsh = 0
  pwd.reverse.each_char { |c|
    hsh = hsh ^ c.ord
    hsh = hsh << 1
    hsh -= 0x7fff if hsh > 0x7fff
  }

  (hsh ^ pwd.length ^ 0xCE4B).to_s(16)
end

#register_new_fill(new_fill, old_xf) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/rubyXL/convenience_methods.rb', line 126

def register_new_fill(new_fill, old_xf)
  new_xf = old_xf.dup
  new_xf.apply_fill = true
  new_xf.fill_id = fills.find_index { |x| x == new_fill } # Reuse existing fill, if it exists
  new_xf.fill_id ||= fills.size # If this fill has never existed before, add it to collection.
  fills[new_xf.fill_id] = new_fill
  new_xf
end

#register_new_font(new_font, old_xf) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/rubyXL/convenience_methods.rb', line 135

def register_new_font(new_font, old_xf)
  new_xf = old_xf.dup
  new_xf.font_id = fonts.find_index { |x| x == new_font } # Reuse existing font, if it exists
  new_xf.font_id ||= fonts.size # If this font has never existed before, add it to collection.
  fonts[new_xf.font_id] = new_font
  new_xf.apply_font = true
  new_xf
end

#register_new_xf(new_xf) ⇒ Object



144
145
146
147
148
149
# File 'lib/rubyXL/convenience_methods.rb', line 144

def register_new_xf(new_xf)
  new_xf_id = cell_xfs.find_index { |xf| xf == new_xf } # Reuse existing XF, if it exists
  new_xf_id ||= cell_xfs.size # If this XF has never existed before, add it to collection.
  cell_xfs[new_xf_id] = new_xf
  new_xf_id
end