Class: RubyXL::Cell

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubyXL/cell.rb', line 7

def initialize(worksheet,row,column,value=nil,formula=nil,datatype='s',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.



4
5
6
# File 'lib/rubyXL/cell.rb', line 4

def column
  @column
end

#datatypeObject

Returns the value of attribute datatype.



4
5
6
# File 'lib/rubyXL/cell.rb', line 4

def datatype
  @datatype
end

#formulaObject

Returns the value of attribute formula.



4
5
6
# File 'lib/rubyXL/cell.rb', line 4

def formula
  @formula
end

#formula_attributesObject (readonly)

Returns the value of attribute formula_attributes.



5
6
7
# File 'lib/rubyXL/cell.rb', line 5

def formula_attributes
  @formula_attributes
end

#rowObject

Returns the value of attribute row.



4
5
6
# File 'lib/rubyXL/cell.rb', line 4

def row
  @row
end

#style_indexObject

Returns the value of attribute style_index.



4
5
6
# File 'lib/rubyXL/cell.rb', line 4

def style_index
  @style_index
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/rubyXL/cell.rb', line 4

def value
  @value
end

#workbookObject (readonly)

Returns the value of attribute workbook.



5
6
7
# File 'lib/rubyXL/cell.rb', line 5

def workbook
  @workbook
end

#worksheetObject

Returns the value of attribute worksheet.



4
5
6
# File 'lib/rubyXL/cell.rb', line 4

def worksheet
  @worksheet
end

Class Method Details

.convert_to_cell(row = 0, col = 0) ⇒ Object

returns Excel-style cell string from matrix indices



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rubyXL/cell.rb', line 272

def Cell.convert_to_cell(row=0,col=0)
  row_string = (row + 1).to_s #+1 for 0 indexing
  col_string = ''

  if row < 0 || col < 0
    raise 'Invalid input: cannot convert negative numbers'
  end

  unless col == 0
    col_length = 1+Integer(Math.log(col) / Math.log(26)) #opposite of 26**
  else
    col_length = 1
  end

  1.upto(col_length) do |i|

    #for the last digit, 0 should mean A. easy way to do this.
    if i == col_length
      col+=1
    end

    if col >= 26**(col_length-i)
      int_val = col / 26**(col_length-i) #+1 for 0 indexing
      int_val += 64 #converts 1 to A, etc.

      col_string += int_val.chr

      #intval multiplier decrements by placeholder, essentially
      #a B subtracts more than an A this way.
      col -= (int_val-64)*26**(col_length-i)
    end
  end
  col_string+row_string
end

Instance Method Details

#border_bottomObject

returns cell’s bottom border



262
263
264
# File 'lib/rubyXL/cell.rb', line 262

def border_bottom()
  return get_border(:bottom)
end

#border_diagonalObject

returns cell’s diagonal border



267
268
269
# File 'lib/rubyXL/cell.rb', line 267

def border_diagonal()
  return get_border(:diagonal)
end

#border_leftObject

returns cell’s left border



252
253
254
# File 'lib/rubyXL/cell.rb', line 252

def border_left()
  return get_border(:left)
end

#border_rightObject

returns cell’s right border



257
258
259
# File 'lib/rubyXL/cell.rb', line 257

def border_right()
  return get_border(:right)
end

#border_topObject

returns cell’s top border



247
248
249
# File 'lib/rubyXL/cell.rb', line 247

def border_top()
  return get_border(:top)
end

#change_border_bottom(weight = 'thin') ⇒ Object

changes bottom border of cell



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

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

#change_border_diagonal(weight = 'thin') ⇒ Object

changes diagonal border of cell



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

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

#change_border_left(weight = 'thin') ⇒ Object

changes left border of cell



125
126
127
# File 'lib/rubyXL/cell.rb', line 125

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

#change_border_right(weight = 'thin') ⇒ Object

changes right border of cell



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

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

#change_border_top(weight = 'thin') ⇒ Object

changes top border of cell



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

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

#change_contents(data, formula = nil) ⇒ Object

changes contents of cell, with formula option



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

def change_contents(data, formula=nil)
  validate_worksheet
  @datatype='str'
  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



21
22
23
24
25
# File 'lib/rubyXL/cell.rb', line 21

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 if font is bolded or not



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

def change_font_bold(bolded=false)
  validate_worksheet
  @style_index = modify_font(@workbook,@style_index)
  if bolded
    @workbook.fonts[font_id()][:font][:b] = {}
  else
    @workbook.fonts[font_id()][:font][:b] = nil
  end
end

#change_font_color(font_color = '000000') ⇒ Object

changes font color of cell



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubyXL/cell.rb', line 46

def change_font_color(font_color='000000')
  validate_worksheet
  #if arg is a color name, convert to integer
  Color.validate_color(font_color)

  @style_index = modify_font(@workbook,@style_index)
  font_id = font_id()
  if @workbook.fonts[font_id][:font][:color].nil?
    @workbook.fonts[font_id][:font][:color] = {:attributes => {:rgb => ''}}
  end
  @workbook.fonts[font_id][:font][:color][:attributes][:rgb] = font_color.to_s
end

#change_font_italics(italicized = false) ⇒ Object

changes if font is italicized or not



60
61
62
63
64
65
66
67
68
# File 'lib/rubyXL/cell.rb', line 60

def change_font_italics(italicized=false)
  validate_worksheet
  @style_index = modify_font(@workbook,@style_index)
  if italicized
    @workbook.fonts[font_id()][:font][:i] = {}
  else
    @workbook.fonts[font_id()][:font][:i] = nil
  end
end

#change_font_name(font_name = 'Verdana') ⇒ Object

changes font name of cell



28
29
30
31
32
# File 'lib/rubyXL/cell.rb', line 28

def change_font_name(font_name='Verdana')
  validate_worksheet
  @style_index = modify_font(@workbook,@style_index)
  @workbook.fonts[font_id()][:font][:name][:attributes][:val] = font_name.to_s
end

#change_font_size(font_size = 10) ⇒ Object

changes font size of cell



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

def change_font_size(font_size=10)
  validate_worksheet
  if font_size.is_a?(Integer) || font_size.is_a?(Float)
    @style_index = modify_font(@workbook, @style_index)
    @workbook.fonts[font_id()][:font][:sz][:attributes][:val] = font_size
  else
    raise 'Argument must be a number'
  end
end

#change_font_strikethrough(struckthrough = false) ⇒ Object

changes if font has a strikethrough or not



94
95
96
97
98
99
100
101
102
103
# File 'lib/rubyXL/cell.rb', line 94

def change_font_strikethrough(struckthrough=false)
  validate_worksheet
  @style_index = modify_font(@workbook,@style_index)

  if struckthrough
    @workbook.fonts[font_id()][:font][:strike] = {}
  else
    @workbook.fonts[font_id()][:font][:strike] = nil
  end
end

#change_font_underline(underlined = false) ⇒ Object

changes if font is underlined or not



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

def change_font_underline(underlined=false)
  validate_worksheet
  @style_index = modify_font(@workbook,@style_index)

  if underlined
    @workbook.fonts[font_id()][:font][:u] = {}
  else
    @workbook.fonts[font_id()][:font][:u] = nil
  end
end

#change_horizontal_alignment(alignment = 'center') ⇒ Object

changes horizontal alignment of cell



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

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

#change_vertical_alignment(alignment = 'center') ⇒ Object

changes vertical alignment of cell



113
114
115
116
117
# File 'lib/rubyXL/cell.rb', line 113

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



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

def fill_color()
  validate_worksheet
  xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
  return @workbook.get_fill_color(xf)
end

#font_colorObject

returns cell’s font color



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

def font_color()
  validate_worksheet
  if @workbook.fonts[font_id()][:font][:color].nil?
    '000000' #black
  else
    @workbook.fonts[font_id()][:font][:color][:attributes][:rgb]
  end
end

#font_nameObject

returns cell’s font name



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

def font_name()
  validate_worksheet
  @workbook.fonts[font_id()][:font][:name][:attributes][:val]
end

#font_sizeObject

returns cell’s font size



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

def font_size()
  validate_worksheet
  return @workbook.fonts[font_id()][:font][:sz][:attributes][:val]
end

#horizontal_alignmentObject

returns cell’s horizontal alignment



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

def horizontal_alignment()
  validate_worksheet
  xf_obj = @workbook.get_style(@style_index)
  if xf_obj[:alignment].nil? || xf_obj[:alignment][:attributes].nil?
    return nil
  end
  xf_obj[:alignment][:attributes][:horizontal].to_s
end

#inspectObject



307
308
309
310
311
312
# File 'lib/rubyXL/cell.rb', line 307

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



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

def is_bolded()
  validate_worksheet
  if @workbook.fonts[font_id()][:font][:b].nil?
    false
  else
    true
  end
end

#is_italicizedObject

returns if font is italicized



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

def is_italicized()
  validate_worksheet
  if @workbook.fonts[font_id()][:font][:i].nil?
    false
  else
    true
  end
end

#is_struckthroughObject

returns if font has a strike through it



187
188
189
190
191
192
193
194
195
# File 'lib/rubyXL/cell.rb', line 187

def is_struckthrough()
  validate_worksheet
  xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
  if @workbook.fonts[font_id()][:font][:strike].nil?
    false
  else
    true
  end
end

#is_underlinedObject

returns if font is underlined



176
177
178
179
180
181
182
183
184
# File 'lib/rubyXL/cell.rb', line 176

def is_underlined()
  validate_worksheet
  xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
  if @workbook.fonts[font_id()][:font][:u].nil?
    false
  else
    true
  end
end

#vertical_alignmentObject

returns cell’s vertical alignment



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

def vertical_alignment()
  validate_worksheet
  xf_obj = @workbook.get_style(@style_index)
  if xf_obj[:alignment].nil? || xf_obj[:alignment][:attributes].nil?
    return nil
  end
  xf_obj[:alignment][:attributes][:vertical].to_s
end