Module: RubyXL::CellConvenienceMethods

Defined in:
lib/rubyXL/convenience_methods/cell.rb

Instance Method Summary collapse

Instance Method Details


256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rubyXL/convenience_methods/cell.rb', line 256

def add_hyperlink(url, tooltip = nil)
  worksheet.relationship_container ||= RubyXL::OOXMLRelationshipsFile.new
  relationships = worksheet.relationship_container.relationships
  r_id = "rId#{relationships.size + 1}"
  relationships << RubyXL::Relationship.new(:id => r_id, :target => url, :target_mode => 'External',
                                            :type => RubyXL::HyperlinkRelFile::REL_TYPE)

  hyperlink = RubyXL::Hyperlink.new(:ref => self.r, :r_id => r_id)
  hyperlink.tooltip = tooltip if tooltip
  worksheet.hyperlinks ||= RubyXL::Hyperlinks.new
  worksheet.hyperlinks << hyperlink
end

#change_border(direction, weight) ⇒ Object


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

def change_border(direction, weight)
  validate_worksheet
  self.style_index = workbook.modify_border(self.style_index, direction, weight)
end

#change_border_color(direction, color) ⇒ Object


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

def change_border_color(direction, color)
  validate_worksheet
  Color.validate_color(color)
  self.style_index = workbook.modify_border_color(self.style_index, direction, color)
end

#change_contents(data, formula_expression = nil) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rubyXL/convenience_methods/cell.rb', line 3

def change_contents(data, formula_expression = nil)
  validate_worksheet

  if formula_expression then
    self.datatype = nil
    self.formula = RubyXL::Formula.new(:expression => formula_expression)
  else
    self.datatype = case data
                    when Date, Time, Numeric then nil
                    else RubyXL::DataType::RAW_STRING
                    end
  end

  data = workbook.date_to_num(data) if data.is_a?(Date) || data.is_a?(Time)

  self.raw_value = data
end

#change_fill(rgb = 'ffffff') ⇒ Object

Changes fill color of cell


165
166
167
168
169
# File 'lib/rubyXL/convenience_methods/cell.rb', line 165

def change_fill(rgb = 'ffffff')
  validate_worksheet
  Color.validate_color(rgb)
  self.style_index = workbook.modify_fill(self.style_index, rgb)
end

#change_font_bold(bolded = false) ⇒ Object

Changes font bold settings of cell


210
211
212
213
214
215
216
# File 'lib/rubyXL/convenience_methods/cell.rb', line 210

def change_font_bold(bolded = false)
  validate_worksheet

  font = get_cell_font.dup
  font.set_bold(bolded)
  update_font_references(font)
end

#change_font_color(font_color = '000000') ⇒ Object

Changes font color of cell


191
192
193
194
195
196
197
198
# File 'lib/rubyXL/convenience_methods/cell.rb', line 191

def change_font_color(font_color = '000000')
  validate_worksheet
  Color.validate_color(font_color)

  font = get_cell_font.dup
  font.set_rgb_color(font_color)
  update_font_references(font)
end

#change_font_italics(italicized = false) ⇒ Object

Changes font italics settings of cell


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

def change_font_italics(italicized = false)
  validate_worksheet

  font = get_cell_font.dup
  font.set_italic(italicized)
  update_font_references(font)
end

#change_font_name(new_font_name = 'Verdana') ⇒ Object

Changes font name of cell


172
173
174
175
176
177
178
# File 'lib/rubyXL/convenience_methods/cell.rb', line 172

def change_font_name(new_font_name = 'Verdana')
  validate_worksheet

  font = get_cell_font.dup
  font.set_name(new_font_name)
  update_font_references(font)
end

#change_font_size(font_size = 10) ⇒ Object

Changes font size of cell


181
182
183
184
185
186
187
188
# File 'lib/rubyXL/convenience_methods/cell.rb', line 181

def change_font_size(font_size = 10)
  validate_worksheet
  raise 'Argument must be a number' unless font_size.is_a?(Integer) || font_size.is_a?(Float)

  font = get_cell_font.dup
  font.set_size(font_size)
  update_font_references(font)
end

#change_font_strikethrough(struckthrough = false) ⇒ Object


227
228
229
230
231
232
233
# File 'lib/rubyXL/convenience_methods/cell.rb', line 227

def change_font_strikethrough(struckthrough = false)
  validate_worksheet

  font = get_cell_font.dup
  font.set_strikethrough(struckthrough)
  update_font_references(font)
end

#change_font_underline(underlined = false) ⇒ Object

Changes font underline settings of cell


219
220
221
222
223
224
225
# File 'lib/rubyXL/convenience_methods/cell.rb', line 219

def change_font_underline(underlined = false)
  validate_worksheet

  font = get_cell_font.dup
  font.set_underline(underlined)
  update_font_references(font)
end

#change_horizontal_alignment(alignment = 'center') ⇒ Object


41
42
43
44
# File 'lib/rubyXL/convenience_methods/cell.rb', line 41

def change_horizontal_alignment(alignment = 'center')
  validate_worksheet
  self.style_index = workbook.modify_alignment(self.style_index) { |a| a.horizontal = alignment }
end

#change_shrink_to_fit(shrink_to_fit = false) ⇒ Object


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

