axlsx 1.0.16 → 1.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.yardopts +1 -0
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +38 -12
  4. data/examples/example.rb +18 -1
  5. data/lib/axlsx.rb +14 -8
  6. data/lib/axlsx/content_type/content_type.rb +1 -0
  7. data/lib/axlsx/content_type/default.rb +1 -0
  8. data/lib/axlsx/content_type/override.rb +1 -0
  9. data/lib/axlsx/doc_props/app.rb +1 -1
  10. data/lib/axlsx/doc_props/core.rb +1 -0
  11. data/lib/axlsx/drawing/axis.rb +1 -0
  12. data/lib/axlsx/drawing/bar_3D_chart.rb +1 -0
  13. data/lib/axlsx/drawing/bar_series.rb +1 -0
  14. data/lib/axlsx/drawing/cat_axis.rb +1 -0
  15. data/lib/axlsx/drawing/cat_axis_data.rb +1 -0
  16. data/lib/axlsx/drawing/chart.rb +1 -1
  17. data/lib/axlsx/drawing/drawing.rb +1 -0
  18. data/lib/axlsx/drawing/graphic_frame.rb +1 -0
  19. data/lib/axlsx/drawing/hyperlink.rb +1 -1
  20. data/lib/axlsx/drawing/line_3D_chart.rb +1 -0
  21. data/lib/axlsx/drawing/line_series.rb +1 -0
  22. data/lib/axlsx/drawing/marker.rb +1 -0
  23. data/lib/axlsx/drawing/one_cell_anchor.rb +1 -0
  24. data/lib/axlsx/drawing/pic.rb +1 -1
  25. data/lib/axlsx/drawing/picture_locking.rb +1 -0
  26. data/lib/axlsx/drawing/pie_3D_chart.rb +1 -0
  27. data/lib/axlsx/drawing/pie_series.rb +1 -0
  28. data/lib/axlsx/drawing/scaling.rb +1 -0
  29. data/lib/axlsx/drawing/ser_axis.rb +1 -0
  30. data/lib/axlsx/drawing/series.rb +1 -0
  31. data/lib/axlsx/drawing/series_title.rb +1 -0
  32. data/lib/axlsx/drawing/title.rb +1 -0
  33. data/lib/axlsx/drawing/two_cell_anchor.rb +1 -0
  34. data/lib/axlsx/drawing/val_axis.rb +1 -0
  35. data/lib/axlsx/drawing/val_axis_data.rb +1 -0
  36. data/lib/axlsx/drawing/view_3D.rb +1 -0
  37. data/lib/axlsx/package.rb +63 -15
  38. data/lib/axlsx/rels/relationship.rb +1 -0
  39. data/lib/axlsx/rels/relationships.rb +1 -0
  40. data/lib/axlsx/stylesheet/border.rb +1 -0
  41. data/lib/axlsx/stylesheet/border_pr.rb +1 -0
  42. data/lib/axlsx/stylesheet/cell_alignment.rb +1 -0
  43. data/lib/axlsx/stylesheet/cell_protection.rb +1 -0
  44. data/lib/axlsx/stylesheet/cell_style.rb +1 -0
  45. data/lib/axlsx/stylesheet/color.rb +1 -0
  46. data/lib/axlsx/stylesheet/fill.rb +1 -0
  47. data/lib/axlsx/stylesheet/font.rb +1 -0
  48. data/lib/axlsx/stylesheet/gradient_fill.rb +1 -1
  49. data/lib/axlsx/stylesheet/gradient_stop.rb +1 -1
  50. data/lib/axlsx/stylesheet/num_fmt.rb +1 -0
  51. data/lib/axlsx/stylesheet/pattern_fill.rb +1 -0
  52. data/lib/axlsx/stylesheet/styles.rb +6 -6
  53. data/lib/axlsx/stylesheet/table_style.rb +1 -0
  54. data/lib/axlsx/stylesheet/table_style_element.rb +1 -0
  55. data/lib/axlsx/stylesheet/table_styles.rb +1 -0
  56. data/lib/axlsx/stylesheet/xf.rb +1 -0
  57. data/lib/axlsx/util/cbf.rb +1 -0
  58. data/lib/axlsx/util/constants.rb +7 -0
  59. data/lib/axlsx/util/ms_off_crypto.rb +1 -1
  60. data/lib/axlsx/util/parser.rb +1 -0
  61. data/lib/axlsx/util/simple_typed_list.rb +2 -1
  62. data/lib/axlsx/util/storage.rb +1 -0
  63. data/lib/axlsx/util/validators.rb +11 -2
  64. data/lib/axlsx/version.rb +2 -1
  65. data/lib/axlsx/workbook/shared_strings_table.rb +71 -0
  66. data/lib/axlsx/workbook/shared_strings_table.rb~ +69 -0
  67. data/lib/axlsx/workbook/workbook.rb +25 -3
  68. data/lib/axlsx/workbook/worksheet/cell.rb +75 -37
  69. data/lib/axlsx/workbook/worksheet/row.rb +15 -0
  70. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  71. data/lib/axlsx/workbook/worksheet/worksheet.rb +35 -3
  72. data/test/content_type/tc_content_type.rb +2 -0
  73. data/test/content_type/tc_default.rb +2 -0
  74. data/test/content_type/tc_override.rb +1 -0
  75. data/test/tc_package.rb +31 -0
  76. data/test/workbook/tc_shared_strings_table.rb +39 -0
  77. data/test/workbook/tc_shared_strings_table.rb~ +8 -0
  78. data/test/workbook/tc_workbook.rb +8 -0
  79. data/test/workbook/worksheet/tc_cell.rb +13 -0
  80. data/test/workbook/worksheet/tc_worksheet.rb +20 -3
  81. metadata +126 -101
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A relationship defines a reference between package parts.
3
4
  # @note Packages automatcially manage relationships.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  require 'axlsx/rels/relationship.rb'
