thunderboltlabs-rubyXL 1.2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,230 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+
4
+ module RubyXL
5
+ module Writer
6
+ class WorksheetWriter
7
+ attr_accessor :dirpath, :filepath, :sheet_num, :workbook, :worksheet
8
+
9
+ def initialize(dirpath, wb, sheet_num=1)
10
+ @dirpath = dirpath
11
+ @workbook = wb
12
+ @sheet_num = sheet_num
13
+ @worksheet = @workbook.worksheets[@sheet_num]
14
+ @filepath = dirpath + '/xl/worksheets/sheet'+(sheet_num+1).to_s()+'.xml'
15
+ end
16
+
17
+ def write()
18
+ builder = Nokogiri::XML::Builder.new do |xml|
19
+ xml.worksheet('xmlns'=>"http://schemas.openxmlformats.org/spreadsheetml/2006/main",
20
+ 'xmlns:r'=>"http://schemas.openxmlformats.org/officeDocument/2006/relationships",
21
+ 'xmlns:mc'=>"http://schemas.openxmlformats.org/markup-compatibility/2006",
22
+ 'xmlns:mv'=>"urn:schemas-microsoft-com:mac:vml",
23
+ 'mc:Ignorable'=>'mv',
24
+ 'mc:PreserveAttributes'=>'mv:*') {
25
+ col = 0
26
+ @worksheet.sheet_data.each do |row|
27
+ if row.size > col
28
+ col = row.size
29
+ end
30
+ end
31
+ row = Integer(@worksheet.sheet_data.size)
32
+ dimension = 'A1:'
33
+ dimension += Cell.convert_to_cell(row-1,col-1)
34
+ xml.dimension('ref'=>dimension)
35
+ xml.sheetViews {
36
+ view = @worksheet.sheet_view
37
+ if view.nil? || view[:attributes].nil?
38
+ xml.sheetView('tabSelected'=>1,'view'=>'normalLayout','workbookViewId'=>0)
39
+ raise 'end test' #TODO remove
40
+ else
41
+ view = view[:attributes]
42
+ if view[:view].nil?
43
+ view[:view] = 'normalLayout'
44
+ end
45
+
46
+ if view[:workbookViewId].nil?
47
+ view[:workbookViewId] = 0
48
+ end
49
+
50
+ if view[:zoomScale].nil? || view[:zoomScaleNormal].nil?
51
+ view[:zoomScale] = 100
52
+ view[:zoomScaleNormal] = 100
53
+ end
54
+
55
+ if view[:tabSelected].nil?
56
+ view[:tabSelected] = 1
57
+ end
58
+
59
+ xml.sheetView('tabSelected'=>view[:tabSelected],
60
+ 'view'=>view[:view],
61
+ 'workbookViewId'=>view[:workbookViewId],
62
+ 'zoomScale'=>view[:zoomScale],
63
+ 'zoomScaleNormal'=>view[:zoomScaleNormal]) {
64
+ #TODO
65
+ #can't be done unless I figure out a way to programmatically add attributes.
66
+ #(can't put xSplit with an invalid value)
67
+ # unless @worksheet.pane.nil?
68
+ # xml.pane('state'=>@worksheet.pane[:state])
69
+ # end
70
+ # unless view[:selection].nil?
71
+ # xml.
72
+ # end
73
+ }
74
+ end
75
+ }
76
+ xml.sheetFormatPr('baseColWidth'=>'10','defaultRowHeight'=>'13')
77
+
78
+ unless @worksheet.cols.nil? || @worksheet.cols.size==0
79
+ xml.cols {
80
+ if !@worksheet.cols.is_a?(Array)
81
+ @worksheet.cols = [@worksheet.cols]
82
+ end
83
+ @worksheet.cols.each do |col|
84
+ if col[:attributes][:customWidth].nil?
85
+ col[:attributes][:customWidth] = '0'
86
+ end
87
+ if col[:attributes][:width].nil?
88
+ col[:attributes][:width] = '10'
89
+ col[:attributes][:customWidth] = '0'
90
+ end
91
+ # unless col[:attributes] == {}
92
+ xml.col('style'=>@workbook.style_corrector[col[:attributes][:style].to_s].to_s,
93
+ 'min'=>col[:attributes][:min].to_s,
94
+ 'max'=>col[:attributes][:max].to_s,
95
+ 'width'=>col[:attributes][:width].to_s,
96
+ 'customWidth'=>col[:attributes][:customWidth].to_s)
97
+ # end
98
+ end
99
+ }
100
+ end
101
+
102
+ xml.sheetData {
103
+ i=0
104
+ @worksheet.sheet_data.each_with_index do |row,i|
105
+ #TODO fix this spans thing. could be 2:3 (not necessary)
106
+ if @worksheet.row_styles[(i+1).to_s].nil?
107
+ @worksheet.row_styles[(i+1).to_s] = {}
108
+ @worksheet.row_styles[(i+1).to_s][:style] = '0'
109
+ end
110
+ custom_format = '1'
111
+ if @worksheet.row_styles[(i+1).to_s][:style] == '0'
112
+ custom_format = '0'
113
+ end
114
+
115
+ @worksheet.row_styles[(i+1).to_s][:style] = @workbook.style_corrector[@worksheet.row_styles[(i+1).to_s][:style].to_s]
116
+ row_opts = {
117
+ 'r'=>(i+1).to_s,
118
+ 'spans'=>'1:'+row.size.to_s,
119
+ 'customFormat'=>custom_format
120
+ }
121
+ unless @worksheet.row_styles[(i+1).to_s][:style].to_s == ''
122
+ row_opts['s'] = @worksheet.row_styles[(i+1).to_s][:style].to_s
123
+ end
124
+ unless @worksheet.row_styles[(i+1).to_s][:height].to_s == ''
125
+ row_opts['ht'] = @worksheet.row_styles[(i+1).to_s][:height].to_s
126
+ end
127
+ unless @worksheet.row_styles[(i+1).to_s][:customheight].to_s == ''
128
+ row_opts['customHeight'] = @worksheet.row_styles[(i+1).to_s][:customHeight].to_s
129
+ end
130
+ xml.row(row_opts) {
131
+ row.each_with_index do |dat, j|
132
+ unless dat.nil?
133
+ #TODO do xml.c for all cases, inside specific.
134
+ # if dat.formula.nil?
135
+ dat.style_index = @workbook.style_corrector[dat.style_index.to_s]
136
+ c_opts = {'r'=>Cell.convert_to_cell(i,j), 's'=>dat.style_index.to_s}
137
+ unless dat.datatype.nil? || dat.datatype == ''
138
+ c_opts['t'] = dat.datatype
139
+ end
140
+ xml.c(c_opts) {
141
+ unless dat.formula.nil?
142
+ if dat.formula_attributes.nil? || dat.formula_attributes.empty?
143
+ xml.f dat.formula.to_s
144
+ else
145
+ xml.f('t'=>dat.formula_attributes['t'].to_s, 'ref'=>dat.formula_attributes['ref'], 'si'=>dat.formula_attributes['si']).nokogiri dat.formula
146
+ end
147
+ end
148
+ if(dat.datatype == 's')
149
+ unless dat.value.nil? #empty cell, but has a style
150
+ xml.v @workbook.shared_strings[dat.value].to_s
151
+ end
152
+ elsif(dat.datatype == 'str')
153
+ xml.v dat.value.to_s
154
+ elsif(dat.datatype == '') #number
155
+ xml.v dat.value.to_s
156
+ end
157
+ }
158
+ #
159
+ # else
160
+ # xml.c('r'=>Cell.convert_to_cell(i,j)) {
161
+ # xml.v dat.value.to_s
162
+ # }
163
+ # end #data.formula.nil?
164
+ end #unless dat.nil?
165
+ end #row.each_with_index
166
+ }
167
+ end
168
+ }
169
+
170
+
171
+ xml.sheetCalcPr('fullCalcOnLoad'=>'1')
172
+
173
+ unless @worksheet.merged_cells.nil? || @worksheet.merged_cells.size==0
174
+ #There is some kind of bug here that when merged_cells is sometimes a hash and not an array
175
+ #initial attempt at a fix fails in corrupted excel documents leaving as is for now.
176
+ xml.mergeCells {
177
+ @worksheet.merged_cells.each do |merged_cell|
178
+ xml.mergeCell('ref'=>merged_cell[:attributes][:ref].to_s)
179
+ end
180
+ }
181
+ end
182
+
183
+ xml.phoneticPr('fontId'=>'1','type'=>'noConversion')
184
+
185
+ unless @worksheet.validations.nil?
186
+ xml.dataValidations('count'=>@worksheet.validations.size.to_s) {
187
+ @worksheet.validations.each do |validation|
188
+ xml.dataValidation('type'=>validation[:attributes][:type],
189
+ 'sqref'=>validation[:attributes][:sqref],
190
+ 'allowBlank'=>Integer(validation[:attributes][:allowBlank]).to_s,
191
+ 'showInputMessage'=>Integer(validation[:attributes][:showInputMessage]).to_s,
192
+ 'showErrorMessage'=>Integer(validation[:attributes][:showErrorMessage]).to_s) {
193
+ unless validation[:formula1].nil?
194
+ xml.formula1 validation[:formula1]
195
+ end
196
+ }
197
+ end
198
+ }
199
+ end
200
+
201
+ xml.pageMargins('left'=>'0.75','right'=>'0.75','top'=>'1',
202
+ 'bottom'=>'1','header'=>'0.5','footer'=>'0.5')
203
+
204
+ xml.pageSetup('orientation'=>'portrait',
205
+ 'horizontalDpi'=>'4294967292', 'verticalDpi'=>'4294967292')
206
+
207
+ unless @worksheet.legacy_drawing.nil?
208
+ xml.legacyDrawing('r:id'=>@worksheet.legacy_drawing[:attributes][:id])
209
+ end
210
+
211
+ unless @worksheet.extLst.nil?
212
+ xml.extLst {
213
+ xml.ext('xmlns:mx'=>"http://schemas.microsoft.com/office/mac/excel/2008/main",
214
+ 'uri'=>"http://schemas.microsoft.com/office/mac/excel/2008/main") {
215
+ xml['mx'].PLV('Mode'=>'1', 'OnePage'=>'0','WScale'=>'0')
216
+ }
217
+ }
218
+ end
219
+ }
220
+ end
221
+ contents = builder.to_xml
222
+ contents = contents.gsub(/\n/,'')
223
+ contents = contents.gsub(/>(\s)+</,'><')
224
+ contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")
225
+ contents
226
+ end
227
+
228
+ end
229
+ end
230
+ end
data/lib/rubyXL/zip.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'zip/zip'
3
+ require 'zip/zipfilesystem'
4
+
5
+ module RubyXL
6
+ class MyZip
7
+
8
+ # Unzips .zip file at zipPath to zipDirPath
9
+ def unzip(zipPath,zipDirPath)
10
+ Zip::ZipFile.open(zipPath) { |zip_file|
11
+ zip_file.each { |f|
12
+ fpath = File.join(zipDirPath, f.name)
13
+ FileUtils.mkdir_p(File.dirname(fpath))
14
+ zip_file.extract(f, fpath) unless File.exist?(fpath)
15
+ }
16
+ }
17
+ end
18
+
19
+ end
20
+ end
data/lib/rubyXL.rb ADDED
@@ -0,0 +1,10 @@
1
+ $: << File.expand_path(File.dirname(__FILE__))
2
+ require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','workbook'))
3
+ require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','private_class'))
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','worksheet'))
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','cell'))
6
+ require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','parser'))
7
+ require File.expand_path(File.join(File.dirname(__FILE__),'rubyXL','color'))
8
+
9
+ module RubyXL
10
+ end
data/rubyXL.gemspec ADDED
@@ -0,0 +1,92 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{thunderboltlabs-rubyXL}
8
+ s.version = "1.2.10.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Vivek Bhagwat"]
12
+ s.date = %q{2012-08-07}
13
+ s.description = %q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents}
14
+ s.email = %q{bhagwat.vivek@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/.DS_Store",
27
+ "lib/rubyXL.rb",
28
+ "lib/rubyXL/Hash.rb",
29
+ "lib/rubyXL/cell.rb",
30
+ "lib/rubyXL/color.rb",
31
+ "lib/rubyXL/parser.rb",
32
+ "lib/rubyXL/private_class.rb",
33
+ "lib/rubyXL/workbook.rb",
34
+ "lib/rubyXL/worksheet.rb",
35
+ "lib/rubyXL/writer/app_writer.rb",
36
+ "lib/rubyXL/writer/calc_chain_writer.rb",
37
+ "lib/rubyXL/writer/content_types_writer.rb",
38
+ "lib/rubyXL/writer/core_writer.rb",
39
+ "lib/rubyXL/writer/root_rels_writer.rb",
40
+ "lib/rubyXL/writer/shared_strings_writer.rb",
41
+ "lib/rubyXL/writer/styles_writer.rb",
42
+ "lib/rubyXL/writer/theme_writer.rb",
43
+ "lib/rubyXL/writer/workbook_rels_writer.rb",
44
+ "lib/rubyXL/writer/workbook_writer.rb",
45
+ "lib/rubyXL/writer/worksheet_writer.rb",
46
+ "lib/rubyXL/zip.rb",
47
+ "rubyXL.gemspec",
48
+ "spec/lib/cell_spec.rb",
49
+ "spec/lib/color_spec.rb",
50
+ "spec/lib/hash_spec.rb",
51
+ "spec/lib/parser_spec.rb",
52
+ "spec/lib/workbook_spec.rb",
53
+ "spec/lib/worksheet_spec.rb"
54
+ ]
55
+ s.homepage = %q{http://github.com/gilt/rubyXL}
56
+ s.licenses = ["MIT"]
57
+ s.require_paths = ["lib"]
58
+ s.rubygems_version = %q{1.3.7}
59
+ s.summary = %q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents}
60
+
61
+ if s.respond_to? :specification_version then
62
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
63
+ s.specification_version = 3
64
+
65
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
66
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
67
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
68
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
69
+ s.add_development_dependency(%q<rcov>, [">= 0"])
70
+ s.add_development_dependency(%q<nokogiri>, [">= 1.4.4"])
71
+ s.add_development_dependency(%q<rubyzip>, [">= 0.9.4"])
72
+ s.add_development_dependency(%q<rspec>, [">= 1.3.4"])
73
+ else
74
+ s.add_dependency(%q<shoulda>, [">= 0"])
75
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
76
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
77
+ s.add_dependency(%q<rcov>, [">= 0"])
78
+ s.add_dependency(%q<nokogiri>, [">= 1.4.4"])
79
+ s.add_dependency(%q<rubyzip>, [">= 0.9.4"])
80
+ s.add_dependency(%q<rspec>, [">= 1.3.4"])
81
+ end
82
+ else
83
+ s.add_dependency(%q<shoulda>, [">= 0"])
84
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
85
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
86
+ s.add_dependency(%q<rcov>, [">= 0"])
87
+ s.add_dependency(%q<nokogiri>, [">= 1.4.4"])
88
+ s.add_dependency(%q<rubyzip>, [">= 0.9.4"])
89
+ s.add_dependency(%q<rspec>, [">= 1.3.4"])
90
+ end
91
+ end
92
+