class RubyXL::Cell

Constants

ERROR
RAW_STRING
SHARED_STRING

Attributes

column[RW]
datatype[RW]
formula[RW]
formula_attributes[R]
row[RW]
style_index[RW]
workbook[R]
worksheet[RW]

Public Class Methods

new(worksheet, row, column, value = nil, formula = nil, datatype = SHARED_STRING, style_index = 0, fmla_attr = {}) click to toggle source
# 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

Public Instance Methods

border_bottom() click to toggle source

returns cell’s bottom border

# File lib/rubyXL/cell.rb, line 260
def border_bottom()
  return get_border(:bottom)
end
border_diagonal() click to toggle source

returns cell’s diagonal border

# File lib/rubyXL/cell.rb, line 265
def border_diagonal()
  return get_border(:diagonal)
end
border_left() click to toggle source

returns cell’s left border

# File lib/rubyXL/cell.rb, line 250
def border_left()
  return get_border(:left)
end
border_right() click to toggle source

returns cell’s right border

# File lib/rubyXL/cell.rb, line 255
def border_right()
  return get_border(:right)
end
border_top() click to toggle source

returns cell’s top border

# File lib/rubyXL/cell.rb, line 245
def border_top()
  return get_border(:top)
end
change_border_bottom(weight='thin') click to toggle source

changes bottom border of cell

# File lib/rubyXL/cell.rb, line 151
def change_border_bottom(weight='thin')
  change_border(:bottom, weight)
end
change_border_diagonal(weight='thin') click to toggle source

changes diagonal border of cell

# File lib/rubyXL/cell.rb, line 156
def change_border_diagonal(weight='thin')
  change_border(:diagonal, weight)
end
change_border_left(weight='thin') click to toggle source

changes left border of cell

# File lib/rubyXL/cell.rb, line 141
def change_border_left(weight='thin')
  change_border(:left, weight)
end
change_border_right(weight='thin') click to toggle source

changes right border of cell

# File lib/rubyXL/cell.rb, line 146
def change_border_right(weight='thin')
  change_border(:right, weight)
end
change_border_top(weight='thin') click to toggle source

changes top border of cell

# File lib/rubyXL/cell.rb, line 136
def change_border_top(weight='thin')
  change_border(:top, weight)
end
change_contents(data, formula=nil) click to toggle source

changes contents of cell, with formula option

# 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') click to toggle source

changes fill color of cell

# 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) click to toggle source

Changes font bold settings of cell

# 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') click to toggle source

Changes font color of cell

# 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) click to toggle source

Changes font italics settings of cell

# 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') click to toggle source

Changes font name of cell

# 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) click to toggle source

Changes font size of cell

# 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) click to toggle source
# 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) click to toggle source

Changes font underline settings of cell

# 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') click to toggle source

changes horizontal alignment of cell

# 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) click to toggle source

changes wrap of cell

# 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') click to toggle source

changes vertical alignment of cell

# 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_color() click to toggle source

returns cell’s fill color

# File lib/rubyXL/cell.rb, line 215
def fill_color()
  validate_worksheet
  return @workbook.get_fill_color(get_cell_xf)
end
font_color() click to toggle source
# File lib/rubyXL/cell.rb, line 209
def font_color()
  validate_worksheet
  get_cell_font.get_rgb_color || '000000'
end
font_name() click to toggle source
# File lib/rubyXL/cell.rb, line 199
def font_name()
  validate_worksheet
  get_cell_font.get_name
end
font_size() click to toggle source
# File lib/rubyXL/cell.rb, line 204
def font_size()
  validate_worksheet
  get_cell_font.get_size
end
horizontal_alignment() click to toggle source

returns cell’s horizontal alignment

# 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
inspect() click to toggle source
# 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_bolded() click to toggle source

returns if font is bolded

# File lib/rubyXL/cell.rb, line 184
def is_bolded()
  validate_worksheet
  get_cell_font.is_bold
end
is_date?() click to toggle source
# 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_italicized() click to toggle source

returns if font is italicized

# File lib/rubyXL/cell.rb, line 178
def is_italicized()
  validate_worksheet
  get_cell_font.is_italic
end
is_struckthrough() click to toggle source
# File lib/rubyXL/cell.rb, line 194
def is_struckthrough()
  validate_worksheet
  get_cell_font.is_strikethrough
end
is_underlined() click to toggle source
# File lib/rubyXL/cell.rb, line 189
def is_underlined()
  validate_worksheet
  get_cell_font.is_underlined
end
text_wrap() click to toggle source

returns cell’s wrap

# 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) click to toggle source

Helper method to update the font array and xf array

# 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 = {}) click to toggle source
# 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_alignment() click to toggle source

returns cell’s vertical alignment

# 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

Private Instance Methods

change_border(direction, weight) click to toggle source
# File lib/rubyXL/cell.rb, line 278
def change_border(direction, weight)
  validate_worksheet
  validate_border(weight)

  border = get_cell_border.dup
  border.set_edge_style(direction, weight)

  xf = workbook.register_new_border(border, get_cell_xf)
  @style_index = workbook.register_new_xf(xf, @style_index)
end
get_border(direction) click to toggle source
# File lib/rubyXL/cell.rb, line 289
def get_border(direction)
  validate_worksheet
  get_cell_border.get_edge_style(direction)
end
get_cell_border() click to toggle source
# File lib/rubyXL/cell.rb, line 320
def get_cell_border
  @workbook.borders[get_cell_xf.border_id]
end
get_cell_font() click to toggle source
# File lib/rubyXL/cell.rb, line 316
def get_cell_font
  @workbook.fonts[@workbook.cell_xfs[@style_index].font_id]
end
get_cell_xf() click to toggle source
# File lib/rubyXL/cell.rb, line 312
def get_cell_xf
  @workbook.cell_xfs[@style_index]
end
validate_workbook() click to toggle source
# File lib/rubyXL/cell.rb, line 294
def validate_workbook()
  unless @workbook.nil? || @workbook.worksheets.nil?
    @workbook.worksheets.each do |sheet|
      unless sheet.nil? || sheet.sheet_data.nil? || sheet.sheet_data[@row].nil?
        if sheet.sheet_data[@row][@column] == self
          return
        end
      end
    end
  end
  raise "This cell #{self} is not in workbook #{@workbook}"
end
validate_worksheet() click to toggle source
# File lib/rubyXL/cell.rb, line 307
def validate_worksheet()
  return if @worksheet && @worksheet[@row][@column] == self
  raise "This cell #{self} is not in worksheet #{worksheet}"
end