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

Overview

Class for parsing cell style

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

Instance Attribute Summary collapse

Attributes inherited from OOXMLDocumentObject

#parent

Instance Method Summary collapse

Methods inherited from OOXMLDocumentObject

#==, add_to_xmls_stack, copy_file_and_rename_to_zip, current_xml, dir, encrypted_file?, get_link_from_rels, unzip_file, #with_data?

Methods included from OoxmlDocumentObjectHelper

#to_hash

Constructor Details

#initialize(parent: nil) ⇒ CellStyle

Returns a new instance of CellStyle.



70
71
72
73
74
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 70

def initialize(parent: nil)
  @numerical_format = 'General'
  @alignment = XlsxAlignment.new
  @parent = parent
end

Instance Attribute Details

#alignmentObject

Returns the value of attribute alignment.



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

def alignment
  @alignment
end

#apply_alignmentTrue, False

Returns is alignment applied.

Returns:

  • (True, False)

    is alignment applied



60
61
62
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 60

def apply_alignment
  @apply_alignment
end

#apply_borderTrue, False

Returns is border applied.

Returns:

  • (True, False)

    is border applied



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

def apply_border
  @apply_border
end

#apply_fillTrue, False

Returns is fill applied.

Returns:

  • (True, False)

    is fill applied



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

def apply_fill
  @apply_fill
end

#apply_fontTrue, False

Returns is font applied.

Returns:

  • (True, False)

    is font applied



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

def apply_font
  @apply_font
end

#apply_number_formatTrue, False

Returns is number format applied.

Returns:

  • (True, False)

    is number format applied



58
59
60
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 58

def apply_number_format
  @apply_number_format
end

#border_idInteger

Returns id of border.

Returns:

  • (Integer)

    id of border



64
65
66
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 64

def border_id
  @border_id
end

#bordersObject

Returns the value of attribute borders.



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

def borders
  @borders
end

#fill_colorObject

Returns the value of attribute fill_color.



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

def fill_color
  @fill_color
end

#fill_idInteger

Returns id of fill.

Returns:

  • (Integer)

    id of fill



66
67
68
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 66

def fill_id
  @fill_id
end

#fontObject

Returns the value of attribute font.



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

def font
  @font
end

#font_idInteger

Returns id of font.

Returns:

  • (Integer)

    id of font



62
63
64
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 62

def font_id
  @font_id
end

#number_format_idInteger

Returns id of number format.

Returns:

  • (Integer)

    id of number format



68
69
70
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 68

def number_format_id
  @number_format_id
end

#numerical_formatObject

Returns the value of attribute numerical_format.



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

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



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

def quote_prefix
  @quote_prefix
end

Instance Method Details

#calculate_valuesObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 115

def calculate_values
  @font = root_object.style_sheet.fonts[@font_id]
  @borders = Borders.parse_from_style(@border_id) if @apply_border
  @fill_color = root_object.style_sheet.fills[@fill_id] if @apply_fill
  return unless @apply_number_format
  format = root_object.style_sheet.number_formats.format_by_id(@number_format_id)
  @numerical_format = if format
                        format.format_code
                      else
                        CellStyle::ALL_FORMAT_VALUE[@number_format_id - 1]
                      end
end

#parse(node) ⇒ CellStyle

Parse CellStyle object

Parameters:

  • node (Nokogiri::XML:Element)

    node to parse

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/xlsx_row/xlsx_cell/cell_style.rb', line 79

def parse(node)
  style_node = XLSXWorkbook.styles_node.xpath('//xmlns:cellXfs/xmlns:xf')[node.to_i]
  style_node.attributes.each do |key, value|
    case key
    when 'applyFont'
      @apply_font = attribute_enabled?(value)
    when 'applyBorder'
      @apply_border = attribute_enabled?(value)
    when 'applyFill'
      @apply_fill = attribute_enabled?(value)
    when 'applyNumberFormat'
      @apply_number_format = attribute_enabled?(value)
    when 'applyAlignment'
      @apply_alignment = attribute_enabled?(value)
    when 'fontId'
      @font_id = value.value.to_i
    when 'borderId'
      @border_id = value.value.to_i
    when 'fillId'
      @fill_id = value.value.to_i
    when 'numFmtId'
      @number_format_id = value.value.to_i
    when 'quotePrefix'
      @quote_prefix = attribute_enabled?(value)
    end
  end
  style_node.xpath('*').each do |node_child|
    case node_child.name
    when 'alignment'
      @alignment.parse(node_child) if @apply_alignment
    end
  end
  calculate_values
  self
end