xlsx_writer 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ 0.3.0 / 2012-07-10
2
+
3
+ * Enhancements
4
+
5
+ * Allow "quiet booleans" in case you don't want FALSE everywhere
6
+ * Allow freezing pane after the first (header) row
7
+
1
8
  0.2.2 / 2012-07-04
2
9
 
3
10
  * Bug fixes
data/README.markdown CHANGED
@@ -25,11 +25,21 @@ Features not present in simple_xlsx_writer:
25
25
  * header and footer, with support for images (.emf only) and page numbers
26
26
  * fits columns to text
27
27
 
28
+ ## Wishlist
29
+
30
+ 1. Optional shared string optimizer
31
+
28
32
  ## Example
29
33
 
30
34
  require 'xlsx_writer'
31
35
 
32
36
  doc = XlsxWriter::Document.new
37
+
38
+ # show TRUE for true but a blank cell instead of FALSE
39
+ doc.quiet_booleans!
40
+
41
+ # freeze pane underneath the first (header) row
42
+ doc.freeze!
33
43
 
34
44
  sheet1 = doc.add_sheet("People")
35
45
 
data/foo.rb CHANGED
@@ -11,14 +11,15 @@ require 'xlsx_writer'
11
11
 
12
12
  @doc = XlsxWriter::Document.new
13
13
 
14
- # @sheet1 = @doc.add_sheet("Sheet1")
15
- # @sheet1.add_row(['a', 'a'])
16
- # @sheet1.add_row(['a', { :value => 'a', :faded => true, :type => :String }])
17
- # @sheet1.add_row(['a', 'a'])
18
- # # @sheet1.add_row(['foo', 'bar'])
19
- # @sheet1.add_autofilter 'A1:B1'
14
+ @sheet1 = @doc.add_sheet("Sheet1")
15
+ @sheet1.add_row(['a', 'a'])
16
+ @sheet1.add_row(['a', { :value => 'a', :faded => true, :type => :String }])
17
+ @sheet1.add_row(['a', 'a'])
18
+ # @sheet1.add_row(['foo', 'bar'])
19
+ @sheet1.add_autofilter 'A1:B1'
20
20
 
21
- @sheet2 = @doc.add_sheet("Sheet2")
21
+ @doc.freeze!
22
+ @sheet2 = @doc.add_sheet("Freeze")
22
23
  @sheet2.add_row(['a', 'a'])
23
24
  @sheet2.add_row(['false1', false])
24
25
  @sheet2.add_row(['false2', {:value => false, :type => :Boolean}])
@@ -187,7 +187,7 @@ module XlsxWriter
187
187
  end
188
188
 
189
189
  def to_xml
190
- if value.nil? or (value.is_a?(String) and value.empty?)
190
+ if value.nil? or (value.is_a?(String) and value.empty?) or (value == false and quiet_booleans?)
191
191
  %{<c r="#{excel_column_letter}#{row.ndx}" s="0" t="inlineStr" />}
192
192
  elsif excel_type == :inlineStr