def change_shrink_to_fit(shrink_to_fit = false)
  validate_worksheet
  self.style_index = workbook.modify_alignment(self.style_index) { |a| a.shrink_to_fit = shrink_to_fit }
end

#change_text_indent(indent) ⇒ Object


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

def change_text_indent(indent)
  validate_worksheet
  self.style_index = workbook.modify_alignment(self.style_index) { |a| a.indent = indent }
end

#change_text_rotation(rot) ⇒ Object


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

def change_text_rotation(rot)
  validate_worksheet
  self.style_index = workbook.modify_alignment(self.style_index) { |a| a.text_rotation = rot }
end

#change_text_wrap(wrap = false) ⇒ Object


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

def change_text_wrap(wrap = false)
  validate_worksheet
  self.style_index = workbook.modify_alignment(self.style_index) { |a| a.wrap_text = wrap }
end

#change_vertical_alignment(alignment = 'center') ⇒ Object


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

def change_vertical_alignment(alignment = 'center')
  validate_worksheet
  self.style_index = workbook.modify_alignment(self.style_index) { |a| a.vertical = alignment }
end

#fill_colorObject


117
118
119
120
# File 'lib/rubyXL/convenience_methods/cell.rb', line 117

def fill_color
  validate_worksheet
  return workbook.get_fill_color(get_cell_xf)
end

#font_colorObject


112
113
114
115
# File 'lib/rubyXL/convenience_methods/cell.rb', line 112

def font_color
  validate_worksheet
  get_cell_font.get_rgb_color || '000000'
end

#font_nameObject


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

def font_name
  validate_worksheet
  get_cell_font.get_name
end

#font_sizeObject


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

def font_size
  validate_worksheet
  get_cell_font.get_size
end

#font_switch(change_type, arg) ⇒ Object

Performs correct modification based on what type of change_type is specified


243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rubyXL/convenience_methods/cell.rb', line 243

def font_switch(change_type, arg)
  case change_type
  when Worksheet::NAME          then change_font_name(arg)
  when Worksheet::SIZE          then change_font_size(arg)
  when Worksheet::COLOR         then change_font_color(arg)
  when Worksheet::ITALICS       then change_font_italics(arg)
  when Worksheet::BOLD          then change_font_bold(arg)
  when Worksheet::UNDERLINE     then change_font_underline(arg)
  when Worksheet::STRIKETHROUGH then change_font_strikethrough(arg)
  else raise 'Invalid change_type'
  end
end

#get_border(direction) ⇒ Object


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

def get_border(direction)
  validate_worksheet
  get_cell_border.get_edge_style(direction)
end

#get_border_color(direction) ⇒ Object


36
37
38
39
# File 'lib/rubyXL/convenience_methods/cell.rb', line 36

def get_border_color(direction)
  validate_worksheet
  get_cell_border.get_edge_color(direction)
end

#horizontal_alignmentObject


122
123
124
125
126
127
# File 'lib/rubyXL/convenience_methods/cell.rb', line 122

def horizontal_alignment
  validate_worksheet
  xf_obj = get_cell_xf
  return nil if xf_obj.alignment.nil?
  xf_obj.alignment.horizontal
end

#is_boldedObject


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

def is_bolded
  validate_worksheet
  get_cell_font.is_bold
end

#is_italicizedObject


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

def is_italicized
  validate_worksheet
  get_cell_font.is_italic
end

#is_struckthroughObject


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

def is_struckthrough
  validate_worksheet
  get_cell_font.is_strikethrough
end

#is_underlinedObject


92
93
94
95
# File 'lib/rubyXL/convenience_methods/cell.rb', line 92

def is_underlined
  validate_worksheet
  get_cell_font.is_underlined
end

#remove_formulaObject


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

def remove_formula
  self.formula = nil

  calculation_chain = workbook && workbook.calculation_chain
  calculation_cells = calculation_chain && calculation_chain.cells
  calculation_cells && calculation_cells.reject! { |c|
    c.ref.col_range.c == self.column && c.ref.row_range.begin == self.row
  }
end

#set_number_format(format_code) ⇒ Object


157
158
159
160
161
162
# File 'lib/rubyXL/convenience_methods/cell.rb', line 157

def set_number_format(format_code)
  new_xf = get_cell_xf.dup
  new_xf.num_fmt_id = workbook.stylesheet.register_number_format(format_code)
  new_xf.apply_number_format = true
  self.style_index = workbook.register_new_xf(new_xf)
end

#text_indentObject


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

def text_indent
  validate_worksheet
  xf_obj = get_cell_xf
  return nil if xf_obj.alignment.nil?
  xf_obj.alignment.indent
end

#text_rotationObject


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

def text_rotation
  validate_worksheet
  xf_obj = get_cell_xf
  return nil if xf_obj.alignment.nil?
  xf_obj.alignment.text_rotation
end

#text_wrapObject


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

def text_wrap
  validate_worksheet
  xf_obj = get_cell_xf
  return nil if xf_obj.alignment.nil?
  xf_obj.alignment.wrap_text
end

#vertical_alignmentObject


129
130
131
132
133
134
# File 'lib/rubyXL/convenience_methods/cell.rb', line 129

def vertical_alignment
  validate_worksheet
  xf_obj = get_cell_xf
  return nil if xf_obj.alignment.nil?
  xf_obj.alignment.vertical
end