axlsx 1.0.15 → 1.1.0

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.
Files changed (176) hide show
  1. data/.yardopts +1 -0
  2. data/CHANGELOG.md +40 -2
  3. data/README.md +223 -92
  4. data/examples/example.csv +1000 -0
  5. data/examples/example.rb +266 -132
  6. data/examples/example.xlsx +0 -0
  7. data/examples/example_streamed.xlsx +0 -0
  8. data/examples/no-use_autowidth.xlsx +0 -0
  9. data/examples/shared_strings_example.xlsx +0 -0
  10. data/lib/axlsx/content_type/content_type.rb +13 -12
  11. data/lib/axlsx/content_type/default.rb +10 -6
  12. data/lib/axlsx/content_type/override.rb +13 -8
  13. data/lib/axlsx/doc_props/app.rb +38 -41
  14. data/lib/axlsx/doc_props/core.rb +13 -17
  15. data/lib/axlsx/drawing/axis.rb +39 -19
  16. data/lib/axlsx/drawing/bar_3D_chart.rb +34 -32
  17. data/lib/axlsx/drawing/bar_series.rb +14 -14
  18. data/lib/axlsx/drawing/cat_axis.rb +16 -14
  19. data/lib/axlsx/drawing/cat_axis_data.rb +17 -18
  20. data/lib/axlsx/drawing/chart.rb +40 -41
  21. data/lib/axlsx/drawing/drawing.rb +27 -12
  22. data/lib/axlsx/drawing/graphic_frame.rb +22 -21
  23. data/lib/axlsx/drawing/hlink_click.rb~ +0 -0
  24. data/lib/axlsx/drawing/hyperlink.rb +96 -0
  25. data/lib/axlsx/drawing/hyperlink.rb~ +64 -0
  26. data/lib/axlsx/drawing/line_3D_chart.rb +31 -28
  27. data/lib/axlsx/drawing/line_series.rb +12 -11
  28. data/lib/axlsx/drawing/marker.rb +11 -8
  29. data/lib/axlsx/drawing/named_axis_data.rb +36 -0
  30. data/lib/axlsx/drawing/one_cell_anchor.rb +18 -16
  31. data/lib/axlsx/drawing/pic.rb +47 -33
  32. data/lib/axlsx/drawing/picture_locking.rb +22 -18
  33. data/lib/axlsx/drawing/pie_3D_chart.rb +11 -8
  34. data/lib/axlsx/drawing/pie_series.rb +16 -12
  35. data/lib/axlsx/drawing/scaling.rb +11 -10
  36. data/lib/axlsx/drawing/scatter_chart.rb +69 -0
  37. data/lib/axlsx/drawing/scatter_series.rb +39 -0
  38. data/lib/axlsx/drawing/ser_axis.rb +11 -10
  39. data/lib/axlsx/drawing/series.rb +16 -15
  40. data/lib/axlsx/drawing/series_title.rb +15 -14
  41. data/lib/axlsx/drawing/title.rb +27 -26
  42. data/lib/axlsx/drawing/two_cell_anchor.rb +19 -20
  43. data/lib/axlsx/drawing/val_axis.rb +9 -7
  44. data/lib/axlsx/drawing/val_axis_data.rb +18 -17
  45. data/lib/axlsx/drawing/view_3D.rb +23 -20
  46. data/lib/axlsx/package.rb +117 -52
  47. data/lib/axlsx/rels/relationship.rb +27 -8
  48. data/lib/axlsx/rels/relationships.rb +9 -2
  49. data/lib/axlsx/stylesheet/#num_fmt.rb# +69 -0
  50. data/lib/axlsx/stylesheet/border.rb +28 -23
  51. data/lib/axlsx/stylesheet/border_pr.rb +17 -15
  52. data/lib/axlsx/stylesheet/cell_alignment.rb +24 -21
  53. data/lib/axlsx/stylesheet/cell_protection.rb +11 -7
  54. data/lib/axlsx/stylesheet/cell_style.rb +9 -5
  55. data/lib/axlsx/stylesheet/color.rb +21 -14
  56. data/lib/axlsx/stylesheet/fill.rb +8 -5
  57. data/lib/axlsx/stylesheet/font.rb +22 -14
  58. data/lib/axlsx/stylesheet/gradient_fill.rb +20 -17
  59. data/lib/axlsx/stylesheet/gradient_stop.rb +10 -6
  60. data/lib/axlsx/stylesheet/num_fmt.rb +13 -6
  61. data/lib/axlsx/stylesheet/pattern_fill.rb +26 -10
  62. data/lib/axlsx/stylesheet/styles.rb +53 -41
  63. data/lib/axlsx/stylesheet/table_style.rb +10 -4
  64. data/lib/axlsx/stylesheet/table_style_element.rb +11 -7
  65. data/lib/axlsx/stylesheet/table_styles.rb +12 -8
  66. data/lib/axlsx/stylesheet/xf.rb +30 -25
  67. data/lib/axlsx/util/cbf.rb +69 -33
  68. data/lib/axlsx/util/constants.rb +39 -20
  69. data/lib/axlsx/util/ms_off_crypto.rb +10 -10
  70. data/lib/axlsx/util/parser.rb +1 -0
  71. data/lib/axlsx/util/simple_typed_list.rb +20 -10
  72. data/lib/axlsx/util/storage.rb +1 -1
  73. data/lib/axlsx/util/validators.rb +23 -7
  74. data/lib/axlsx/version.rb +5 -2
  75. data/lib/axlsx/workbook/shared_strings_table.rb +69 -0
  76. data/lib/axlsx/workbook/shared_strings_table.rb~ +69 -0
  77. data/lib/axlsx/workbook/workbook.rb +82 -26
  78. data/lib/axlsx/workbook/worksheet/cell.rb +172 -117
  79. data/lib/axlsx/workbook/worksheet/col.rb +114 -0
  80. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  81. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +29 -0
  82. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
  83. data/lib/axlsx/workbook/worksheet/row.rb +53 -21
  84. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  85. data/lib/axlsx/workbook/worksheet/table.rb +96 -0
  86. data/lib/axlsx/workbook/worksheet/table.rb~ +97 -0
  87. data/lib/axlsx/workbook/worksheet/worksheet.rb +270 -121
  88. data/lib/axlsx.rb +41 -14
  89. data/lib/schema/dc.xsd +5 -5
  90. data/lib/schema/dcmitype.xsd +5 -3
  91. data/lib/schema/dcterms.xsd +15 -15
  92. data/lib/schema/opc-coreProperties.xsd +6 -2
  93. data/lib/schema/xml.xsd +7 -8
  94. data/test/#benchmark.txt# +7 -0
  95. data/test/#tc_helper.rb# +3 -0
  96. data/test/benchmark.rb +81 -0
  97. data/test/benchmark.rb~ +0 -0
  98. data/test/benchmark.txt +6 -0
  99. data/test/benchmark.txt~ +6 -0
  100. data/test/content_type/tc_content_type.rb +31 -31
  101. data/test/content_type/tc_default.rb +9 -22
  102. data/test/content_type/tc_override.rb +8 -21
  103. data/test/doc_props/tc_app.rb +2 -8
  104. data/test/doc_props/tc_core.rb +6 -7
  105. data/test/drawing/tc_axis.rb +7 -3
  106. data/test/drawing/tc_bar_3D_chart.rb +6 -7
  107. data/test/drawing/tc_bar_series.rb +4 -5
  108. data/test/drawing/tc_cat_axis.rb +2 -3
  109. data/test/drawing/tc_cat_axis_data.rb +2 -3
  110. data/test/drawing/tc_chart.rb +13 -14
  111. data/test/drawing/tc_drawing.rb +7 -8
  112. data/test/drawing/tc_graphic_frame.rb +3 -4
  113. data/test/drawing/tc_hyperlink.rb +69 -0
  114. data/test/drawing/tc_line_3d_chart.rb +5 -6
  115. data/test/drawing/tc_line_series.rb +3 -4
  116. data/test/drawing/tc_marker.rb +3 -4
  117. data/test/drawing/tc_one_cell_anchor.rb +6 -7
  118. data/test/drawing/tc_pic.rb +13 -8
  119. data/test/drawing/tc_picture_locking.rb +2 -3
  120. data/test/drawing/tc_pie_3D_chart.rb +5 -6
  121. data/test/drawing/tc_pie_series.rb +4 -5
  122. data/test/drawing/tc_scaling.rb +3 -4
  123. data/test/drawing/tc_scatter_chart.rb +43 -0
  124. data/test/drawing/tc_scatter_series.rb +20 -0
  125. data/test/drawing/tc_ser_axis.rb +2 -3
  126. data/test/drawing/tc_series.rb +4 -5
  127. data/test/drawing/tc_series_title.rb +4 -5
  128. data/test/drawing/tc_title.rb +4 -5
  129. data/test/drawing/tc_two_cell_anchor.rb +4 -5
  130. data/test/drawing/tc_val_axis.rb +2 -3
  131. data/test/drawing/tc_val_axis_data.rb +2 -3
  132. data/test/drawing/tc_view_3D.rb +6 -7
  133. data/test/example.csv +1000 -0
  134. data/test/example.xlsx +0 -0
  135. data/test/example_streamed.xlsx +0 -0
  136. data/test/profile.rb +33 -0
  137. data/test/rels/tc_relationship.rb +10 -6
  138. data/test/rels/tc_relationships.rb +4 -5
  139. data/test/stylesheet/tc_border.rb +3 -4
  140. data/test/stylesheet/tc_border_pr.rb +3 -4
  141. data/test/stylesheet/tc_cell_alignment.rb +10 -6
  142. data/test/stylesheet/tc_cell_protection.rb +2 -3
  143. data/test/stylesheet/tc_cell_style.rb +2 -3
  144. data/test/stylesheet/tc_color.rb +2 -3
  145. data/test/stylesheet/tc_fill.rb +1 -2
  146. data/test/stylesheet/tc_font.rb +12 -5
  147. data/test/stylesheet/tc_gradient_fill.rb +1 -2
  148. data/test/stylesheet/tc_gradient_stop.rb +1 -2
  149. data/test/stylesheet/tc_num_fmt.rb +1 -2
  150. data/test/stylesheet/tc_pattern_fill.rb +3 -4
  151. data/test/stylesheet/tc_styles.rb +16 -6
  152. data/test/stylesheet/tc_table_style.rb +2 -3
  153. data/test/stylesheet/tc_table_style_element.rb +2 -3
  154. data/test/stylesheet/tc_table_styles.rb +3 -4
  155. data/test/stylesheet/tc_xf.rb +16 -17
  156. data/test/tc_axlsx.rb +39 -0
  157. data/test/tc_axlsx.rb~ +0 -0
  158. data/test/tc_helper.rb +3 -0
  159. data/test/tc_helper.rb~ +3 -0
  160. data/test/tc_package.rb +43 -9
  161. data/test/util/tc_simple_typed_list.rb +8 -9
  162. data/test/util/tc_validators.rb +7 -8
  163. data/test/workbook/tc_shared_strings_table.rb +38 -0
  164. data/test/workbook/tc_workbook.rb +32 -6
  165. data/test/workbook/worksheet/table/tc_table.rb +71 -0
  166. data/test/workbook/worksheet/table/tc_table.rb~ +72 -0
  167. data/test/workbook/worksheet/tc_cell.rb +70 -13
  168. data/test/workbook/worksheet/tc_col.rb +59 -0
  169. data/test/workbook/worksheet/tc_col.rb~ +10 -0
  170. data/test/workbook/worksheet/tc_date_time_converter.rb +126 -0
  171. data/test/workbook/worksheet/tc_page_margins.rb +97 -0
  172. data/test/workbook/worksheet/tc_row.rb +54 -4
  173. data/test/workbook/worksheet/tc_worksheet.rb +198 -31
  174. metadata +167 -111
  175. data/test/drawing/tc_line_series.tc~ +0 -34
  176. data/test/drawing/tc_picture_locking.rb~ +0 -77