3
4
 
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # This class details a border used in Office Open XML spreadsheet styles.
3
4
  class Border
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A border part.
3
4
  class BorderPr
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # CellAlignment stores information about the cell alignment of a style Xf Object.
3
4
  # @note Using Styles#add_style is the recommended way to manage cell alignment.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # CellProtection stores information about locking or hiding cells in spreadsheet.
3
4
  # @note Using Styles#add_style is the recommended way to manage cell protection.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # CellStyle defines named styles that reference defined formatting records and can be used in your worksheet.
3
4
  # @note Using Styles#add_style is the recommended way to manage cell styling.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # The color class represents a color used for borders, fills an fonts
3
4
  class Color
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # The Fill is a formatting object that manages the background color, and pattern for cells.
3
4
  # @note The recommended way to manage styles in your workbook is to use Styles#add_style.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # The Font class details a font instance for use in styling cells.
3
4
  # @note The recommended way to manage fonts, and other styles is Styles#add_style
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: UTF-8
2
2
  module Axlsx
3
3
  # A GradientFill defines the color and positioning for gradiant cell fill.
4
4
  # @see Open Office XML Part 1 §18.8.24
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: UTF-8
2
2
  module Axlsx
3
3
  # The GradientStop object represents a color point in a gradient.
4
4
  # @see Open Office XML Part 1 §18.8.24
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A NumFmt object defines an identifier and formatting code for data in cells.
3
4
  # @note The recommended way to manage styles is Styles#add_style
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A PatternFill is the pattern and solid fill styling for a cell.
3
4
  # @note The recommended way to manage styles is with Styles#add_style
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: UTF-8
2
2
  module Axlsx
3
3
  require 'axlsx/stylesheet/border.rb'
4
4
  require 'axlsx/stylesheet/border_pr.rb'
@@ -253,22 +253,22 @@ module Axlsx
253
253
  # Creates the default set of styles the exel requires to be valid as well as setting up the
