Class: RubyXL::Cell

Inherits:
PrivateClass show all
Defined in:
lib/rubyXL/cell.rb

Constant Summary collapse

SHARED_STRING =
's'
RAW_STRING =
'str'
ERROR =
'e'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worksheet, row, column, value = nil, formula = nil, datatype = SHARED_STRING, style_index = 0, fmla_attr = {}) ⇒ Cell

Returns a new instance of Cell.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rubyXL/cell.rb', line 10

def initialize(worksheet, row, column, value = nil, formula = nil, datatype = SHARED_STRING, style_index = 0, fmla_attr = {})
  @worksheet = worksheet

  @workbook = worksheet.workbook
  @row = row
  @column = column
  @datatype = datatype
  @value = value
  @formula=formula
  @style_index = style_index
  @formula_attributes = fmla_attr
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



7
8
9
# File 'lib/rubyXL/cell.rb', line 7

def column
  @column
end

#datatypeObject

Returns the value of attribute datatype.



7
8
9
# File 'lib/rubyXL/cell.rb', line 7

def datatype
  @datatype
end

#formulaObject

Returns the value of attribute formula.



7
8
9
# File 'lib/rubyXL/cell.rb', line 7

def formula
  @formula
end

#formula_attributesObject (readonly)

Returns the value of attribute formula_attributes.



8
9
10
# File 'lib/rubyXL/cell.rb', line 8

def formula_attributes
  @formula_attributes
end

#rowObject

Returns the value of attribute row.



7
8
9
# File 'lib/rubyXL/cell.rb', line 7

def row
  @row
end

#style_indexObject

Returns the value of attribute style_index.



7
8
9
# File 'lib/rubyXL/cell.rb', line 7

def style_index
  @style_index
end

#workbookObject (readonly)

Returns the value of attribute workbook.



8
9
10
# File 'lib/rubyXL/cell.rb', line 8

def workbook
  @workbook
end

#worksheetObject

Returns the value of attribute worksheet.



7
8
9
# File 'lib/rubyXL/cell.rb', line 7

def worksheet
  @worksheet
end

Instance Method Details

#border_bottomObject

returns cell’s bottom border



260
261
262
# File 'lib/rubyXL/cell.rb', line 260

def border_bottom()
  return get_border(:bottom)
end

#border_diagonalObject

returns cell’s diagonal border



265
266
267
# File 'lib/rubyXL/cell.rb', line 265

def border_diagonal()
  return get_border(:diagonal)
end

#border_leftObject

returns cell’s left border



250
251
252
# File 'lib/rubyXL/cell.rb', line 250

def border_left()
  return get_border(:left)
end

#border_rightObject

returns cell’s right border



255
256
257
# File 'lib/rubyXL/cell.rb', line 255

def border_right()
  return get_border(:right)
end

#border_topObject

returns cell’s top border



245
246
247
# File 'lib/rubyXL/cell.rb', line 245

def border_top()
  return get_border(:top)
end

#change_border_bottom(weight = 'thin') ⇒ Object

changes bottom border of cell



151
152
153
# File 'lib/rubyXL/cell.rb', line 151

def change_border_bottom(weight='thin')
  change_border(:bottom, weight)
end

#change_border_diagonal(weight = 'thin') ⇒ Object

changes diagonal border of cell



156
157
158
# File 'lib/rubyXL/cell.rb', line 156

def change_border_diagonal(weight='thin')
  change_border(:diagonal, weight)
end

#change_border_left(weight = 'thin') ⇒ Object

changes left border of cell



141
142
143
# File 'lib/rubyXL/cell.rb', line 141

def change_border_left(weight='thin')
  change_border(:left, weight)
end

#change_border_right(weight = 'thin') ⇒ Object

changes right border of cell



146
147
148
# File 'lib/rubyXL/cell.rb', line 146

def change_border_right(weight='thin')
  change_border(:right, weight)
end

#change_border_top(weight = 'thin') ⇒ Object

changes top border of cell



136
137
138
# File 'lib/rubyXL/cell.rb', line 136

def change_border_top(weight='thin')
  change_border(:top, weight)
end

#change_contents(data, formula = nil) ⇒ Object

changes contents of cell, with formula option



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rubyXL/cell.rb', line 161

def change_contents(data, formula=nil)
  validate_worksheet
  @datatype = RAW_STRING

  if data.is_a?(Date) || data.is_a?(DateTime)
    data = @workbook.date_to_num(data)
  end

  if (data.is_a?Integer) || (data.is_a?Float)
    @datatype = ''
  end

  @value=data
  @formula=formula