@@ -0,0 +1,69 @@
1
+ # encoding: UTF-8
2
+ module Axlsx
3
+
4
+ # The Shared String Table class is responsible for managing and serializing common strings in a workbook.
5
+ # While the ECMA-376 spec allows for both inline and shared strings it seems that at least some applications like iWorks Numbers
6
+ # and Google Docs require that the shared string table is populated in order to interoperate properly.
7
+ # As a developer, you should never need to directly work against this class. Simply set 'use_shared_strings'
8
+ # on the package or workbook to generate a package that uses the shared strings table instead of inline strings.
9
+ # @note Serialization performance is affected by using this serialization method so if you do not need interoperability
10
+ # it is recomended that you use the default inline string method of serialization.
11
+ class SharedStringsTable
12
+
13
+ # The total number of strings in the workbook including duplicates
14
+ # Empty cells are treated as blank strings
15
+ # @return [Integer]
16
+ attr_reader :count
17
+
18
+ # The total number of unique strings in the workbook.
19
+ # @return [Integer]
20
+ def unique_count
21
+ @unique_cells.size
22
+ end
23
+
24
+ # An array of unique cells. Multiple attributes of the cell are used in comparison
25
+ # each of these unique cells is parsed into the shared string table.
26
+ # @see Cell#sharable
27
+ attr_reader :unique_cells
28
+
29
+ # Creates a new Shared Strings Table agains an array of cells
30
+ # @param [Array] cells This is an array of all of the cells in the workbook
31
+ def initialize(cells)
32
+ cells = cells.flatten.reject { |c| c.type != :string || c.value.nil? || c.value.start_with?('=') }
33
+ @count = cells.size
34
+ @unique_cells = []
35
+ @shared_xml_string = ""
36
+ resolve(cells)
37
+ end
38
+
39
+ # Serializes the object
40
+ # @param [String] str
41
+ # @return [String]
42
+ def to_xml_string
43
+ '<?xml version="1.0" encoding="UTF-8"?><sst xmlns="' << XML_NS << '" count="' << @count.to_s << '" uniqueCount="' << unique_count.to_s << '">' << @shared_xml_string << '</sst>'
44
+ end
45
+
46
+ private
47
+
48
+ # Interate over all of the cells in the array.
49
+ # if our unique cells array does not contain a sharable cell,
50
+ # add the cell to our unique cells array and set the ssti attribute on the index of this cell in the shared strings table
51
+ # if a sharable cell already exists in our unique_cells array, set the ssti attribute of the cell and move on.
52
+ # @return [Array] unique cells
53
+ def resolve(cells)
54
+ cells.each do |cell|
55
+ cell_hash = cell.shareable_hash
56
+ index = @unique_cells.index do |item|
57
+ item == cell_hash
58
+ end
59
+ if index == nil
60
+ cell.send :ssti=, @unique_cells.size
61
+ @shared_xml_string << '<si>' << cell.run_xml_string << '</si>'
62
+ @unique_cells << cell_hash
63
+ else
64
+ cell.send :ssti=, index
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,69 @@
1
+ # encoding: UTF--8
2
+ module Axlsx
3
+
4
+ # The Shared String Table class is responsible for managing and serializing common strings in a workbook.
5
+ # While the ECMA-376 spec allows for both inline and shared strings it seems that at least some applications like Numbers (Mac)
6
+ # and Google Docs require that the shared string table is populated in order to interoperate properly.
7
+ # As a developer, you should never need to directly work against this class. Simply set 'use_shared_strings'
8
+ # on the package or workbook to generate a package that uses the shared strings table instead of inline strings.
9
+ # @note Serialization performance is affected by using this serialization method so if you do not need interoperability
10
+ # it is recomended that you use the default inline string method of serialization.
11
+ class SharedStringsTable
12
+
13
+ # The total number of strings in the workbook including duplicates
14
+ # Empty cells are treated as blank strings
15
+ # @return [Integer]
16
+ attr_reader :count
17
+
18
+ # The total number of unique strings in the workbook.
19
+ # @return [Integer]
20
+ def unique_count
21
+ @unique_cells.size
22
+ end
23
+
24
+ # An array of unique cells. Multiple attributes of the cell are used in comparison
25
+ # each of these unique cells is parsed into the shared string table.
26
+ # @see Cell#sharable
27
+ attr_reader :unique_cells
28
+
29
+ # Creates a new Shared Strings Table agains an array of cells
30
+ # @param [Array] cells This is an array of all of the cells in the workbook
31
+ def initialize(cells)
32
+ cells = cells.reject { |c| c.type != :string }
33
+ @count = cells.size
34
+ @unique_cells = []
35
+ resolve cells
36
+ end
37
+
38
+ # Interate over all of the cells in the array.
39
+ # if our unique cells array does not contain a sharable cell,
40
+ # add the cell to our unique cells array and set the ssti attribute on the index of this cell in the shared strings table
41
+ # if a sharable cell already exists in our unique_cells array, set the ssti attribute of the cell and move on.
42
+ # @return [Array] unique cells
43
+ def resolve(cells)
44
+ cells.each do |cell|
45
+ index = @unique_cells.index { |item| item.sharable(cell) }
46
+ if index == nil
47
+ cell.ssti = @unique_cells.size
48
+ @unique_cells << cell
49
+ else
50
+ cell.ssti = index
51
+ end
52
+ end
53
+ end
54
+
55
+ # Generate the xml document for the Shared Strings Table
56
+ # @return [String]
57
+ def to_xml
58
+ builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
59
+ xml.sst(:xmlns => Axlsx::XML_NS, :count => count, :uniqueCount => unique_count) {
60
+ @unique_cells.each do |cell|
61
+ xml.si { xml.t cell.value.to_s }
62
+ end
63
+ }
64
+ end
65
+ builder.to_xml(:save_with => 0)
66
+ end
67
+ end
68
+
69
+ end
@@ -1,13 +1,18 @@
1
1
  # -*- coding: utf-8 -*-