193
193
  %{<c r="#{excel_column_letter}#{row.ndx}" s="#{excel_style_number}" t="#{excel_type}"><is><t>#{excel_value}</t></is></c>}
@@ -195,10 +195,17 @@ module XlsxWriter
195
195
  %{<c r="#{excel_column_letter}#{row.ndx}" s="#{excel_style_number}" t="#{excel_type}"><v>#{excel_value}</v></c>}
196
196
  end
197
197
  end
198
-
198
+
199
199
  # 0 -> A (zero based!)
200
200
  def excel_column_letter
201
201
  Cell.excel_column_letter row.cells.index(self)
202
202
  end
203
+
204
+ private
205
+
206
+ def quiet_booleans?
207
+ return @quiet_booleans if defined?(@quiet_booleans)
208
+ @quiet_booleans = row.sheet.document.quiet_booleans?
209
+ end
203
210
  end
204
211
  end
@@ -28,6 +28,24 @@ module XlsxWriter
28
28
  @header_footer = HeaderFooter.new
29
29
  @mutex = ::Mutex.new
30
30
  end
31
+
32
+ # Instead of TRUE or FALSE, show TRUE and blank if false
33
+ def quiet_booleans!
34
+ @quiet_booleans = true
35
+ end
36
+
37
+ def quiet_booleans?
38
+ @quiet_booleans == true
39
+ end
40
+
41
+ # Freeze the pane under the first (header) row
42
+ def freeze!
43
+ @freeze_pane = true
44
+ end
45
+
46
+ def freeze?
47
+ @freeze_pane == true
48
+ end
31
49
 
32
50
  def add_sheet(name)
33
51
  raise ::RuntimeError, "Can't add sheet, already generated!" if generated?
@@ -81,22 +81,31 @@ module XlsxWriter
81
81
 
82
82
  # not using ERB to save memory
83
83
  def to_file(f)
84
- f.puts <<-EOS
84
+ f.write <<-EOS
85
85
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
86
86
  <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
87
- <cols>
88
87
  EOS
88
+ if document.freeze?
89
+ f.write <<-EOS
90
+ <sheetViews>
91
+ <sheetView workbookViewId="0">
92
+ <pane ySplit="1" topLeftCell="A2" activePane="bottomLeft" state="frozen"/>
93
+ </sheetView>
94
+ </sheetViews>
95
+ EOS
96
+ end
97
+ f.write %{<cols>}
89
98
  (0..max_length-1).each do |x|
90
- f.puts %{<col min="#{x+1}" max="#{x+1}" width="#{max_cell_width(x)}" bestFit="1" customWidth="1" />}
99
+ f.write %{<col min="#{x+1}" max="#{x+1}" width="#{max_cell_width(x)}" bestFit="1" customWidth="1" />}
91
100
  end
92
- f.puts %{</cols>}
93
- f.puts %{<sheetData>}
94
- rows.each { |row| f.puts row.to_xml }
95
- f.puts %{</sheetData>}
96
- autofilters.each { |autofilter| f.puts autofilter.to_xml }
97
- f.puts document.page_setup.to_xml
98
- f.puts document.header_footer.to_xml
99
- f.puts %{</worksheet>}
101
+ f.write %{</cols>}
102
+ f.write %{<sheetData>}
103
+ rows.each { |row| f.write row.to_xml }
104
+ f.write %{</sheetData>}
105
+ autofilters.each { |autofilter| f.write autofilter.to_xml }
106
+ f.write document.page_setup.to_xml
107
+ f.write document.header_footer.to_xml
108
+ f.write %{</worksheet>}
100
109
  end
101
110
 
102
111
  def max_length
@@ -1,3 +1,3 @@
1
1
  module XlsxWriter
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -8,6 +8,8 @@ describe XlsxWriter do
8
8
  @sheet1 = @doc.add_sheet("People")
9
9
  @sheet1.add_row(['header1', 'header2'])
10
10
  @sheet1.add_row(['hello', 'world'])
11
+ @sheet1.add_row(['affirmative', true])
12
+ @sheet1.add_row(['negative', false])
11
13
  end
12
14
  after do
13
15
  @doc.cleanup
@@ -17,7 +19,7 @@ describe XlsxWriter do
17
19
  File.extname(@doc.path).must_equal '.xlsx'
18
20
  end
19
21
  it "is a readable xlsx" do
20
- RemoteTable.new("file://#{@doc.path}", :format => :xlsx).rows.first.must_equal('header1' => 'hello', 'header2' => 'world')
22
+ RemoteTable.new(@doc.path, :format => :xlsx).rows.first.must_equal('header1' => 'hello', 'header2' => 'world')
21
23
  end
22
24
  it "only generates once" do
23
25
  @doc.generate
@@ -48,6 +50,52 @@ describe XlsxWriter do
48
50
  end
49
51
  end
50
52
 
53
+ describe "quiet booleans" do
54
+ before do
55
+ @doc = XlsxWriter::Document.new
56
+ @sheet1 = @doc.add_sheet("QuietBooleans")
57
+ @sheet1.add_row(['affirmative', 'negative'])
58
+ @sheet1.add_row([true, false])
59
+ end
60
+ after do
61
+ @doc.cleanup
62
+ end
63
+ it "shows TRUE or FALSE for booleans by default" do
64
+ t = RemoteTable.new(@doc.path, :format => :xlsx)
65
+ t[0]['affirmative'].must_equal 'TRUE'
66
+ t[0]['negative'].must_equal 'FALSE'
67
+ end
68
+ it "shows TRUE or blank for false if quiet booleans is enabled" do
69
+ @doc.quiet_booleans!
70
+ t = RemoteTable.new(@doc.path, :format => :xlsx)
71
+ t[0]['affirmative'].must_equal 'TRUE'
72
+ t[0]['negative'].must_equal ''
73
+ end
74
+ end
75
+
76
+ describe "freeze pane under first (header) row" do
77
+ before do
78
+ @doc = XlsxWriter::Document.new
79
+ @sheet1 = @doc.add_sheet("Freeze")
80
+ @sheet1.add_row([1, 2])
81
+ @sheet1.add_row([3, 4])
82
+ end
83
+ after do
84
+ @doc.cleanup
85
+ end
86
+ it "doesn't freeze by default" do
87
+ dir = UnixUtils.unzip @doc.path
88
+ File.read("#{dir}/xl/worksheets/sheet1.xml").wont_include 'pane'
89
+ FileUtils.rm_rf dir
90
+ end
91
+ it "shows TRUE or blank for false if quiet booleans is enabled" do
92
+ @doc.freeze!
93
+ dir = UnixUtils.unzip @doc.path
94
+ File.read("#{dir}/xl/worksheets/sheet1.xml").must_include 'pane'
95
+ FileUtils.rm_rf dir
96
+ end
97
+ end
98
+
51
99
  describe "example with autofilter, header image, and footer text" do
52
100
  before do
53
101
  @doc = XlsxWriter::Document.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlsx_writer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-07-04 00:00:00.000000000 Z
14
+ date: 2012-07-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport