class RubyXL::Cell
Constants
- ERROR
- RAW_STRING
- SHARED_STRING
Attributes
Public Class Methods
# 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
returns cell’s bottom border
# File lib/rubyXL/cell.rb, line 260 def border_bottom() return get_border(:bottom) end
returns cell’s diagonal border
# File lib/rubyXL/cell.rb, line 265 def border_diagonal() return get_border(:diagonal) end
returns cell’s left border
# File lib/rubyXL/cell.rb, line 250 def border_left() return get_border(:left) end
returns cell’s right border
# File lib/rubyXL/cell.rb, line 255 def border_right() return get_border(:right) end
returns cell’s top border
# File lib/rubyXL/cell.rb, line 245 def border_top() return get_border(:top) end
changes bottom border of cell
# File lib/rubyXL/cell.rb, line 151 def change_border_bottom(weight='thin') change_border(:bottom, weight) end
changes diagonal border of cell
# File lib/rubyXL/cell.rb, line 156 def change_border_diagonal(weight='thin') change_border(:diagonal, weight) end
changes left border of cell
# File lib/rubyXL/cell.rb, line 141 def change_border_left(weight='thin') change_border(:left, weight) end
changes right border of cell
# File lib/rubyXL/cell.rb, line 146 def change_border_right(weight='thin') change_border(:right, weight) end
changes top border of cell
# File lib/rubyXL/cell.rb, line 136 def change_border_top(weight='thin') change_border(:top, weight) end
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
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
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
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
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
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
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
# 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
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
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
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
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
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
# File lib/rubyXL/cell.rb, line 209 def font_color() validate_worksheet get_cell_font.get_rgb_color || '000000' end
# File lib/rubyXL/cell.rb, line 199 def font_name() validate_worksheet get_cell_font.get_name end
# File lib/rubyXL/cell.rb, line 204 def font_size() validate_worksheet get_cell_font.get_size end
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
# 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
returns if font is bolded
# File lib/rubyXL/cell.rb, line 184 def is_bolded() validate_worksheet get_cell_font.is_bold end
# 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
returns if font is italicized
# File lib/rubyXL/cell.rb, line 178 def is_italicized() validate_worksheet get_cell_font.is_italic end
# File lib/rubyXL/cell.rb, line 194 def is_struckthrough() validate_worksheet get_cell_font.is_strikethrough end
# File lib/rubyXL/cell.rb, line 189 def is_underlined() validate_worksheet get_cell_font.is_underlined end
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
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
# 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
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
# 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
# File lib/rubyXL/cell.rb, line 289 def get_border(direction) validate_worksheet get_cell_border.get_edge_style(direction) end
# File lib/rubyXL/cell.rb, line 320 def get_cell_border @workbook.borders[get_cell_xf.border_id] end
# File lib/rubyXL/cell.rb, line 316 def get_cell_font @workbook.fonts[@workbook.cell_xfs[@style_index].font_id] end
# File lib/rubyXL/cell.rb, line 312 def get_cell_xf @workbook.cell_xfs[@style_index] end
# 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
# 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