2
- module Axlsx
2
+ module Axlsx
3
3
 
4
+ require 'axlsx/workbook/worksheet/date_time_converter.rb'
4
5
  require 'axlsx/workbook/worksheet/cell.rb'
6
+ require 'axlsx/workbook/worksheet/page_margins.rb'
5
7
  require 'axlsx/workbook/worksheet/row.rb'
8
+ require 'axlsx/workbook/worksheet/col.rb'
6
9
  require 'axlsx/workbook/worksheet/worksheet.rb'
10
+ require 'axlsx/workbook/shared_strings_table.rb'
11
+ require 'axlsx/workbook/worksheet/table.rb'
7
12
 
8
13
  # The Workbook class is an xlsx workbook that manages worksheets, charts, drawings and styles.
9
14
  # The following parts of the Office Open XML spreadsheet specification are not implimented in this version.
10
- #
15
+ #
11
16
  # bookViews
12
17
  # calcPr
13
18
  # customWorkbookViews
@@ -30,7 +35,20 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
30
35
  # *workbookPr is only supported to the extend of date1904
31
36
  class Workbook
32
37
 
33
- # A collection of worksheets associated with this workbook.
38
+ # When true, the Package will be generated with a shared string table. This may be required by some OOXML processors that do not
39
+ # adhere to the ECMA specification that dictates string may be inline in the sheet.
40
+ # Using this option will increase the time required to serialize the document as every string in every cell must be analzed and referenced.
41
+ # @return [Boolean]
42
+ attr_reader :use_shared_strings
43
+
44
+ # @see use_shared_strings
45
+ def use_shared_strings=(v)
46
+ Axlsx::validate_boolean(v)
47
+ @use_shared_strings = v
48
+ end
49
+
50
+
51
+ # A collection of worksheets associated with this workbook.
34
52
  # @note The recommended way to manage worksheets is add_worksheet