254
254
  # Axlsx::STYLE_THIN_BORDER
255
255
  def load_default_styles
256
- @numFmts = SimpleTypedList.new NumFmt
256
+ @numFmts = SimpleTypedList.new NumFmt, 'numFmts'
257
257
  @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDD, :formatCode=> "yyyy/mm/dd")
258
258
  @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDDHHMMSS, :formatCode=> "yyyy/mm/dd hh:mm:ss")
259
259
 
260
260
  @numFmts.lock
261
261
 
262
- @fonts = SimpleTypedList.new Font
262
+ @fonts = SimpleTypedList.new Font, 'fonts'
263
263
  @fonts << Font.new(:name => "Arial", :sz => 11, :family=>1)
264
264
  @fonts.lock
265
265
 
266
- @fills = SimpleTypedList.new Fill
266
+ @fills = SimpleTypedList.new Fill, 'fills'
267
267
  @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:none))
268
268
  @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:gray125))
269
269
  @fills.lock
270
270
 
271
- @borders = SimpleTypedList.new Border
271
+ @borders = SimpleTypedList.new Border, 'borders'
272
272
  @borders << Border.new
273
273
  black_border = Border.new
274
274
  [:left, :right, :top, :bottom].each do |item|
@@ -281,7 +281,7 @@ module Axlsx
281
281
  @cellStyleXfs << Xf.new(:borderId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
282
282
  @cellStyleXfs.lock
283
283
 
284
- @cellStyles = SimpleTypedList.new CellStyle
284
+ @cellStyles = SimpleTypedList.new CellStyle, 'cellStyles'
285
285
  @cellStyles << CellStyle.new(:name =>"Normal", :builtinId =>0, :xfId=>0)
286
286
  @cellStyles.lock
287
287
 
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A single table style definition and is a collection for tableStyleElements
3
4
  # @note Table are not supported in this version and only the defaults required for a valid workbook are created.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # an element of style that belongs to a table style.
3
4
  # @note tables and table styles are not supported in this version. This class exists in preparation for that support.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # TableStyles represents a collection of style definitions for table styles and pivot table styles.
3
4
  # @note Support for custom table styles does not exist in this version. Many of the classes required are defined in preparation for future release. Please do not attempt to add custom table styles.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # The Xf class defines a formatting record for use in Styles. The recommended way to manage styles for your workbook is with Styles#add_style
3
4
  # @see Styles#add_style
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
 
3
4
  # The Cfb class is a MS-OFF-CRYPTOGRAPHY specific OLE (MS-CBF) writer implementation. No attempt is made to re-invent the wheel for read/write of compound binary files.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
 
3
4
  # XML Encoding
@@ -64,6 +65,9 @@ module Axlsx
64
65
  # styles rels namespace
65
66
  STYLES_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
66
67
 
68
+ # shared strings namespace
69
+ SHARED_STRINGS_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
70
+
67
71
  # drawing rels namespace
68
72
  DRAWING_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
69
73
 
@@ -138,6 +142,9 @@ module Axlsx
138
142
 
139
143
  # styles part
140
144
  STYLES_PN = "styles.xml"
145
+
146
+ # shared_strings part
147
+ SHARED_STRINGS_PN = "sharedStrings.xml"
141
148
 
142
149
  # app part
143
150
  APP_PN = "docProps/app.xml"
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: UTF-8
2
2
  require 'digest'
3
3
  require 'base64'
4
4
  require 'openssl'
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # The Parser module mixes in a number of methods to help in generating a model from xml
3
4
  # This module is not included in the axlsx library at this time. It is for future development only,
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # A SimpleTypedList is a type restrictive collection that allows some of the methods from Array and supports basic xml serialization.
3
4
  # @private
@@ -153,7 +154,7 @@ module Axlsx
153
154
  # @return [String]
154
155
  def to_xml(xml)
155
156
  classname = @allowed_types[0].name.split('::').last
156
- el_name = serialize_as || (classname[0,1].downcase + classname[1..-1]).pluralize
157
+ el_name = serialize_as || (classname[0,1].downcase + classname[1..-1])
157
158
  xml.send(el_name, :count=>@list.size) {
158
159
  @list.each { |item| item.to_xml(xml) }
159
160
  }
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
 
3
4
  # The Storage class represents a storage object or stream in a compound file.
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
  # Validate a value against a specific list of allowed values.
3
4
  class RestrictionValidator
@@ -52,6 +53,14 @@ module Axlsx
52
53
  DataTypeValidator.validate(:unsigned_int, [Fixnum, Integer], v, lambda { |arg| arg.respond_to?(:>=) && arg >= 0 })
53
54
  end
54
55
 
56
+ # Requires that the value is a Fixnum Integer or Float and is greater or equal to 0
57
+ # @param [Any] v The value validated
58
+ # @raise [ArgumentError] raised if the value is not a Fixnum or Integer value greater or equal to 0
59
+ # @return [Boolean] true if the data is valid
60
+ def self.validate_unsigned_numeric(v)
61
+ DataTypeValidator.validate("Invalid column width", [Fixnum, Integer, Float], v, lambda { |arg| arg.respond_to?(:>=) && arg >= 0 })
62
+ end
63
+
55
64
  # Requires that the value is a Fixnum or Integer
56
65
  # @param [Any] v The value validated
57
66
  def self.validate_int(v)
@@ -116,10 +125,10 @@ module Axlsx
116
125
  end
117
126
 
118
127
  # Requires that the value is a valid relationship_type
119
- # XML_NS_R, TABLE_R, WORKBOOK_R, WORKSHEET_R, APP_R, RELS_R, CORE_R, STYLES_R, CHART_R, DRAWING_R are allowed
128
+ # XML_NS_R, TABLE_R, WORKBOOK_R, WORKSHEET_R, APP_R, RELS_R, CORE_R, STYLES_R, CHART_R, DRAWING_R, IMAGE_R, HYPERLINK_R, SHARED_STRINGS_R are allowed
120
129
  # @param [Any] v The value validated
121
130
  def self.validate_relationship_type(v)
122
- RestrictionValidator.validate :relationship_type, [XML_NS_R, TABLE_R, WORKBOOK_R, WORKSHEET_R, APP_R, RELS_R, CORE_R, STYLES_R, CHART_R, DRAWING_R, IMAGE_R, HYPERLINK_R], v
131
+ RestrictionValidator.validate :relationship_type, [XML_NS_R, TABLE_R, WORKBOOK_R, WORKSHEET_R, APP_R, RELS_R, CORE_R, STYLES_R, CHART_R, DRAWING_R, IMAGE_R, HYPERLINK_R, SHARED_STRINGS_R], v
123
132
  end
124
133
 
125
134
  # Requires that the value is a valid table element type
@@ -1,9 +1,10 @@
1
+ # encoding: UTF-8
1
2
  module Axlsx
2
3
 
3
4
  # The version of the gem
4
5
  # When using bunle exec rake and referencing the gem on github or locally
5
6
  # it will use the gemspec, which preloads this constant for the gem's version.
6
7
  # We check to make sure that it has not already been loaded
7
- VERSION="1.0.16" unless Axlsx.const_defined? :VERSION
8
+ VERSION="1.0.17" unless Axlsx.const_defined? :VERSION
8
9
 
9
10
  end
@@ -0,0 +1,71 @@
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.start_with?('=') }
33
+ @count = cells.size
34
+ @unique_cells = []
35
+ resolve(cells)
36
+ end
37
+
38
+
39
+ # Generate the xml document for the Shared Strings Table
40
+ # @return [String]
41
+ def to_xml
42
+ builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
43
+ xml.sst(:xmlns => Axlsx::XML_NS, :count => count, :uniqueCount => unique_count) {
44
+ @unique_cells.each do |cell|
45
+ xml.si { cell.run_xml(xml) }
46
+ end
47
+ }
48
+ end
49
+ builder.to_xml(:save_with => 0)
50
+ end
51
+
52
+ private
53
+
54
+ # Interate over all of the cells in the array.
55
+ # if our unique cells array does not contain a sharable cell,
56
+ # add the cell to our unique cells array and set the ssti attribute on the index of this cell in the shared strings table
57
+ # if a sharable cell already exists in our unique_cells array, set the ssti attribute of the cell and move on.
58
+ # @return [Array] unique cells
59
+ def resolve(cells)
60
+ cells.each do |cell|
61
+ index = @unique_cells.index { |item| item.shareable(cell) }
62
+ if index == nil
63
+ cell.send :ssti=, @unique_cells.size
64
+ @unique_cells << cell
65
+ else
66
+ cell.send :ssti=, index
67
+ end
68
+ end
69
+ end
70
+ end
71
+ 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
@@ -4,6 +4,7 @@ module Axlsx
4
4
  require 'axlsx/workbook/worksheet/cell.rb'
