Class: SpreadsheetX::Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/spreadsheetx/workbook.rb

Overview

This class represents an XLSX Document on disk

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Workbook

return a Workbook object which relates to an existing xlsx file on disk



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/spreadsheetx/workbook.rb', line 11

def initialize(path)
  @path = path
  Zip::Archive.open(path) do |archive|
    
    # open the workbook
    archive.fopen('xl/workbook.xml') do |f| 

      # read contents of this file
      file_contents = f.read 

      #parse the XML and build the worksheets
      @worksheets = []
      # parse the XML and hold the doc
      xml_doc = XML::Document.string(file_contents)
      # set the default namespace
      xml_doc.root.namespaces.default_prefix = 'spreadsheetml'
      
      xml_doc.find('spreadsheetml:sheets/spreadsheetml:sheet').each do |node|
        sheet_id = node['sheetId'].to_i
        r_id = node['id'].gsub('rId','').to_i
        name = node['name'].to_s
        @worksheets.push SpreadsheetX::Worksheet.new(archive, sheet_id, r_id, name)
      end
      
    end

    # open the styles, to get the cell formats
    archive.fopen('xl/styles.xml') do |f| 

      # read contents of this file
      file_contents = f.read 

      #parse the XML and build the worksheets
      @formats = []
      # parse the XML and hold the doc
      xml_doc = XML::Document.string(file_contents)
      # set the default namespace
      xml_doc.root.namespaces.default_prefix = 'spreadsheetml'
      
      format_id = 0
      xml_doc.find('spreadsheetml:numFmts/spreadsheetml:numFmt').each do |node|
        @formats.push SpreadsheetX::CellFormat.new((format_id+=1), node['formatCode'])
      end
      
    end
    
  end
end

Instance Attribute Details

#formatsObject (readonly)

Returns the value of attribute formats.



8
9
10
# File 'lib/spreadsheetx/workbook.rb', line 8

def formats
  @formats
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/spreadsheetx/workbook.rb', line 6

def path
  @path
end

#worksheetsObject (readonly)

Returns the value of attribute worksheets.



7
8
9
# File 'lib/spreadsheetx/workbook.rb', line 7

def worksheets
  @worksheets
end

Instance Method Details

#save(destination_path) ⇒ Object

saves the binary form of the complete xlsx file to a new xlsx file



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/spreadsheetx/workbook.rb', line 61

def save(destination_path)

  # copy the xlsx file to the destination
  FileUtils.cp(@path, destination_path)
  
  # replace the xlsx files with the new workbooks
  Zip::Archive.open(destination_path) do |ar|
    
    # replace with the new worksheets
    @worksheets.each do |worksheet|
      ar.replace_buffer("xl/worksheets/sheet#{worksheet.r_id}.xml", worksheet.to_s)
    end
            
  end

end