35
53
  # @see Workbook#add_worksheet
36
54
  # @see Worksheet
@@ -58,6 +76,14 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
58
76
  # @return [SimpleTypedList]
59
77
  attr_reader :drawings
60
78
 
79
+ # A colllection of tables associated with this workbook
80
+ # @note The recommended way to manage drawings is Worksheet#add_table
81
+ # @see Worksheet#add_table
82
+ # @see Table
83
+ # @return [SimpleTypedList]
84
+ attr_reader :tables
85
+
86
+
61
87
  # The styles associated with this workbook
62
88
  # @note The recommended way to manage styles is Styles#add_style
63
89
  # @see Style#add_style
@@ -72,30 +98,35 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
72
98
  # Indicates if the epoc date for serialization should be 1904. If false, 1900 is used.
73
99
  @@date1904 = false
74
100
 
75
- # lets come back to this later when we are ready for parsing.
101
+
102
+ # lets come back to this later when we are ready for parsing.
76
103
  #def self.parse entry
77
104
  # io = entry.get_input_stream
78
105
  # w = self.new
79
106
  # w.parser_xml = Nokogiri::XML(io.read)
80
- # w.parse_string :date1904, "//xmlns:workbookPr/@date1904"
107
+ # w.parse_string :date1904, "//xmlns:workbookPr/@date1904"
81
108
  # w
