Module: RubyXL::CellConvenienceMethods

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

Constant Summary collapse

65530

Instance Method Summary collapse

Instance Method Details



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/rubyXL/convenience_methods/cell.rb', line 259

def add_hyperlink(url, tooltip = nil)
  if worksheet.hyperlinks then
    if worksheet.hyperlinks.size >= MAX_HYPERLINKS_PER_WORKSHEET then
      raise RuntimeError, "Maximum number of hyperlinks per worksheet (#{MAX_HYPERLINKS_PER_WORKSHEET}) reached"
    end
  else
    worksheet.hyperlinks = RubyXL::Hyperlinks.new
  end

  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 << hyperlink
end

#add_shared_string(str) ⇒ Object



279
280
281
282
283
# File 'lib/rubyXL/convenience_methods/cell.rb', line 279

def add_shared_string(str)
  self.datatype = RubyXL::DataType::SHARED_STRING
  workbook.shared_strings_container ||= RubyXL::SharedStringsTable.new
  self.raw_value = workbook.shared_strings_container.add(str)
end

#change_border(direction, weight, diagonals = nil) ⇒ Object



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

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

#change_border_color(direction, color) ⇒ Object



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

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



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

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



168
169
170
171
172
# File 'lib/rubyXL/convenience_methods/cell.rb', line 168

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



213
214
215
216
217
218
219
# File 'lib/rubyXL/convenience_methods/cell.rb', line 213

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



194
195
196
197
198
199
200
201
# File 'lib/rubyXL/convenience_methods/cell.rb', line 194

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



204
205
206
207
208
209
210
# File 'lib/rubyXL/convenience_methods/cell.rb', line 204

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



175
176
177
178
179
180
181
# File 'lib/rubyXL/convenience_methods/cell.rb', line 175

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

Raises:

  • (ArgumentError)


184
185
186
187
188
189
190
191
# File 'lib/rubyXL/convenience_methods/cell.rb', line 184

def change_font_size(font_size = 10)
  validate_worksheet
  raise(ArgumentError, '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



230
231
232
233
234
235
236
# File 'lib/rubyXL/convenience_methods/cell.rb', line 230

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



222
223
224
225
226
227
228
# File 'lib/rubyXL/convenience_methods/cell.rb', line 222

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



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

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



59
60
61
62
# File 'lib/rubyXL/convenience_methods/cell.rb', line 59

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



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

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



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

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



54
55
56
57
# File 'lib/rubyXL/convenience_methods/cell.rb', line 54

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



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

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

#fill_color ⇒ Object



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

def fill_color
  validate_worksheet
  workbook.get_fill_color(get_cell_xf)
end

#font_color ⇒ Object



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

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

#font_name ⇒ Object



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

def font_name
  validate_worksheet
  get_cell_font.get_name
end

#font_size ⇒ Object



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

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



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rubyXL/convenience_methods/cell.rb', line 246

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 ArgumentError, "Invalid change_type: #{change_type}"
  end
end

#get_border(direction) ⇒ Object



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

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

#get_border_color(direction) ⇒ Object



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

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

#horizontal_alignment ⇒ Object



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

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

#is_bolded ⇒ Object



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

def is_bolded
  validate_worksheet
  get_cell_font.is_bold
end

#is_italicized ⇒ Object



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

def is_italicized
  validate_worksheet
  get_cell_font.is_italic
end

#is_struckthrough ⇒ Object



100
101
102
103
# File 'lib/rubyXL/convenience_methods/cell.rb', line 100

def is_struckthrough
  validate_worksheet
  get_cell_font.is_strikethrough
end

#is_underlined ⇒ Object



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

def is_underlined
  validate_worksheet
  get_cell_font.is_underlined
end

#remove_formula ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rubyXL/convenience_methods/cell.rb', line 24

def remove_formula
  self.formula = nil

  calculation_chain = workbook&.calculation_chain
  calculation_cells = calculation_chain&.cells
  calculation_cells&.reject! { |c|
    (c.ref.first_col == self.column) && (c.ref.first_row == self.row)
  }
end

#set_number_format(format_code) ⇒ Object



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

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_indent ⇒ Object



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

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

#text_rotation ⇒ Object



146
147
148
149
150
151
# File 'lib/rubyXL/convenience_methods/cell.rb', line 146

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

#text_wrap ⇒ Object



139
140
141
142
143
144
# File 'lib/rubyXL/convenience_methods/cell.rb', line 139

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

#vertical_alignment ⇒ Object



132
133
134
135
136
137
# File 'lib/rubyXL/convenience_methods/cell.rb', line 132

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