end

#change_fill(rgb = 'ffffff') ⇒ Object

changes fill color of cell



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

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

#change_font_bold(bolded = false) ⇒ Object

Changes font bold settings of cell



83
84
85
86
87
88
89
# File 'lib/rubyXL/cell.rb', line 83

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



64
65
66
67
68
69
70
71
# File 'lib/rubyXL/cell.rb', line 64

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



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

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



45
46
47
48
49
50
51
# File 'lib/rubyXL/cell.rb', line 45

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



54
55
56
57
58
59
60
61
# File 'lib/rubyXL/cell.rb', line 54

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



100
101
102
103
104
105
106
# File 'lib/rubyXL/cell.rb', line 100

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



92
93
94
95
96
97
98
# File 'lib/rubyXL/cell.rb', line 92

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

changes horizontal alignment of cell



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

def change_horizontal_alignment(alignment='center')
  validate_worksheet
  validate_horizontal_alignment(alignment)
  @style_index = modify_alignment(@workbook,@style_index,true,alignment)
end

#change_text_wrap(wrap = false) ⇒ Object

changes wrap of cell



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

def change_text_wrap(wrap=false)
  validate_worksheet
  validate_text_wrap(wrap)
  @style_index = modify_text_wrap(@workbook,@style_index,wrap)
end

#change_vertical_alignment(alignment = 'center') ⇒ Object

changes vertical alignment of cell



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

def change_vertical_alignment(alignment='center')
  validate_worksheet
  validate_vertical_alignment(alignment)
  @style_index = modify_alignment(@workbook,@style_index,false,alignment)
end

#fill_colorObject

returns cell’s fill color



215
216
217
218
# File 'lib/rubyXL/cell.rb', line 215

def fill_color()
  validate_worksheet
  return @workbook.get_fill_color(get_cell_xf)
end

#font_colorObject



209
210
211
212
# File 'lib/rubyXL/cell.rb', line 209

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

#font_nameObject



199
200
201
202
# File 'lib/rubyXL/cell.rb', line 199

def font_name()
  validate_worksheet
  get_cell_font.get_name
end

#font_sizeObject



204
205
206
207
# File 'lib/rubyXL/cell.rb', line 204

def font_size()
  validate_worksheet
  get_cell_font.get_size
end

#horizontal_alignmentObject

returns cell’s horizontal alignment



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

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

#inspectObject



269
270
271
272
273
274
# File 'lib/rubyXL/cell.rb', line 269

def inspect
  str = "(#{@row},#{@column}): #{@value}" 
  str += " =#{@formula}" if @formula
  str += ", datatype = #{@datatype}, style_index = #{@style_index}"
  return str
end

#is_boldedObject

returns if font is bolded



184
185
186
187
# File 'lib/rubyXL/cell.rb', line 184

def is_bolded()
  validate_worksheet
  get_cell_font.is_bold
end

#is_date?Boolean

Returns:

  • (Boolean)


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

def is_date?
  return false if @value.is_a?(String)
  tmp_num_fmt = @workbook.num_fmts_by_id[Integer(get_cell_xf.num_fmt_id)]
  num_fmt = tmp_num_fmt && tmp_num_fmt.format_code
  num_fmt && workbook.date_num_fmt?(num_fmt)
end

#is_italicizedObject

returns if font is italicized



178
179
180
181
# File 'lib/rubyXL/cell.rb', line 178

def is_italicized()
  validate_worksheet
  get_cell_font.is_italic
end

#is_struckthroughObject



194
195
196
197
# File 'lib/rubyXL/cell.rb', line 194

def is_struckthrough()
  validate_worksheet
  get_cell_font.is_strikethrough
end

#is_underlinedObject



189
190
191
192
# File 'lib/rubyXL/cell.rb', line 189

def is_underlined()
  validate_worksheet
  get_cell_font.is_underlined
end

#text_wrapObject

returns cell’s wrap



237
238
239
240
241
242
# File 'lib/rubyXL/cell.rb', line 237

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

#update_font_references(modified_font) ⇒ Object

Helper method to update the font array and xf array



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

def update_font_references(modified_font)
  xf = @workbook.register_new_font(modified_font, get_cell_xf)
  @style_index = workbook.register_new_xf(xf, @style_index)
end

#value(args = {}) ⇒ Object



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

def value(args = {})
  raw_values = args.delete(:raw) || false
  return @value if raw_values
  return @workbook.num_to_date(@value) if is_date?
  @value
end

#vertical_alignmentObject

returns cell’s vertical alignment



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

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