82
109
  #end
83
110
 
84
111
  # Creates a new Workbook
85
- # @option options [Boolean] date1904
112
+ # The recomended way to work with workbooks is via Package#workbook
113
+ # @option options [Boolean] date1904. If this is not specified, date1904 is set to false. Office 2011 for Mac defaults to false.
86
114
  def initialize(options={})
87
115
  @styles = Styles.new
88
116
  @worksheets = SimpleTypedList.new Worksheet
89
117
  @drawings = SimpleTypedList.new Drawing
90
118
  @charts = SimpleTypedList.new Chart
91
119
  @images = SimpleTypedList.new Pic
92
- self.date1904= options[:date1904] unless options[:date1904].nil?
93
- yield self if block_given?
120
+ @tables = SimpleTypedList.new Table
121
+ @use_autowidth = true
122
+
123
+ self.date1904= !options[:date1904].nil? && options[:date1904]
124
+ yield self if block_given?
94
125
  end
95
126
 
96
127
  # Instance level access to the class variable 1904
97
128
  # @return [Boolean]
98
- def date1904() @@date1904; end
129
+ def date1904() @@date1904; end
99
130
 
100
131
  # see @date1904
101
132
  def date1904=(v) Axlsx::validate_boolean v; @@date1904 = v; end
@@ -108,9 +139,18 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
108
139
  # @return [Boolean]
