Class: OoxmlParser::XLSXWorkbook

Inherits:
CommonDocumentStructure show all
Includes:
WorkbookHelpers
Defined in:
lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from CommonDocumentStructure

#file_path

Attributes inherited from OOXMLDocumentObject

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WorkbookHelpers

#with_data?

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(worksheets = []) ⇒ XLSXWorkbook

Returns a new instance of XLSXWorkbook.



17
18
19
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 17

def initialize(worksheets = [])
  @worksheets = worksheets
end

Class Attribute Details

.shared_stringsObject

Accessor for shared string. Initialization for this array



105
106
107
108
109
110
111
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 105

def shared_strings
  if @shared_strings.nil?
    shared_strings_file = "#{OOXMLDocumentObject.path_to_folder}/#{OOXMLDocumentObject.root_subfolder}/sharedStrings.xml"
    @shared_strings = Nokogiri::XML(File.open(shared_strings_file)).xpath('//xmlns:si') if File.exist?(shared_strings_file)
  end
  @shared_strings
end

.styles_nodeObject

Returns the value of attribute styles_node.



102
103
104
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 102

def styles_node
  @styles_node
end

Instance Attribute Details

#relationshipsRelationships

Returns rels of book.

Returns:



13
14
15
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 13

def relationships
  @relationships
end

#style_sheetStyleSheet

Returns styles of book.

Returns:



15
16
17
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 15

def style_sheet
  @style_sheet
end

#themePresentationTheme

Returns theme of Workbook.

Returns:



11
12
13
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 11

def theme
  @theme
end

#worksheetsObject

Returns the value of attribute worksheets.



9
10
11
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 9

def worksheets
  @worksheets
end

Class Method Details



113
114
115
116
117
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 113

def link_to_theme_xml
  file = File.open(OOXMLDocumentObject.path_to_folder + 'xl/_rels/workbook.xml.rels')
  relationships = Relationships.parse_rels(file)
  relationships.target_by_type('theme')
end

.parseObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 75

def self.parse
  workbook = XLSXWorkbook.new
  workbook.relationships = Relationships.parse_rels("#{OOXMLDocumentObject.path_to_folder}xl/_rels/workbook.xml.rels")
  OOXMLDocumentObject.xmls_stack = []
  OOXMLDocumentObject.root_subfolder = 'xl/'
  self.shared_strings = nil
  OOXMLDocumentObject.add_to_xmls_stack('xl/workbook.xml')
  doc = Nokogiri::XML.parse(File.open(OOXMLDocumentObject.current_xml))
  XLSXWorkbook.styles_node = Nokogiri::XML(File.open("#{OOXMLDocumentObject.path_to_folder}/#{OOXMLDocumentObject.root_subfolder}/styles.xml"))
  workbook.theme = PresentationTheme.parse("xl/#{link_to_theme_xml}") if link_to_theme_xml
  workbook.style_sheet = StyleSheet.new(parent: self).parse
  doc.xpath('xmlns:workbook/xmlns:sheets/xmlns:sheet').each do |sheet|
    file = workbook.relationships.target_by_id(sheet.attribute('id').value)
    if file.start_with?('worksheets')
      workbook.worksheets << Worksheet.parse(file, parent: workbook)
      workbook.worksheets.last.name = sheet.attribute('name').value
    elsif file.start_with?('chartsheets')
      workbook.worksheets << Chartsheet.new(parent: workbook).parse(file)
    end
  end
  OOXMLDocumentObject.xmls_stack.pop
  workbook
end

Instance Method Details

#all_formula_values(precision = 14) ⇒ Array, String

Get all values of formulas.

Parameters:

  • precision (Fixnum) (defaults to: 14)

    of formulas counting

Returns:

  • (Array, String)

    all formulas



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 46

def all_formula_values(precision = 14)
  formulas = []
  worksheets.each do |c_sheet|
    next unless c_sheet
    c_sheet.rows.each do |c_row|
      next unless c_row
      c_row.cells.each do |c_cell|
        next unless c_cell
        next unless c_cell.formula
        text = c_cell.raw_text
        if StringHelper.numeric?(text)
          text = text.to_f.round(10).to_s[0..precision]
        elsif StringHelper.complex?(text)
          complex_number = Complex(text.tr(',', '.'))
          real_part = complex_number.real
          real_rounded = real_part.to_f.round(10).to_s[0..precision].to_f

          imag_part = complex_number.imag
          imag_rounded = imag_part.to_f.round(10).to_s[0..precision].to_f
          complex_rounded = Complex(real_rounded, imag_rounded)
          text = complex_rounded.to_s
        end
        formulas << text
      end
    end
  end
  formulas
end

#cell(column, row, sheet = 0) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 21

def cell(column, row, sheet = 0)
  column = Coordinates.new(row, column).get_column_number unless StringHelper.numeric?(column.to_s)

  if StringHelper.numeric?(sheet.to_s)
    row = @worksheets[sheet].rows[row.to_i - 1]
    return nil if row.nil?
    return row.cells[column.to_i - 1]
  elsif sheet.is_a?(String)
    @worksheets.each do |worksheet|
      if worksheet.name == sheet
        return worksheet.rows[row.to_i - 1].cells[column.to_i - 1] unless worksheet.rows[row.to_i - 1].nil?
      end
    end
    return nil
  end
  raise "Error. Wrong sheet value: #{sheet}"
end

#difference(other) ⇒ Object



39
40
41
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook.rb', line 39

def difference(other)
  Hash.object_to_hash(self).diff(Hash.object_to_hash(other))
end