5
5
  require 'axlsx/workbook/worksheet/row.rb'
6
6
  require 'axlsx/workbook/worksheet/worksheet.rb'
7
+ require 'axlsx/workbook/shared_strings_table.rb'
7
8
 
8
9
  # The Workbook class is an xlsx workbook that manages worksheets, charts, drawings and styles.
9
10
  # The following parts of the Office Open XML spreadsheet specification are not implimented in this version.
@@ -30,6 +31,19 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
30
31
  # *workbookPr is only supported to the extend of date1904
31
32
  class Workbook
32
33
 
34
+ # When true, the Package will be generated with a shared string table. This may be required by some OOXML processors that do not
35
+ # adhere to the ECMA specification that dictates string may be inline in the sheet.
36
+ # Using this option will increase the time required to serialize the document as every string in every cell must be analzed and referenced.
37
+ # @return [Boolean]
38
+ attr_reader :use_shared_strings
39
+
40
+ # @see use_shared_strings
41
+ def use_shared_strings=(v)
42
+ Axlsx::validate_boolean(v)
43
+ @use_shared_strings = v
44
+ end
45
+
46
+
33
47
  # A collection of worksheets associated with this workbook.
34
48
  # @note The recommended way to manage worksheets is add_worksheet
35
49
  # @see Workbook#add_worksheet