109
140
  def self.date1904() @@date1904; end
110
141
 
142
+ # Indicates if the workbook should use autowidths or not.
143
+ # this must be set before instantiating a worksheet to avoid Rmagix inclusion
144
+ # @return [Boolean]
145
+ def use_autowidth() @use_autowidth; end
146
+
147
+ # see @use_autowidth
148
+ def use_autowidth=(v) Axlsx::validate_boolean v; @use_autowidth = v; end
149
+
111
150
  # Adds a worksheet to this workbook
112
151
  # @return [Worksheet]
113
152
  # @option options [String] name The name of the worksheet.
153
+ # @option options [Hash] page_margins The page margins for the worksheet.
114
154
  # @see Worksheet#initialize
115
155
  def add_worksheet(options={})
116
156
  worksheet = Worksheet.new(self, options)
@@ -124,38 +164,54 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
124
164
  r = Relationships.new
125
165
  @worksheets.each do |sheet|
126
166
  r << Relationship.new(WORKSHEET_R, WORKSHEET_PN % (r.size+1))
127
- end
167
+ end
128
168
  r << Relationship.new(STYLES_R, STYLES_PN)
169
+ if use_shared_strings
170
+ r << Relationship.new(SHARED_STRINGS_R, SHARED_STRINGS_PN)
171
+ end
129
172
  r
