Skip to content
This repository was archived by the owner on Jun 11, 2019. It is now read-only.

Commit c9c344c

Browse files
committed
Drafting.
1 parent ada281a commit c9c344c

File tree

16 files changed

+325
-1
lines changed

16 files changed

+325
-1
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*~
2+
*.swp
3+
\#*#
4+
TAGS
5+
tmtags
6+
tmp
7+
*.patch
8+
*.diff
9+
*.xmlx
10+
notes.txt
11+
*.\#*
12+
*.rbc

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
o = SimpleXlsx::Serializer.new("filename") do |doc|
1+
o = SimpleXlsx::Serializer.new do |doc|
22
doc.add_sheet("Sheet1") do |sheet|
33
sheet.add_header(%w{Col1 Col2 Col2})
44
sheet.add_row(%w{Val1 Val2 Val3})

lib/simple_xlsx.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'rubygems'
2+
require 'zip/zip'
3+
4+
unless String.method_defined? :to_xs
5+
require 'fast_xs'
6+
class String
7+
alias_method :to_xs, :fast_xs
8+
end
9+
end
10+
11+
$:.unshift(File.dirname(__FILE__))
12+
require 'simple_xlsx/serializer'
13+
require 'simple_xlsx/document'
14+
require 'simple_xlsx/sheet'
15+
16+

lib/simple_xlsx.rb~

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$:.unshift(File.dirname(__FILE__))
2+
3+
module SimpleXlsx
4+
5+
end

lib/simple_xlsx/document.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
@io.open_stream_for_sheet(name) do |stream|
12+
@sheets << Sheet.new(self, name, stream, &block)
13+
end
14+
end
15+
16+
def has_shared_strings?
17+
false
18+
end
19+
20+
def has_styles?
21+
false
22+
end
23+
24+
end
25+
end

lib/simple_xlsx/document.rb~

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module SimpleXlsx
2+
class Document
3+
end
4+
end

lib/simple_xlsx/serializer.rb

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
module SimpleXlsx
2+
3+
class Serializer
4+
5+
def initialize to
6+
@to = to
7+
Zip::ZipFile.open(to, Zip::ZipFile::CREATE) do |zip|
8+
@zip = zip
9+
add_doc_props
10+
add_relationship_part
11+
add_worksheets_directory
12+
@doc = Document.new(self)
13+
yield @doc
14+
add_content_types
15+
add_workbook_part
16+
end
17+
end
18+
19+
def add_workbook_part
20+
@zip.get_output_stream "xl/workbook.xml" do |f|
21+
f.puts <<-ends
22+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
23+
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
24+
<sheets>
25+
ends
26+
@doc.sheets.each_with_index do |sheet, ndx|
27+
f.puts "<sheet name=\"#{sheet.name}\" sheetId=\"#{ndx + 1}\" r:id=\"rId#{ndx + 2}\"/>"
28+
end
29+
f.puts "</sheets></workbook>"
30+
end
31+
end
32+
33+
def add_worksheets_directory
34+
@zip.mkdir "xl"
35+
@zip.mkdir "xl/worksheets"
36+
end
37+
38+
def open_stream_for_sheet sheet_name
39+
@zip.get_output_stream "xl/worksheets/#{sheet_name}.xml" do |f|
40+
yield f
41+
end
42+
end
43+
44+
def add_content_types
45+
@zip.get_output_stream "[Content_Types].xml" do |f|
46+
f.puts '<?xml version="1.0" encoding="UTF-8"?>'
47+
f.puts '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'
48+
f.puts <<-ends
49+
<Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
50+
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
51+
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
52+
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
53+
ends
54+
if @doc.has_shared_strings?
55+
f.puts '<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>'
56+
end
57+
@doc.sheets.each do |sheet|
58+
f.puts "<Override PartName=\"/xl/worksheets/#{sheet.name}.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/>"
59+
end
60+
if @doc.has_styles?
61+
f.puts '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>'
62+
end
63+
f.puts "</Types>"
64+
end
65+
end
66+
67+
def add_relationship_part
68+
@zip.mkdir "_rels"
69+
@zip.get_output_stream "_rels/.rels" do |f|
70+
f.puts <<-ends
71+
<?xml version="1.0" encoding="UTF-8"?>
72+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
73+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
74+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
75+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
76+
</Relationships>
77+
ends
78+
end
79+
end
80+
81+
def add_doc_props
82+
@zip.mkdir "docProps"
83+
@zip.get_output_stream "docProps/core.xml" do |f|
84+
f.puts <<-ends
85+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
86+
<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">
87+
<dcterms:created xsi:type="dcterms:W3CDTF">2010-07-20T14:30:58.00Z</dcterms:created>
88+
<cp:revision>0</cp:revision>
89+
</cp:coreProperties>
90+
ends
91+
end
92+
@zip.get_output_stream "docProps/app.xml" do |f|
93+
f.puts <<-ends
94+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
95+
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
96+
<TotalTime>0</TotalTime>
97+
</Properties>
98+
ends
99+
end
100+
end
101+
102+
end
103+
104+
end
105+
106+

lib/simple_xlsx/serializer.rb~

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module SimpleXlsx
2+
3+
class Serializer
4+
end
5+
6+
end

lib/simple_xlsx/sheet.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module SimpleXlsx
2+
3+
class Sheet
4+
attr_reader :name
5+
6+
def initialize document, name, stream, &block
7+
@document = document
8+
@stream = stream
9+
@name = name
10+
@row_ndx = 1
11+
@stream.write <<-ends
12+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
13+
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
14+
<sheetData>
15+
ends
16+
if block_given?
17+
yield self
18+
end
19+
@stream.write "</sheetData></worksheet>"
20+
end
21+
22+
def add_row arry
23+
row = ["<row r=\"#{@row_ndx}\">"]
24+
arry.each_with_index do |value, col_ndx|
25+
kind, ccontent = Sheet.format_field_and_type value
26+
row << "<c r=\"#{Sheet.column_index(col_ndx)}#{@row_ndx}\" t=#{kind}>#{ccontent}</c>"
27+
end
28+
row << "</row>"
29+
@row_ndx += 1
30+
@stream.write(row.join())
31+
end
32+
33+
def self.format_field_and_type value
34+
if value.is_a?(String)
35+
[:inlineStr, "<is><t>#{value.to_xs}</t></is>"]
36+
elsif value.is_a?(Numeric)
37+
[:inlineStr, "<is><t>#{value.to_s.to_xs}</t></is>"]
38+
elsif value.is_a?(Date)
39+
[:inlineStr, "<is><t>#{value.to_s.to_xs}</t></is>"]
40+
elsif value.is_a?(DateTime)
41+
[:inlineStr, "<is><t>#{value.to_s.to_xs}</t></is>"]
42+
elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
43+
[:inlineStr, "<is><t>#{value.to_s.to_xs}</t></is>"]
44+
else
45+
[:inlineStr, "<is><t>#{value.to_s.to_xs}</t></is>"]
46+
end
47+
end
48+
49+
def self.abc
50+
@@abc ||= ('A'..'Z').to_a
51+
end
52+
53+
def self.column_index n
54+
result = []
55+
while n >= 26 do
56+
result << abc[n % 26]
57+
n /= 26
58+
end
59+
result << abc[n]
60+
result.reverse.join
61+
end
62+
63+
end
64+
end

lib/simple_xlsx/sheet.rb~

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module SimpleXlsx
2+
class Sheet
3+
def initialize document, name
4+
end
5+
end
6+
end

0 commit comments

Comments
 (0)