/
workbook.rb
55 lines (38 loc) · 1.47 KB
/
workbook.rb
1
2
3
4
5
6
7
8
9
10
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
module SpreadsheetX
# This class represents an XLSX Document on disk
class Workbook
attr_reader :path
attr_reader :worksheets
# return a Workbook object which relates to an existing xlsx file on disk
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 = []
REXML::Document.new(file_contents).elements.each('workbook/sheets/sheet') do |node|
sheet_id = node.attributes['sheetId'].to_i
r_id = node.attributes['r:id'].gsub('rId','').to_i
name = node.attributes['name'].to_s
@worksheets.push SpreadsheetX::Worksheet.new(archive, sheet_id, r_id, name)
end
end
end
end
# saves the binary form of the complete xlsx file to a new xlsx file
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
end
end