130
173
  end
131
174
 
175
+ # generates a shared string object against all cells in all worksheets.
176
+ # @return [SharedStringTable]
177
+ def shared_strings
178
+ SharedStringsTable.new(worksheets.collect { |ws| ws.cells })
179
+ end
180
+
132
181
  # returns a range of cells in a worksheet
133
- # @param [String] cell_def The excel style reference defining the worksheet and cells. The range must specify the sheet to
182
+ # @param [String] cell_def The excel style reference defining the worksheet and cells. The range must specify the sheet to
134
183
  # retrieve the cells from. e.g. range('Sheet1!A1:B2') will return an array of four cells [A1, A2, B1, B2] while range('Sheet1!A1') will return a single Cell.
135
184
  # @return [Cell, Array]
136
185
  def [](cell_def)
137
186
  sheet_name = cell_def.split('!')[0] if cell_def.match('!')
138
187
  worksheet = self.worksheets.select { |s| s.name == sheet_name }.first
139
- raise ArgumentError, 'Unknown Sheet' unless sheet_name && worksheet.is_a?(Worksheet)
188
+ raise ArgumentError, 'Unknown Sheet' unless sheet_name && worksheet.is_a?(Worksheet)
140
189
  worksheet[cell_def.gsub(/.+!/,"")]
141
190
  end
142
191
 
143
- # Serializes the workbook document
192
+ # Serialize the workbook
193
+ # @param [String] str
144
194
  # @return [String]
145
- def to_xml()
195
+ def to_xml_string(str='')
146
196
  add_worksheet unless worksheets.size > 0
147
- builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
148
- xml.workbook(:xmlns => XML_NS, :'xmlns:r' => XML_NS_R) {
149
- xml.workbookPr(:date1904=>@@date1904)
150
- #<x:workbookProtection workbookPassword="xsd:hexBinary data" lockStructure="1" lockWindows="1" />
151
- xml.sheets {
152
- @worksheets.each_with_index do |sheet, index|
153
- xml.sheet(:name=>sheet.name, :sheetId=>index+1, :"r:id"=>sheet.rId)
154
- end
155
- }
156
- }
157
- end
158
- builder.to_xml(:indent=>0)
197
+ str << '<?xml version="1.0" encoding="UTF-8"?>'
198
+ str << '<workbook xmlns="' << XML_NS << '" xmlns:r="' << XML_NS_R << '">'
199
+ str << '<workbookPr date1904="' << @@date1904.to_s << '"/>'
200
+ str << '<sheets>'
201
+ @worksheets.each_with_index do |sheet, index|
202
+ str << '<sheet name="' << sheet.name << '" sheetId="' << (index+1).to_s << '" r:id="' << sheet.rId << '"/>'
203
+ end
204
+ str << '</sheets>'
205
+ str << '<definedNames>'
206
+ @worksheets.each_with_index do |sheet, index|
207
+ if sheet.auto_filter
208
+ str << '<definedName name="_xlnm._FilterDatabase" localSheetId="' << index.to_s << '" hidden="1">'
209
+ str << sheet.abs_auto_filter << '</definedName>'
210
+ end
211
+ end
212
+ str << '</definedNames>'
213
+ str << '</workbook>'
159
214
  end
215
+
160
216
  end
161
217
  end