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



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

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



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

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



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

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

#application=(v) ⇒ Object



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

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

#appversionObject



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

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

#appversion=(v) ⇒ Object



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

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()



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

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()



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

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

#companyObject



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

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

#company=(v) ⇒ Object



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

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

#created_atObject



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

def created_at
  root.core_properties.created_at
end

#created_at=(v) ⇒ Object



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

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

#creatorObject



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

def creator
  root.core_properties.creator
end

#creator=(v) ⇒ Object



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

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

#date1904Object



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

def date1904
  workbook_properties && workbook_properties.date1904
end

#date1904=(v) ⇒ Object



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

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

#eachObject



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

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

#fillsObject

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



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

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

#fontsObject

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



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

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

#get_fill_color(xf) ⇒ Object



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

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



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

def modified_at
  root.core_properties.modified_at
end

#modified_at=(v) ⇒ Object



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

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

#modifierObject



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

def modifier
  root.core_properties.modifier
end

#modifier=(v) ⇒ Object



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

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

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

Yields:

  • (xf.alignment)


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

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

#modify_border(style_index, direction, weight) ⇒ Object



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

def modify_border(style_index, direction, weight)
  old_xf = cell_xfs[style_index].dup
  new_border = borders[old_xf.border_id].dup
  new_border.set_edge_style(direction, weight)

  new_xf = old_xf.dup
  new_xf.apply_border = true

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

  register_new_xf(new_xf)
end

#modify_fill(style_index, rgb) ⇒ Object



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

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

#register_new_fill(new_fill, old_xf) ⇒ Object



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

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 } # Use 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



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

def register_new_font(new_font, old_xf)
  new_xf = old_xf.dup
  new_xf.apply_font = true
  new_xf.font_id = fonts.find_index { |x| x == new_font } # Use 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
end

#register_new_xf(new_xf) ⇒ Object



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

def register_new_xf(new_xf)
  new_xf_id = cell_xfs.find_index { |xf| xf == new_xf } # Use 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