@@ -82,9 +96,8 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
82
96
  #end
83
97
 
84
98
  # Creates a new Workbook
85
- #
86
- # @option options [Boolean] date1904. If this is not specified, we try to determine if the platform is bsd/darwin and set date1904 to true automatically.
87
- #
99
+ # The recomended way to work with workbooks is via Package#workbook
100
+ # @option options [Boolean] date1904. If this is not specified, we try to determine if the platform is bsd/darwin and set date1904 to true automatically.
88
101
  def initialize(options={})
89
102
  @styles = Styles.new
90
103
  @worksheets = SimpleTypedList.new Worksheet
@@ -136,9 +149,18 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
136
149
  r << Relationship.new(WORKSHEET_R, WORKSHEET_PN % (r.size+1))
137
150
  end
138
151
  r << Relationship.new(STYLES_R, STYLES_PN)
152
+ if use_shared_strings
153
+ r << Relationship.new(SHARED_STRINGS_R, SHARED_STRINGS_PN)
154
+ end
139
155
  r
140
156
  end
141
157
 
158
+ # generates a shared string object against all cells in all worksheets.
159
+ # @return [SharedStringTable]
160
+ def shared_strings
161
+ SharedStringsTable.new(worksheets.collect { |ws| ws.cells })
162
+ end
163
+
142
164
  # returns a range of cells in a worksheet
143
165
  # @param [String] cell_def The excel style reference defining the worksheet and cells. The range must specify the sheet to
144
166
  # 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.