Class: OoxmlParser::CellStyle

Inherits:
OOXMLDocumentObject show all
Defined in:
lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb

Constant Summary collapse

ALL_FORMAT_VALUE =
%w|0 0.00 #,##0 #,##0.00 $#,##0_);($#,##0) $#,##0_);[Red]($#,##0) $#,##0.00_);($#,##0.00)
$#,##0.00_);[Red]($#,##0.00)
0.00% 0.00%
0.00E+00
#\ ?/?
#\ ??/??
m/d/yyyy
d-mmm-yy
d-mmm
mmm-yy
h:mm AM/PM
h:mm:ss AM/PM
h:mm
h:mm:ss
m/d/yyyy h:mm
0
0
0
0
0
0
0
0
0
0
0
0
0
0
#,##0_);(#,##0)
#,##0_);[Red](#,##0)
#,##0.00_);(#,##0.00)
#,##0.00_);[Red](#,##0.00)
0
0
0
0
mm:ss
General
mm:ss.0
##0.0E+0
@|.freeze

Constants inherited from OOXMLDocumentObject

OOXMLDocumentObject::DEFAULT_DIRECTORY_FOR_MEDIA

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OOXMLDocumentObject

#==, add_to_xmls_stack, copy_file_and_rename_to_zip, copy_media_file, current_xml, dir, encrypted_file?, get_link_from_rels, media_folder, option_enabled?, unzip_file

Constructor Details

#initialize(font = nil, borders = nil, fill_color = ForegroundColor.new, numerical_format = 'General', alignment = nil) ⇒ CellStyle

Returns a new instance of CellStyle.



53
54
55
56
57
58
59
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 53

def initialize(font = nil, borders = nil, fill_color = ForegroundColor.new, numerical_format = 'General', alignment = nil)
  @font = font
  @borders = borders
  @fill_color = fill_color
  @numerical_format = numerical_format
  @alignment = alignment
end

Instance Attribute Details

#alignmentObject

Returns the value of attribute alignment.



49
50
51
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 49

def alignment
  @alignment
end

#bordersObject

Returns the value of attribute borders.



49
50
51
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 49

def borders
  @borders
end

#fill_colorObject

Returns the value of attribute fill_color.



49
50
51
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 49

def fill_color
  @fill_color
end

#fontObject

Returns the value of attribute font.



49
50
51
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 49

def font
  @font
end

#numerical_formatObject

Returns the value of attribute numerical_format.



49
50
51
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 49

def numerical_format
  @numerical_format
end

#quote_prefixTrue, False

Returns check if style should add QuotePrefix (‘ symbol) to start of the string.

Returns:

  • (True, False)

    check if style should add QuotePrefix (‘ symbol) to start of the string



51
52
53
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 51

def quote_prefix
  @quote_prefix
end

Class Method Details

.parse(style_number) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 61

def self.parse(style_number)
  current_cell_style = XLSXWorkbook.styles_node.xpath('//xmlns:cellXfs/xmlns:xf')[style_number.to_i]
  cell_style = CellStyle.new
  cell_style.alignment = XlsxAlignment.new
  cell_style.font = if current_cell_style.attribute('applyFont').nil? || current_cell_style.attribute('applyFont').value == '0'
                      OOXMLFont.parse(0)
                    else
                      OOXMLFont.parse(current_cell_style.attribute('fontId').value.to_i)
                    end
  unless current_cell_style.attribute('applyBorder').nil? || current_cell_style.attribute('applyBorder').value == '0'
    cell_style.borders = Borders.parse_from_style(current_cell_style.attribute('borderId').value.to_i)
  end
  unless current_cell_style.attribute('applyFill').nil? || current_cell_style.attribute('applyFill').value == '0'
    cell_style.fill_color = ForegroundColor.parse(current_cell_style.attribute('fillId').value.to_i)
  end
  unless current_cell_style.attribute('applyNumberFormat').nil? || current_cell_style.attribute('applyNumberFormat').value == '0'
    format_id = current_cell_style.attribute('numFmtId').value.to_i
    XLSXWorkbook.styles_node.xpath('//xmlns:numFmt').each do |numeric_format|
      if format_id == numeric_format.attribute('numFmtId').value.to_i
        cell_style.numerical_format = numeric_format.attribute('formatCode').value
      elsif CellStyle::ALL_FORMAT_VALUE[format_id - 1]
        cell_style.numerical_format = CellStyle::ALL_FORMAT_VALUE[format_id - 1]
      end
    end
    cell_style.numerical_format = CellStyle::ALL_FORMAT_VALUE[format_id - 1] if CellStyle::ALL_FORMAT_VALUE[format_id - 1]
  end
  unless current_cell_style.attribute('applyAlignment').nil? || current_cell_style.attribute('applyAlignment').value == '0'
    alignment_node = current_cell_style.xpath('xmlns:alignment').first
    cell_style.alignment = XlsxAlignment.parse(alignment_node) unless alignment_node.nil?
  end
  cell_style.quote_prefix = option_enabled?(current_cell_style, 'quotePrefix')
  cell_style
end