backstop-simple_xlsx_writer 0.5.4

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Dee Zsombor (zsombor@primalgrasp.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,44 @@
1
+ ** Description **
2
+
3
+
4
+ This is a simple no fuss generator for OpenXML aka XLSX files. No
5
+ formatting, styles just raw content with a few basic datatypes
6
+ supported. Produced output is tested to be compatible with
7
+
8
+ - Open Office 3.2 series (Linux, Mac, Windows)
9
+ - Neo Office 3.2 series (Mac)
10
+ - Microsoft Office 2007 (Windows)
11
+ - Microsoft Office 2010 (Windows)
12
+ - Microsoft Office 2008 for Mac (versions 12.2.5 or above)
13
+ - Microsoft Excel Viewer (Windows)
14
+
15
+ Numbers of iWork '09 does not appear to support the inline string
16
+ storage model prefered by this gem. Apple may release a fix for this
17
+ eventually, I have avoided the more common shared string table
18
+ method as it cannot be implemented in linear time.
19
+
20
+
21
+ ** Sample **
22
+
23
+
24
+ serializer = SimpleXlsx::Serializer.new("test.xlsx") do |doc|
25
+ doc.add_sheet("People") do |sheet|
26
+ sheet.add_row(%w{DoB Name Occupation})
27
+ sheet.add_row([Date.parse("July 31, 1912"),
28
+ "Milton Friedman",
29
+ "Economist / Statistician"])
30
+ end
31
+ end
32
+
33
+
34
+ ** License **
35
+
36
+
37
+ See attached LICENSE for details.
38
+
39
+
40
+ ** Credits **
41
+
42
+
43
+ Written by Dee Zsombor: http://primalgrasp.com
44
+ Funded by Harvest: http://www.getharvest.com
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/gempackagetask'
5
+
6
+ task :default => [:test]
7
+
8
+ Rake::TestTask.new do |test|
9
+ test.libs << "test"
10
+ test.test_files = Dir['test/**/*_test.rb'].sort
11
+ test.verbose = true
12
+ end
13
+
14
+ desc "generate tags for emacs"
15
+ task :tags do
16
+ sh "ctags -Re lib/ "
17
+ end
18
+
19
+
20
+ spec = Gem::Specification.new do |s|
21
+ s.name = "simple_xlsx_writer"
22
+ s.version = "0.5.3.justinbeck1"
23
+ s.author = "Dee Zsombor & Justin Beck"
24
+ s.email = "justinbeck@mac.com"
25
+ s.homepage = "http://simplxlsxwriter.rubyforge.org"
26
+ s.rubyforge_project = "simple_xlsx_writer"
27
+ s.platform = Gem::Platform::RUBY
28
+ s.summary = "Just as the name says, simple writter for Office 2007+ Excel files"
29
+ s.files = [FileList["{bin,lib}/**/*"].to_a, "LICENSE", "Rakefile"].flatten
30
+ s.require_path = "lib"
31
+ s.test_files = [FileList["{test}/**/*test.rb"].to_a, "test/test_helper.rb"].flatten
32
+ s.has_rdoc = true
33
+ s.extra_rdoc_files = ["README"]
34
+ s.add_dependency("rubyzip", ">= 0.9.4")
35
+ s.add_dependency("fast_xs", ">= 0.7.3")
36
+ end
37
+
38
+ Rake::GemPackageTask.new(spec) do |pkg|
39
+ pkg.need_tar = true
40
+ end
@@ -0,0 +1,11 @@
1
+ require 'tempfile'
2
+ require 'rubygems'
3
+
4
+ $:.unshift(File.dirname(__FILE__))
5
+ require 'simple_xlsx/xml_escape'
6
+ require 'simple_xlsx/monkey_patches_for_true_zip_stream'
7
+ require 'simple_xlsx/serializer'
8
+ require 'simple_xlsx/document'
9
+ require 'simple_xlsx/sheet'
10
+
11
+
@@ -0,0 +1,23 @@
1
+ module SimpleXlsx
2
+ class Document
3
+ def initialize(io)
4
+ @sheets = []
5
+ @io = io
6
+ end
7
+
8
+ attr_reader :sheets
9
+
10
+ def add_sheet name, &block
11
+ stream = @io.open_stream_for_sheet(@sheets.size)
12
+ @sheets << Sheet.new(self, escape_for_excel(name), stream, &block)
13
+ end
14
+
15
+ def has_shared_strings?
16
+ false
17
+ end
18
+
19
+ def escape_for_excel(name)
20
+ name.gsub(/\//, "").strip # Remove forward slashes and trim white space
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,61 @@
1
+ require 'zip/zip' #dep
2
+
3
+ __END__
4
+
5
+ module Zip
6
+ class ZipOutputStream
7
+ def initialize(fileName)
8
+ super()
9
+ if fileName.is_a?(String) && !fileName.empty?
10
+ @fileName = fileName
11
+ @outputStream = File.new(@fileName, "wb")
12
+ else
13
+ @outputStream = fileName
14
+ @fileName = ''
15
+ end
16
+ @entrySet = ZipEntrySet.new
17
+ @compressor = NullCompressor.instance
18
+ @closed = false
19
+ @currentEntry = nil
20
+ @comment = nil
21
+ end
22
+ end
23
+
24
+ class ZipFile < ZipCentralDirectory
25
+ def initialize(stream, create = nil)
26
+ super()
27
+ @name = stream.is_a?(String) ? stream : ''
28
+ @comment = ""
29
+ if stream.is_a?(String) && File.exists?(stream)
30
+ File.open(name, "rb") { |f| read_from_stream(f) }
31
+ elsif (create)
32
+ @entrySet = ZipEntrySet.new
33
+ elsif !stream.is_a?(String) && !create && !stream.respond_to(:path)
34
+ # do nothing here
35
+ elsif !stream.is_a?(String) && !create
36
+ File.open(stream.path, "rb") { |f| read_from_stream(f) }
37
+ else
38
+ raise ZipError, "File #{stream} not found"
39
+ end
40
+ @create = create
41
+ @storedEntries = @entrySet.dup
42
+
43
+ @restore_ownership = false
44
+ @restore_permissions = false
45
+ @restore_times = true
46
+ end
47
+
48
+ def on_success_replace arg
49
+ if arg.is_a?(String) && !arg.empty?
50
+ tmpfile = get_tempfile
51
+ tmpFilename = tmpfile.path
52
+ tmpfile.close
53
+ if yield tmpFilename
54
+ File.rename(tmpFilename, name)
55
+ end
56
+ else
57
+ yield arg
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,193 @@
1
+ # Naview
2
+ # - Adapted to use Tempfile and ZipOutputStream rather than ZipFile
3
+ # - Replaced @zip.get_output_stream with @zip.put_next_entry
4
+ # - Commented out @zip.mkdir
5
+ #
6
+ # source: http://github.com/harvesthq/simple_xlsx_writer
7
+ # see also: http://info.michael-simons.eu/2008/01/21/using-rubyzip-to-create-zip-files-on-the-fly/
8
+
9
+ module SimpleXlsx
10
+ class Serializer
11
+ def initialize tempfile
12
+ Zip::ZipOutputStream.open(tempfile.path) do |zip|
13
+ @zip = zip
14
+ add_doc_props
15
+ add_relationship_part
16
+ add_styles
17
+ @doc = Document.new(self)
18
+ yield @doc
19
+ add_workbook_relationship_part
20
+ add_content_types
21
+ add_workbook_part
22
+ end
23
+ end
24
+
25
+ def add_workbook_part
26
+ @zip.put_next_entry("xl/workbook.xml")
27
+ f = @zip
28
+ f.puts <<-ends
29
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
30
+ <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
31
+ <workbookPr date1904="0" />
32
+ <sheets>
33
+ ends
34
+ @doc.sheets.each_with_index do |sheet, ndx|
35
+ f.puts "<sheet name=\"#{sheet.name}\" sheetId=\"#{ndx + 1}\" r:id=\"#{sheet.rid}\"/>"
36
+ end
37
+ f.puts "</sheets></workbook>"
38
+ end
39
+
40
+ def open_stream_for_sheet ndx
41
+ @zip.put_next_entry("xl/worksheets/sheet#{ndx + 1}.xml")
42
+ @zip
43
+ end
44
+
45
+ def add_content_types
46
+ @zip.put_next_entry("[Content_Types].xml")
47
+ f = @zip
48
+ f.puts '<?xml version="1.0" encoding="UTF-8"?>'
49
+ f.puts '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'
50
+ f.puts <<-ends
51
+ <Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
52
+ <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
53
+ <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
54
+ <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
55
+ <Override PartName="/xl/_rels/workbook.xml.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
56
+ ends
57
+ if @doc.has_shared_strings?
58
+ f.puts '<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>'
59
+ end
60
+ @doc.sheets.each_with_index do |sheet, ndx|
61
+ f.puts "<Override PartName=\"/xl/worksheets/sheet#{ndx+1}.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/>"
62
+ end
63
+ f.puts '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>'
64
+ f.puts "</Types>"
65
+ end
66
+
67
+ def add_workbook_relationship_part
68
+ @zip.put_next_entry("xl/_rels/workbook.xml.rels")
69
+ f = @zip
70
+ f.puts <<-ends
71
+ <?xml version="1.0" encoding="UTF-8"?>
72
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
73
+ ends
74
+ cnt = 0
75
+ f.puts "<Relationship Id=\"rId#{cnt += 1}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\"/>"
76
+ @doc.sheets.each_with_index do |sheet, ndx|
77
+ sheet.rid = "rId#{cnt += 1}"
78
+ f.puts "<Relationship Id=\"#{sheet.rid}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet\" Target=\"worksheets/sheet#{ndx + 1}.xml\"/>"
79
+ end
80
+ if @doc.has_shared_strings?
81
+ f.puts '<Relationship Id="rId#{cnt += 1}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="xl/sharedStrings.xml"/>'
82
+ end
83
+ f.puts "</Relationships>"
84
+ end
85
+
86
+ def add_relationship_part
87
+ @zip.put_next_entry("_rels/.rels")
88
+ f = @zip
89
+ f.puts <<-ends
90
+ <?xml version="1.0" encoding="UTF-8"?>
91
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
92
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
93
+ <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
94
+ <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
95
+ ends
96
+ f.puts "</Relationships>"
97
+ end
98
+
99
+ def add_doc_props
100
+ @zip.put_next_entry("docProps/core.xml")
101
+ f = @zip
102
+ f.puts <<-ends
103
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
104
+ <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
105
+ <dcterms:created xsi:type="dcterms:W3CDTF">#{Time.now.utc.xmlschema}</dcterms:created>
106
+ <cp:revision>0</cp:revision>
107
+ </cp:coreProperties>
108
+ ends
109
+ @zip.put_next_entry("docProps/app.xml")
110
+ f = @zip
111
+ f.puts <<-ends
112
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
113
+ <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
114
+ <TotalTime>0</TotalTime>
115
+ </Properties>
116
+ ends
117
+ end
118
+
119
+ def add_styles
120
+ @zip.put_next_entry("xl/styles.xml")
121
+ f = @zip
122
+ f.puts <<-ends
123
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
124
+ <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
125
+ <numFmts count="4">
126
+ <numFmt formatCode="GENERAL" numFmtId="164"/>
127
+ <numFmt formatCode="&quot;TRUE&quot;;&quot;TRUE&quot;;&quot;FALSE&quot;" numFmtId="170"/>
128
+ <numFmt formatCode="mmmm\ yyyy" numFmtId="165"/>
129
+ <numFmt formatCode="0.00%" numFmtId="166"/>
130
+ </numFmts>
131
+ <fonts count="5">
132
+ <font><name val="Mangal"/><family val="2"/><sz val="10"/></font>
133
+ <font><name val="Arial"/><family val="0"/><sz val="10"/></font>
134
+ <font><name val="Arial"/><family val="0"/><sz val="10"/></font>
135
+ <font><name val="Arial"/><family val="0"/><sz val="10"/></font>
136
+ <font><name val="Arial"/><family val="2"/><sz val="10"/></font>
137
+ </fonts>
138
+ <fills count="2">
139
+ <fill><patternFill patternType="none"/></fill>
140
+ <fill><patternFill patternType="gray125"/></fill>
141
+ </fills>
142
+ <borders count="1">
143
+ <border diagonalDown="false" diagonalUp="false"><left/><right/><top/><bottom/><diagonal/></border>
144
+ </borders>
145
+ <cellStyleXfs count="20">
146
+ <xf applyAlignment="true" applyBorder="true" applyFont="true" applyProtection="true" borderId="0" fillId="0" fontId="0" numFmtId="164">
147
+ <alignment horizontal="general" indent="0" shrinkToFit="false" textRotation="0" vertical="bottom" wrapText="false"/>
148
+ <protection hidden="false" locked="true"/>
149
+ </xf>
150
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"></xf>
151
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"></xf>
152
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="2" numFmtId="0"></xf>
153
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="2" numFmtId="0"></xf>
154
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
155
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
156
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
157
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
158
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
159
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
160
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
161
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
162
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
163
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
164
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="43"></xf>
165
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="41"></xf>
166
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="44"></xf>
167
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="42"></xf>
168
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="9"></xf>
169
+ </cellStyleXfs>
170
+ <cellXfs count="9">
171
+ <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="164" xfId="0"></xf>
172
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="22" xfId="0"></xf>
173
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="15" xfId="0"></xf>
174
+ <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="1" xfId="0"></xf>
175
+ <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="2" xfId="0"></xf>
176
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="49" xfId="0"></xf>
177
+ <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="170" xfId="0"></xf>
178
+ <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="165" xfId="0"></xf>
179
+ <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="166" xfId="0"></xf>
180
+ </cellXfs>
181
+ <cellStyles count="6"><cellStyle builtinId="0" customBuiltin="false" name="Normal" xfId="0"/>
182
+ <cellStyle builtinId="3" customBuiltin="false" name="Comma" xfId="15"/>
183
+ <cellStyle builtinId="6" customBuiltin="false" name="Comma [0]" xfId="16"/>
184
+ <cellStyle builtinId="4" customBuiltin="false" name="Currency" xfId="17"/>
185
+ <cellStyle builtinId="7" customBuiltin="false" name="Currency [0]" xfId="18"/>
186
+ <cellStyle builtinId="5" customBuiltin="false" name="Percent" xfId="19"/>
187
+ </cellStyles>
188
+ </styleSheet>
189
+ ends
190
+ end
191
+ end
192
+ end
193
+
@@ -0,0 +1,91 @@
1
+ require 'bigdecimal'
2
+ require 'time'
3
+
4
+ module SimpleXlsx
5
+ class Sheet
6
+ attr_reader :name
7
+ attr_accessor :rid
8
+
9
+ def initialize document, name, stream, &block
10
+ @document = document
11
+ @stream = stream
12
+ @name = name.to_xs
13
+ @row_ndx = 1
14
+ @stream.write <<-ends
15
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
16
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
17
+ <sheetData>
18
+ ends
19
+ if block_given?
20
+ yield self
21
+ end
22
+ @stream.write "</sheetData></worksheet>"
23
+ end
24
+
25
+ def add_row arry
26
+ row = ["<row r=\"#{@row_ndx}\">"]
27
+ arry.each_with_index do |val_and_type, col_ndx|
28
+ kind, ccontent, cstyle = Sheet.format_field_and_type_and_style val_and_type
29
+ row << "<c r=\"#{Sheet.column_index(col_ndx)}#{@row_ndx}\" t=\"#{kind.to_s}\" s=\"#{cstyle}\">#{ccontent}</c>"
30
+ end
31
+ row << "</row>"
32
+ @row_ndx += 1
33
+ @stream.write(row.join())
34
+ end
35
+
36
+ def self.format_field_and_type_and_style val_and_type
37
+ value, type = val_and_type
38
+ if value.is_a?(String)
39
+ [:inlineStr, "<is><t>#{value.to_xs}</t></is>", 5]
40
+ elsif type == :period
41
+ [:n, "<v>#{days_since_jan_1_1900(value.to_date)}</v>", 7]
42
+ elsif value.is_a?(Date)
43
+ [:n, "<v>#{days_since_jan_1_1900(value)}</v>", 2]
44
+ elsif value.is_a?(Time)
45
+ [:n, "<v>#{fractional_days_since_jan_1_1900(value)}</v>", 1]
46
+ elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
47
+ [:b, "<v>#{value ? '1' : '0'}</v>", 6]
48
+ elsif type == :percent
49
+ if value.is_a?(BigDecimal)
50
+ [:n, "<v>#{value.to_s('f')}</v>", 8]
51
+ else
52
+ [:n, "<v>#{value.to_s}</v>", 8]
53
+ end
54
+ elsif value.is_a?(BigDecimal)
55
+ [:n, "<v>#{value.to_s('f')}</v>", 4]
56
+ elsif value.is_a?(Float)
57
+ [:n, "<v>#{value.to_s}</v>", 4]
58
+ elsif value.is_a?(Numeric)
59
+ [:n, "<v>#{value.to_s}</v>", 3]
60
+ else
61
+ [:inlineStr, "<is><t>#{value.to_s.to_xs}</t></is>", 5]
62
+ end
63
+ end
64
+
65
+ def self.days_since_jan_1_1900 date
66
+ @@jan_1_1904 ||= Date.parse("1904 Jan 1")
67
+ (date - @@jan_1_1904).to_i + 1462 # http://support.microsoft.com/kb/180162
68
+ end
69
+
70
+ def self.fractional_days_since_jan_1_1900 value
71
+ @@jan_1_1904_midnight ||= ::Time.utc(1904, 1, 1)
72
+ ((value - @@jan_1_1904_midnight) / 86400.0) + #24*60*60
73
+ 1462 # http://support.microsoft.com/kb/180162
74
+ end
75
+
76
+ def self.abc
77
+ @@abc ||= ('A'..'Z').to_a
78
+ end
79
+
80
+ def self.column_index n
81
+ result = []
82
+ while n >= 26 do
83
+ result << abc[n % 26]
84
+ n /= 26
85
+ end
86
+ result << abc[result.empty? ? n : n - 1]
87
+ result.reverse.join
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,6 @@
1
+ unless String.method_defined? :to_xs
2
+ require 'fast_xs' #dep
3
+ class String
4
+ alias_method :to_xs, :fast_xs
5
+ end
6
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper.rb'
2
+
3
+ module SimpleXlsx
4
+ class DocumentTest < Test::Unit::TestCase
5
+ def open_stream_for_sheet sheets_size
6
+ self
7
+ end
8
+
9
+ def write arg
10
+ # This space intentionally left blank
11
+ end
12
+
13
+ def test_add_sheet
14
+ @doc = Document.new self
15
+ assert_equal [], @doc.sheets
16
+ @doc.add_sheet "new sheet"
17
+ assert_equal 1, @doc.sheets.size
18
+ assert_equal 'new sheet', @doc.sheets.first.name
19
+ end
20
+
21
+ def test_add_sheet_with_forward_slash_in_name
22
+ @doc = Document.new self
23
+ assert_equal [], @doc.sheets
24
+ @doc.add_sheet "new sheet with /"
25
+ assert_equal 1, @doc.sheets.size
26
+ assert_equal 'new sheet with', @doc.sheets.first.name
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,81 @@
1
+ require 'test_helper'
2
+ require "rexml/document"
3
+ require 'time'
4
+
5
+ module SimpleXlsx
6
+
7
+ class SheetTest < Test::Unit::TestCase
8
+
9
+ def test_column_index
10
+ assert_equal 'A', Sheet.column_index(0)
11
+ assert_equal 'B', Sheet.column_index(1)
12
+ assert_equal 'C', Sheet.column_index(2)
13
+ assert_equal 'D', Sheet.column_index(3)
14
+ assert_equal 'Y', Sheet.column_index(24)
15
+ assert_equal 'Z', Sheet.column_index(25)
16
+ end
17
+
18
+ def test_column_index_two_digits
19
+ assert_equal 'AA', Sheet.column_index(0+26)
20
+ assert_equal 'AB', Sheet.column_index(1+26)
21
+ assert_equal 'AC', Sheet.column_index(2+26)
22
+ assert_equal 'AD', Sheet.column_index(3+26)
23
+ assert_equal 'AZ', Sheet.column_index(25+26)
24
+ assert_equal 'BA', Sheet.column_index(25+26+1)
25
+ assert_equal 'BB', Sheet.column_index(25+26+2)
26
+ assert_equal 'BC', Sheet.column_index(25+26+3)
27
+ end
28
+
29
+ def test_format_field_for_strings
30
+ v = Sheet.format_field_and_type_and_style "<escape this>"
31
+ assert_equal [:inlineStr, "<is><t>&lt;escape this&gt;</t></is>", 5], v
32
+ end
33
+
34
+ def test_format_field_for_numbers
35
+ v = Sheet.format_field_and_type_and_style 3
36
+ assert_equal [:n, "<v>3</v>", 3], v
37
+ v = Sheet.format_field_and_type_and_style(BigDecimal.new("45"))
38
+ assert_equal [:n, "<v>45.0</v>", 4], v
39
+ v = Sheet.format_field_and_type_and_style(9.32)
40
+ assert_equal [:n, "<v>9.32</v>", 4], v
41
+ end
42
+
43
+ def test_format_field_for_date
44
+ v = Sheet.format_field_and_type_and_style(Date.parse('2010-Jul-24'))
45
+ assert_equal [:n, "<v>#{38921+1462}</v>", 2], v
46
+ end
47
+
48
+ def test_format_field_for_datetime
49
+ v = Sheet.format_field_and_type_and_style(Time.parse('2010-Jul-24 12:00 UTC'))
50
+ assert_equal [:n, "<v>#{38921.5+1462}</v>", 1], v
51
+ end
52
+
53
+
54
+ def test_format_field_for_boolean
55
+ v = Sheet.format_field_and_type_and_style(false)
56
+ assert_equal [:b, "<v>0</v>", 6], v
57
+ v = Sheet.format_field_and_type_and_style(true)
58
+ assert_equal [:b, "<v>1</v>", 6], v
59
+ end
60
+
61
+ def test_add_row
62
+ str = ""
63
+ io = StringIO.new(str)
64
+ Sheet.new(nil, 'name', io) do |sheet|
65
+ sheet.add_row ['this is ', 'a new row']
66
+ end
67
+ doc = REXML::Document.new str
68
+ assert_equal 'worksheet', doc.root.name
69
+ sheetdata = doc.root.elements['sheetData']
70
+ assert sheetdata
71
+ row = sheetdata.elements['row']
72
+ assert row
73
+ assert_equal '1', row.attributes['r']
74
+ assert_equal 2, row.elements.to_a.size
75
+ assert_equal ["r", "t", "s"], row.elements.to_a[0].attributes.keys
76
+ end
77
+
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+ require 'fileutils'
3
+ require 'date'
4
+
5
+ class SimpleXlsxTest < Test::Unit::TestCase
6
+
7
+ def test_top_level
8
+ FileUtils.rm_f "test.xlsx"
9
+ SimpleXlsx::Serializer.new(Tempfile.new("test.xlsx")) do |doc|
10
+ doc.add_sheet("First") do |sheet|
11
+ sheet.add_row ["Hello", "World", 3.14, 7]
12
+ sheet.add_row ["Another", "Row", Date.today, Time.parse('2010-Jul-24 12:00 UTC')]
13
+ end
14
+ end
15
+ end
16
+
17
+ if false
18
+ def test_top_level_stream
19
+ File.open "test_stream.xlsx", "wb" do |stream|
20
+ o = SimpleXlsx::Serializer.new(stream) do |doc|
21
+ doc.add_sheet "First" do |sheet|
22
+ sheet.add_row ["Hello", "World", 3.14]
23
+ sheet.add_row ["Another", "Row", Date.today]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,8 @@
1
+ require "test/unit"
2
+ require "rubygems"
3
+ require File.dirname(__FILE__) + '/../lib/simple_xlsx' unless defined?(SimpleXlsx)
4
+
5
+ # require 'ruby-debug'
6
+ # Debugger.settings[:autoeval] = true
7
+ # Debugger.settings[:autolist] = 1
8
+ # Debugger.start
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backstop-simple_xlsx_writer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 3
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 4
10
+ version: 0.5.4
11
+ platform: ruby
12
+ authors:
13
+ - Dee Zsombor
14
+ - Justin Beck
15
+ - Backstop Solutions Group
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-04-14 00:00:00 -05:00
21
+ default_executable:
22
+ dependencies: []
23
+
24
+ description: Writes XLSX files
25
+ email:
26
+ - dbortz@backstopsolutions.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/simple_xlsx/document.rb
35
+ - lib/simple_xlsx/monkey_patches_for_true_zip_stream.rb
36
+ - lib/simple_xlsx/serializer.rb
37
+ - lib/simple_xlsx/sheet.rb
38
+ - lib/simple_xlsx/xml_escape.rb
39
+ - lib/simple_xlsx.rb
40
+ - test/simple_xlsx/document_test.rb
41
+ - test/simple_xlsx/sheet_test.rb
42
+ - test/simple_xlsx_test.rb
43
+ - test/test_helper.rb
44
+ - LICENSE
45
+ - README
46
+ - Rakefile
47
+ has_rdoc: true
48
+ homepage: http://github.com/backstop/simple_xlsx_writer
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Gem version of Justin Beck's modifications to Dee Zsombor's Simple XLSX writer to use Tempfile
81
+ test_files: []
82
+