summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--lib/axlsx.rb1
-rw-r--r--lib/axlsx/content_type/content_type.rb1
-rw-r--r--lib/axlsx/content_type/default.rb1
-rw-r--r--lib/axlsx/content_type/override.rb1
-rw-r--r--lib/axlsx/doc_props/app.rb2
-rw-r--r--lib/axlsx/doc_props/core.rb1
-rw-r--r--lib/axlsx/drawing/axis.rb1
-rw-r--r--lib/axlsx/drawing/bar_3D_chart.rb1
-rw-r--r--lib/axlsx/drawing/bar_series.rb1
-rw-r--r--lib/axlsx/drawing/cat_axis.rb1
-rw-r--r--lib/axlsx/drawing/cat_axis_data.rb1
-rw-r--r--lib/axlsx/drawing/chart.rb2
-rw-r--r--lib/axlsx/drawing/drawing.rb1
-rw-r--r--lib/axlsx/drawing/graphic_frame.rb1
-rw-r--r--lib/axlsx/drawing/hyperlink.rb2
-rw-r--r--lib/axlsx/drawing/line_3D_chart.rb1
-rw-r--r--lib/axlsx/drawing/line_series.rb1
-rw-r--r--lib/axlsx/drawing/marker.rb1
-rw-r--r--lib/axlsx/drawing/one_cell_anchor.rb1
-rw-r--r--lib/axlsx/drawing/pic.rb2
-rw-r--r--lib/axlsx/drawing/picture_locking.rb1
-rw-r--r--lib/axlsx/drawing/pie_3D_chart.rb1
-rw-r--r--lib/axlsx/drawing/pie_series.rb1
-rw-r--r--lib/axlsx/drawing/scaling.rb1
-rw-r--r--lib/axlsx/drawing/ser_axis.rb1
-rw-r--r--lib/axlsx/drawing/series.rb1
-rw-r--r--lib/axlsx/drawing/series_title.rb1
-rw-r--r--lib/axlsx/drawing/title.rb1
-rw-r--r--lib/axlsx/drawing/two_cell_anchor.rb1
-rw-r--r--lib/axlsx/drawing/val_axis.rb1
-rw-r--r--lib/axlsx/drawing/val_axis_data.rb1
-rw-r--r--lib/axlsx/drawing/view_3D.rb1
-rw-r--r--lib/axlsx/package.rb31
-rw-r--r--lib/axlsx/rels/relationship.rb1
-rw-r--r--lib/axlsx/rels/relationships.rb1
-rw-r--r--lib/axlsx/stylesheet/border.rb1
-rw-r--r--lib/axlsx/stylesheet/border_pr.rb1
-rw-r--r--lib/axlsx/stylesheet/cell_alignment.rb1
-rw-r--r--lib/axlsx/stylesheet/cell_protection.rb1
-rw-r--r--lib/axlsx/stylesheet/cell_style.rb1
-rw-r--r--lib/axlsx/stylesheet/color.rb1
-rw-r--r--lib/axlsx/stylesheet/fill.rb1
-rw-r--r--lib/axlsx/stylesheet/font.rb1
-rw-r--r--lib/axlsx/stylesheet/gradient_fill.rb2
-rw-r--r--lib/axlsx/stylesheet/gradient_stop.rb2
-rw-r--r--lib/axlsx/stylesheet/num_fmt.rb1
-rw-r--r--lib/axlsx/stylesheet/pattern_fill.rb1
-rw-r--r--lib/axlsx/stylesheet/styles.rb2
-rw-r--r--lib/axlsx/stylesheet/table_style.rb1
-rw-r--r--lib/axlsx/stylesheet/table_style_element.rb1
-rw-r--r--lib/axlsx/stylesheet/table_styles.rb1
-rw-r--r--lib/axlsx/stylesheet/xf.rb1
-rw-r--r--lib/axlsx/util/cbf.rb1
-rw-r--r--lib/axlsx/util/constants.rb7
-rw-r--r--lib/axlsx/util/ms_off_crypto.rb2
-rw-r--r--lib/axlsx/util/parser.rb1
-rw-r--r--lib/axlsx/util/simple_typed_list.rb1
-rw-r--r--lib/axlsx/util/storage.rb1
-rw-r--r--lib/axlsx/util/validators.rb5
-rw-r--r--lib/axlsx/version.rb1
-rw-r--r--lib/axlsx/workbook/shared_strings_table.rb72
-rw-r--r--lib/axlsx/workbook/workbook.rb24
-rw-r--r--lib/axlsx/workbook/worksheet/cell.rb88
-rw-r--r--lib/axlsx/workbook/worksheet/row.rb1
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb17
-rw-r--r--test/content_type/tc_content_type.rb2
-rw-r--r--test/content_type/tc_default.rb2
-rw-r--r--test/content_type/tc_override.rb1
-rw-r--r--test/tc_package.rb31
-rw-r--r--test/workbook/tc_shared_strings_table.rb39
-rw-r--r--test/workbook/tc_workbook.rb8
-rw-r--r--test/workbook/worksheet/tc_cell.rb13
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb8
73 files changed, 375 insertions, 39 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb
index d60a4aa8..06488091 100644
--- a/lib/axlsx.rb
+++ b/lib/axlsx.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
Encoding::default_internal = 'UTF-8' unless RUBY_VERSION < '1.9'
Encoding::default_external = 'UTF-8' unless RUBY_VERSION < '1.9'
diff --git a/lib/axlsx/content_type/content_type.rb b/lib/axlsx/content_type/content_type.rb
index 1685c9f8..32182fd5 100644
--- a/lib/axlsx/content_type/content_type.rb
+++ b/lib/axlsx/content_type/content_type.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
require 'axlsx/content_type/default.rb'
require 'axlsx/content_type/override.rb'
diff --git a/lib/axlsx/content_type/default.rb b/lib/axlsx/content_type/default.rb
index 5c688568..bd572b14 100644
--- a/lib/axlsx/content_type/default.rb
+++ b/lib/axlsx/content_type/default.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# An default content part. These parts are automatically created by for you based on the content of your package.
class Default
diff --git a/lib/axlsx/content_type/override.rb b/lib/axlsx/content_type/override.rb
index 362e834b..2513b8ff 100644
--- a/lib/axlsx/content_type/override.rb
+++ b/lib/axlsx/content_type/override.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# An override content part. These parts are automatically created by for you based on the content of your package.
class Override
diff --git a/lib/axlsx/doc_props/app.rb b/lib/axlsx/doc_props/app.rb
index df3d919d..16966da4 100644
--- a/lib/axlsx/doc_props/app.rb
+++ b/lib/axlsx/doc_props/app.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# App represents the app.xml document. The attributes for this object are primarily managed by the application the end user uses to edit the document. None of the attributes are required to serialize a valid xlsx object.
# @see shared-documentPropertiesExtended.xsd
diff --git a/lib/axlsx/doc_props/core.rb b/lib/axlsx/doc_props/core.rb
index 72607fc6..96716a6b 100644
--- a/lib/axlsx/doc_props/core.rb
+++ b/lib/axlsx/doc_props/core.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The core object for the package.
# @note Packages manage their own core object.
diff --git a/lib/axlsx/drawing/axis.rb b/lib/axlsx/drawing/axis.rb
index a8bd3afa..ad550269 100644
--- a/lib/axlsx/drawing/axis.rb
+++ b/lib/axlsx/drawing/axis.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# the access class defines common properties and values for a chart axis.
class Axis
diff --git a/lib/axlsx/drawing/bar_3D_chart.rb b/lib/axlsx/drawing/bar_3D_chart.rb
index 9230a9b5..de179cb6 100644
--- a/lib/axlsx/drawing/bar_3D_chart.rb
+++ b/lib/axlsx/drawing/bar_3D_chart.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The Bar3DChart is a three dimentional barchart (who would have guessed?) that you can add to your worksheet.
diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb
index 5acbe55f..5be8fabc 100644
--- a/lib/axlsx/drawing/bar_series.rb
+++ b/lib/axlsx/drawing/bar_series.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A BarSeries defines the title, data and labels for bar charts
# @note The recommended way to manage series is to use Chart#add_series
diff --git a/lib/axlsx/drawing/cat_axis.rb b/lib/axlsx/drawing/cat_axis.rb
index 5490c5b4..ee408c56 100644
--- a/lib/axlsx/drawing/cat_axis.rb
+++ b/lib/axlsx/drawing/cat_axis.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
#A CatAxis object defines a chart category axis
class CatAxis < Axis
diff --git a/lib/axlsx/drawing/cat_axis_data.rb b/lib/axlsx/drawing/cat_axis_data.rb
index 28fe9750..b9b57d01 100644
--- a/lib/axlsx/drawing/cat_axis_data.rb
+++ b/lib/axlsx/drawing/cat_axis_data.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The CatAxisData class serializes the category axis data for a chart
class CatAxisData < SimpleTypedList
diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb
index 9fd94ea6..d3c26692 100644
--- a/lib/axlsx/drawing/chart.rb
+++ b/lib/axlsx/drawing/chart.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# A Chart is the superclass for specific charts
# @note Worksheet#add_chart is the recommended way to create charts for your worksheets.
diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb
index 0f8e0892..b9a31150 100644
--- a/lib/axlsx/drawing/drawing.rb
+++ b/lib/axlsx/drawing/drawing.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
require 'axlsx/drawing/title.rb'
require 'axlsx/drawing/series_title.rb'
diff --git a/lib/axlsx/drawing/graphic_frame.rb b/lib/axlsx/drawing/graphic_frame.rb
index ae877187..903ff1d6 100644
--- a/lib/axlsx/drawing/graphic_frame.rb
+++ b/lib/axlsx/drawing/graphic_frame.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A graphic frame defines a container for a chart object
# @note The recommended way to manage charts is Worksheet#add_chart
diff --git a/lib/axlsx/drawing/hyperlink.rb b/lib/axlsx/drawing/hyperlink.rb
index dfd1ddb2..67b08f29 100644
--- a/lib/axlsx/drawing/hyperlink.rb
+++ b/lib/axlsx/drawing/hyperlink.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# a hyperlink object adds an action to an image when clicked so that when the image is clicked the link is fecthed.
# @note using the hyperlink option when calling add_image on a drawing object is the recommended way to manage hyperlinks
diff --git a/lib/axlsx/drawing/line_3D_chart.rb b/lib/axlsx/drawing/line_3D_chart.rb
index 6dc3a0e1..13e42dff 100644
--- a/lib/axlsx/drawing/line_3D_chart.rb
+++ b/lib/axlsx/drawing/line_3D_chart.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The Line3DChart is a three dimentional line chart (who would have guessed?) that you can add to your worksheet.
diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb
index c56980fc..a7de888f 100644
--- a/lib/axlsx/drawing/line_series.rb
+++ b/lib/axlsx/drawing/line_series.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A LineSeries defines the title, data and labels for line charts
# @note The recommended way to manage series is to use Chart#add_series
diff --git a/lib/axlsx/drawing/marker.rb b/lib/axlsx/drawing/marker.rb
index 248b8b6e..dac8604e 100644
--- a/lib/axlsx/drawing/marker.rb
+++ b/lib/axlsx/drawing/marker.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The Marker class defines a point in the worksheet that drawing anchors attach to.
# @note The recommended way to manage markers is Worksheet#add_chart Markers are created for a two cell anchor based on the :start and :end options.
diff --git a/lib/axlsx/drawing/one_cell_anchor.rb b/lib/axlsx/drawing/one_cell_anchor.rb
index 1c7005fb..2cd43331 100644
--- a/lib/axlsx/drawing/one_cell_anchor.rb
+++ b/lib/axlsx/drawing/one_cell_anchor.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# This class details a single cell anchor for drawings.
# @note The recommended way to manage drawings, images and charts is Worksheet#add_chart or Worksheet#add_image.
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb
index f1939e2a..b922bcc4 100644
--- a/lib/axlsx/drawing/pic.rb
+++ b/lib/axlsx/drawing/pic.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# a Pic object represents an image in your worksheet
# Worksheet#add_image is the recommended way to manage images in your sheets
diff --git a/lib/axlsx/drawing/picture_locking.rb b/lib/axlsx/drawing/picture_locking.rb
index b1c4850e..c05f1641 100644
--- a/lib/axlsx/drawing/picture_locking.rb
+++ b/lib/axlsx/drawing/picture_locking.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The picture locking class defines the locking properties for pictures in your workbook.
class PictureLocking
diff --git a/lib/axlsx/drawing/pie_3D_chart.rb b/lib/axlsx/drawing/pie_3D_chart.rb
index 5f476a99..cb36e4f5 100644
--- a/lib/axlsx/drawing/pie_3D_chart.rb
+++ b/lib/axlsx/drawing/pie_3D_chart.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
diff --git a/lib/axlsx/drawing/pie_series.rb b/lib/axlsx/drawing/pie_series.rb
index ed9be40e..8e583806 100644
--- a/lib/axlsx/drawing/pie_series.rb
+++ b/lib/axlsx/drawing/pie_series.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A PieSeries defines the data and labels and explosion for pie charts series.
# @note The recommended way to manage series is to use Chart#add_series
diff --git a/lib/axlsx/drawing/scaling.rb b/lib/axlsx/drawing/scaling.rb
index 0f267676..628aa016 100644
--- a/lib/axlsx/drawing/scaling.rb
+++ b/lib/axlsx/drawing/scaling.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The Scaling class defines axis scaling
class Scaling
diff --git a/lib/axlsx/drawing/ser_axis.rb b/lib/axlsx/drawing/ser_axis.rb
index 680e4d2c..6fc5fec0 100644
--- a/lib/axlsx/drawing/ser_axis.rb
+++ b/lib/axlsx/drawing/ser_axis.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
#A SerAxis object defines a series axis
class SerAxis < Axis
diff --git a/lib/axlsx/drawing/series.rb b/lib/axlsx/drawing/series.rb
index 51e53e78..1544bc48 100644
--- a/lib/axlsx/drawing/series.rb
+++ b/lib/axlsx/drawing/series.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A Series defines the common series attributes and is the super class for all concrete series types.
# @note The recommended way to manage series is to use Chart#add_series
diff --git a/lib/axlsx/drawing/series_title.rb b/lib/axlsx/drawing/series_title.rb
index 50567579..b13cec89 100644
--- a/lib/axlsx/drawing/series_title.rb
+++ b/lib/axlsx/drawing/series_title.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A series title is a Title with a slightly different serialization than chart titles.
class SeriesTitle < Title
diff --git a/lib/axlsx/drawing/title.rb b/lib/axlsx/drawing/title.rb
index 59cee112..cc382205 100644
--- a/lib/axlsx/drawing/title.rb
+++ b/lib/axlsx/drawing/title.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A Title stores information about the title of a chart
class Title
diff --git a/lib/axlsx/drawing/two_cell_anchor.rb b/lib/axlsx/drawing/two_cell_anchor.rb
index 867e2584..436f2e7a 100644
--- a/lib/axlsx/drawing/two_cell_anchor.rb
+++ b/lib/axlsx/drawing/two_cell_anchor.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# This class details the anchor points for drawings.
# @note The recommended way to manage drawings and charts is Worksheet#add_chart. Anchors are specified by the :start_at and :end_at options to that method.
diff --git a/lib/axlsx/drawing/val_axis.rb b/lib/axlsx/drawing/val_axis.rb
index e442b263..c7091d49 100644
--- a/lib/axlsx/drawing/val_axis.rb
+++ b/lib/axlsx/drawing/val_axis.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# the ValAxis class defines a chart value axis.
class ValAxis < Axis
diff --git a/lib/axlsx/drawing/val_axis_data.rb b/lib/axlsx/drawing/val_axis_data.rb
index 17ab9a4e..6b950443 100644
--- a/lib/axlsx/drawing/val_axis_data.rb
+++ b/lib/axlsx/drawing/val_axis_data.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The ValAxisData class manages the values for a chart value series.
class ValAxisData < CatAxisData
diff --git a/lib/axlsx/drawing/view_3D.rb b/lib/axlsx/drawing/view_3D.rb
index 8f5cdc87..d8a569b1 100644
--- a/lib/axlsx/drawing/view_3D.rb
+++ b/lib/axlsx/drawing/view_3D.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# 3D attributes for a chart.
class View3D
diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb
index cf050d62..3d33d0e2 100644
--- a/lib/axlsx/package.rb
+++ b/lib/axlsx/package.rb
@@ -1,10 +1,10 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# Package is responsible for managing all the bits and peices that Open Office XML requires to make a valid
# xlsx document including valdation and serialization.
class Package
- # plain text password
+
# provides access to the app doc properties for this package
# see App
attr_reader :app
@@ -17,14 +17,31 @@ module Axlsx
#
# @param [Hash] options A hash that you can use to specify the author and workbook for this package.
# @option options [String] :author The author of the document
+ # @option options [Boolean] :use_shared_strings This is passed to the workbook to specify that shared strings should be used when serializing the package.
# @example Package.new :author => 'you!', :workbook => Workbook.new
def initialize(options={})
@workbook = nil
@core, @app = Core.new, App.new
@core.creator = options[:author] || @core.creator
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
yield self if block_given?
end
+ # Shortcut to specify that the workbook should use shared strings
+ # @see Workbook#use_shared_strings
+ def use_shared_strings=(v)
+ Axlsx::validate_boolean(v);
+ workbook.use_shared_strings = v
+ end
+
+ # Shortcut to determine if the workbook is configured to use shared strings
+ # @see Workbook#use_shared_strings
+ def use_shared_strings
+ workbook.use_shared_strings
+ end
+
# The workbook this package will serialize or validate.
# @return [Workbook] If no workbook instance has been assigned with this package a new Workbook instance is returned.
# @raise ArgumentError if workbook parameter is not a Workbook instance.
@@ -90,7 +107,9 @@ module Axlsx
end
# Encrypt the package into a CFB using the password provided
+ # This is not ready yet
def encrypt(file_name, password)
+ return false
moc = MsOffCrypto.new(file_name, password)
moc.save
end
@@ -164,6 +183,10 @@ module Axlsx
@parts << {:entry => "xl/#{image.pn}", :path => image.image_src}
end
+ if use_shared_strings
+ @parts << {:entry => "xl/#{SHARED_STRINGS_PN}", :doc => workbook.shared_strings.to_xml, :schema => SML_XSD}
+ end
+
workbook.worksheets.each do |sheet|
@parts << {:entry => "xl/#{sheet.rels_pn}", :doc => sheet.relationships.to_xml, :schema => RELS_XSD}
@parts << {:entry => "xl/#{sheet.pn}", :doc => sheet.to_xml, :schema => SML_XSD}
@@ -216,6 +239,10 @@ module Axlsx
end
c_types << Axlsx::Default.new(:ContentType => ct, :Extension => ext )
end
+ if use_shared_strings
+ c_types << Axlsx::Override.new(:PartName => "/xl/#{SHARED_STRINGS_PN}",
+ :ContentType => SHARED_STRINGS_CT)
+ end
c_types
end
diff --git a/lib/axlsx/rels/relationship.rb b/lib/axlsx/rels/relationship.rb
index 2cadf03b..23e6428a 100644
--- a/lib/axlsx/rels/relationship.rb
+++ b/lib/axlsx/rels/relationship.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A relationship defines a reference between package parts.
# @note Packages automatcially manage relationships.
diff --git a/lib/axlsx/rels/relationships.rb b/lib/axlsx/rels/relationships.rb
index 125082f4..8516bc69 100644
--- a/lib/axlsx/rels/relationships.rb
+++ b/lib/axlsx/rels/relationships.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
require 'axlsx/rels/relationship.rb'
diff --git a/lib/axlsx/stylesheet/border.rb b/lib/axlsx/stylesheet/border.rb
index 73a873e5..334ed8d3 100644
--- a/lib/axlsx/stylesheet/border.rb
+++ b/lib/axlsx/stylesheet/border.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# This class details a border used in Office Open XML spreadsheet styles.
class Border
diff --git a/lib/axlsx/stylesheet/border_pr.rb b/lib/axlsx/stylesheet/border_pr.rb
index cdab5b13..62fe3bab 100644
--- a/lib/axlsx/stylesheet/border_pr.rb
+++ b/lib/axlsx/stylesheet/border_pr.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A border part.
class BorderPr
diff --git a/lib/axlsx/stylesheet/cell_alignment.rb b/lib/axlsx/stylesheet/cell_alignment.rb
index 75d86e2d..0a1ce60a 100644
--- a/lib/axlsx/stylesheet/cell_alignment.rb
+++ b/lib/axlsx/stylesheet/cell_alignment.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# CellAlignment stores information about the cell alignment of a style Xf Object.
# @note Using Styles#add_style is the recommended way to manage cell alignment.
diff --git a/lib/axlsx/stylesheet/cell_protection.rb b/lib/axlsx/stylesheet/cell_protection.rb
index e1c6183f..89d85289 100644
--- a/lib/axlsx/stylesheet/cell_protection.rb
+++ b/lib/axlsx/stylesheet/cell_protection.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# CellProtection stores information about locking or hiding cells in spreadsheet.
# @note Using Styles#add_style is the recommended way to manage cell protection.
diff --git a/lib/axlsx/stylesheet/cell_style.rb b/lib/axlsx/stylesheet/cell_style.rb
index f11f5193..0a40ab13 100644
--- a/lib/axlsx/stylesheet/cell_style.rb
+++ b/lib/axlsx/stylesheet/cell_style.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# CellStyle defines named styles that reference defined formatting records and can be used in your worksheet.
# @note Using Styles#add_style is the recommended way to manage cell styling.
diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb
index 60d4c229..4e9806ad 100644
--- a/lib/axlsx/stylesheet/color.rb
+++ b/lib/axlsx/stylesheet/color.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The color class represents a color used for borders, fills an fonts
class Color
diff --git a/lib/axlsx/stylesheet/fill.rb b/lib/axlsx/stylesheet/fill.rb
index ad313679..4dccaa8a 100644
--- a/lib/axlsx/stylesheet/fill.rb
+++ b/lib/axlsx/stylesheet/fill.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The Fill is a formatting object that manages the background color, and pattern for cells.
# @note The recommended way to manage styles in your workbook is to use Styles#add_style.
diff --git a/lib/axlsx/stylesheet/font.rb b/lib/axlsx/stylesheet/font.rb
index b0c9eaae..d3eea3b2 100644
--- a/lib/axlsx/stylesheet/font.rb
+++ b/lib/axlsx/stylesheet/font.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The Font class details a font instance for use in styling cells.
# @note The recommended way to manage fonts, and other styles is Styles#add_style
diff --git a/lib/axlsx/stylesheet/gradient_fill.rb b/lib/axlsx/stylesheet/gradient_fill.rb
index 2b02ac76..6090b570 100644
--- a/lib/axlsx/stylesheet/gradient_fill.rb
+++ b/lib/axlsx/stylesheet/gradient_fill.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# A GradientFill defines the color and positioning for gradiant cell fill.
# @see Open Office XML Part 1 §18.8.24
diff --git a/lib/axlsx/stylesheet/gradient_stop.rb b/lib/axlsx/stylesheet/gradient_stop.rb
index d493c037..a05e6d33 100644
--- a/lib/axlsx/stylesheet/gradient_stop.rb
+++ b/lib/axlsx/stylesheet/gradient_stop.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# The GradientStop object represents a color point in a gradient.
# @see Open Office XML Part 1 §18.8.24
diff --git a/lib/axlsx/stylesheet/num_fmt.rb b/lib/axlsx/stylesheet/num_fmt.rb
index 0ed45e5c..261d5bb3 100644
--- a/lib/axlsx/stylesheet/num_fmt.rb
+++ b/lib/axlsx/stylesheet/num_fmt.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A NumFmt object defines an identifier and formatting code for data in cells.
# @note The recommended way to manage styles is Styles#add_style
diff --git a/lib/axlsx/stylesheet/pattern_fill.rb b/lib/axlsx/stylesheet/pattern_fill.rb
index a6a6f6a0..79910ede 100644
--- a/lib/axlsx/stylesheet/pattern_fill.rb
+++ b/lib/axlsx/stylesheet/pattern_fill.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A PatternFill is the pattern and solid fill styling for a cell.
# @note The recommended way to manage styles is with Styles#add_style
diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb
index 2ec568f0..7c2913e5 100644
--- a/lib/axlsx/stylesheet/styles.rb
+++ b/lib/axlsx/stylesheet/styles.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
require 'axlsx/stylesheet/border.rb'
require 'axlsx/stylesheet/border_pr.rb'
diff --git a/lib/axlsx/stylesheet/table_style.rb b/lib/axlsx/stylesheet/table_style.rb
index 77fbd448..4397cffb 100644
--- a/lib/axlsx/stylesheet/table_style.rb
+++ b/lib/axlsx/stylesheet/table_style.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A single table style definition and is a collection for tableStyleElements
# @note Table are not supported in this version and only the defaults required for a valid workbook are created.
diff --git a/lib/axlsx/stylesheet/table_style_element.rb b/lib/axlsx/stylesheet/table_style_element.rb
index 20159788..a8901f0b 100644
--- a/lib/axlsx/stylesheet/table_style_element.rb
+++ b/lib/axlsx/stylesheet/table_style_element.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# an element of style that belongs to a table style.
# @note tables and table styles are not supported in this version. This class exists in preparation for that support.
diff --git a/lib/axlsx/stylesheet/table_styles.rb b/lib/axlsx/stylesheet/table_styles.rb
index bfd15261..f0e9c904 100644
--- a/lib/axlsx/stylesheet/table_styles.rb
+++ b/lib/axlsx/stylesheet/table_styles.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# TableStyles represents a collection of style definitions for table styles and pivot table styles.
# @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.
diff --git a/lib/axlsx/stylesheet/xf.rb b/lib/axlsx/stylesheet/xf.rb
index 3ca1a8a2..7b55336b 100644
--- a/lib/axlsx/stylesheet/xf.rb
+++ b/lib/axlsx/stylesheet/xf.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# 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
# @see Styles#add_style
diff --git a/lib/axlsx/util/cbf.rb b/lib/axlsx/util/cbf.rb
index cff58f9d..f8fc057b 100644
--- a/lib/axlsx/util/cbf.rb
+++ b/lib/axlsx/util/cbf.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# 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.
diff --git a/lib/axlsx/util/constants.rb b/lib/axlsx/util/constants.rb
index c7d19f95..b2cbad26 100644
--- a/lib/axlsx/util/constants.rb
+++ b/lib/axlsx/util/constants.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# XML Encoding
@@ -64,6 +65,9 @@ module Axlsx
# styles rels namespace
STYLES_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
+ # shared strings namespace
+ SHARED_STRINGS_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
+
# drawing rels namespace
DRAWING_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
@@ -138,6 +142,9 @@ module Axlsx
# styles part
STYLES_PN = "styles.xml"
+
+ # shared_strings part
+ SHARED_STRINGS_PN = "sharedStrings.xml"
# app part
APP_PN = "docProps/app.xml"
diff --git a/lib/axlsx/util/ms_off_crypto.rb b/lib/axlsx/util/ms_off_crypto.rb
index 32bbf53f..e91bb20f 100644
--- a/lib/axlsx/util/ms_off_crypto.rb
+++ b/lib/axlsx/util/ms_off_crypto.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
require 'digest'
require 'base64'
require 'openssl'
diff --git a/lib/axlsx/util/parser.rb b/lib/axlsx/util/parser.rb
index 049c2d43..7719a6d1 100644
--- a/lib/axlsx/util/parser.rb
+++ b/lib/axlsx/util/parser.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The Parser module mixes in a number of methods to help in generating a model from xml
# This module is not included in the axlsx library at this time. It is for future development only,
diff --git a/lib/axlsx/util/simple_typed_list.rb b/lib/axlsx/util/simple_typed_list.rb
index 21640b3b..5a5addfa 100644
--- a/lib/axlsx/util/simple_typed_list.rb
+++ b/lib/axlsx/util/simple_typed_list.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A SimpleTypedList is a type restrictive collection that allows some of the methods from Array and supports basic xml serialization.
# @private
diff --git a/lib/axlsx/util/storage.rb b/lib/axlsx/util/storage.rb
index eeda73e0..9f8afb7f 100644
--- a/lib/axlsx/util/storage.rb
+++ b/lib/axlsx/util/storage.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The Storage class represents a storage object or stream in a compound file.
diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb
index 3d72794a..9accd3db 100644
--- a/lib/axlsx/util/validators.rb
+++ b/lib/axlsx/util/validators.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# Validate a value against a specific list of allowed values.
class RestrictionValidator
@@ -116,10 +117,10 @@ module Axlsx
end
# Requires that the value is a valid relationship_type
- # XML_NS_R, TABLE_R, WORKBOOK_R, WORKSHEET_R, APP_R, RELS_R, CORE_R, STYLES_R, CHART_R, DRAWING_R are allowed
+ # 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
# @param [Any] v The value validated
def self.validate_relationship_type(v)
- 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
+ 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
end
# Requires that the value is a valid table element type
diff --git a/lib/axlsx/version.rb b/lib/axlsx/version.rb
index dc751e3f..15656867 100644
--- a/lib/axlsx/version.rb
+++ b/lib/axlsx/version.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# The version of the gem
diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb
new file mode 100644
index 00000000..afd8498c
--- /dev/null
+++ b/lib/axlsx/workbook/shared_strings_table.rb
@@ -0,0 +1,72 @@
+# encoding: UTF-8
+module Axlsx
+
+ # The Shared String Table class is responsible for managing and serializing common strings in a workbook.
+ # While the ECMA-376 spec allows for both inline and shared strings it seems that at least some applications like Numbers (Mac)
+ # and Google Docs require that the shared string table is populated in order to interoperate properly.
+ # As a developer, you should never need to directly work against this class. Simply set 'use_shared_strings'
+ # on the package or workbook to generate a package that uses the shared strings table instead of inline strings.
+ # @note Serialization performance is affected by using this serialization method so if you do not need interoperability
+ # it is recomended that you use the default inline string method of serialization.
+ class SharedStringsTable
+
+ # The total number of strings in the workbook including duplicates
+ # Empty cells are treated as blank strings
+ # @return [Integer]
+ attr_reader :count
+
+ # The total number of unique strings in the workbook.
+ # @return [Integer]
+ def unique_count
+ @unique_cells.size
+ end
+
+ # An array of unique cells. Multiple attributes of the cell are used in comparison
+ # each of these unique cells is parsed into the shared string table.
+ # @see Cell#sharable
+ attr_reader :unique_cells
+
+ # Creates a new Shared Strings Table agains an array of cells
+ # @param [Array] cells This is an array of all of the cells in the workbook
+ def initialize(cells)
+ cells = cells.flatten.reject { |c| c.type != :string }
+ @count = cells.size
+ @unique_cells = []
+ resolve cells
+ end
+
+
+ # Generate the xml document for the Shared Strings Table
+ # @return [String]
+ def to_xml
+ builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
+ xml.sst(:xmlns => Axlsx::XML_NS, :count => count, :uniqueCount => unique_count) {
+ @unique_cells.each do |cell|
+ xml.si { xml.t cell.value.to_s }
+ end
+ }
+ end
+ builder.to_xml(:save_with => 0)
+ end
+ end
+
+ private
+
+ # Interate over all of the cells in the array.
+ # if our unique cells array does not contain a sharable cell,
+ # add the cell to our unique cells array and set the ssti attribute on the index of this cell in the shared strings table
+ # if a sharable cell already exists in our unique_cells array, set the ssti attribute of the cell and move on.
+ # @return [Array] unique cells
+ def resolve(cells)
+ cells.each do |cell|
+ index = @unique_cells.index { |item| item.shareable(cell) }
+ if index == nil
+ cell.send :ssti=, @unique_cells.size
+ @unique_cells << cell
+ else
+ cell.send :ssti=, index
+ end
+ end
+ end
+
+end
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb
index c74e74a8..b9db0a45 100644
--- a/lib/axlsx/workbook/workbook.rb
+++ b/lib/axlsx/workbook/workbook.rb
@@ -4,6 +4,7 @@ module Axlsx
require 'axlsx/workbook/worksheet/cell.rb'
require 'axlsx/workbook/worksheet/row.rb'
require 'axlsx/workbook/worksheet/worksheet.rb'
+require 'axlsx/workbook/shared_strings_table.rb'
# The Workbook class is an xlsx workbook that manages worksheets, charts, drawings and styles.
# 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'
# *workbookPr is only supported to the extend of date1904
class Workbook
+ # When true, the Package will be generated with a shared string table. This may be required by some OOXML processors that do not
+ # adhere to the ECMA specification that dictates string may be inline in the sheet.
+ # Using this option will increase the time required to serialize the document as every string in every cell must be analzed and referenced.
+ # @return [Boolean]
+ attr_reader :use_shared_strings
+
+ # @see use_shared_strings
+ def use_shared_strings=(v)
+ Axlsx::validate_boolean(v)
+ @use_shared_strings = v
+ end
+
+
# A collection of worksheets associated with this workbook.
# @note The recommended way to manage worksheets is add_worksheet
# @see Workbook#add_worksheet
@@ -84,6 +98,7 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
# Creates a new Workbook
#
# @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.
+ # @option
#
def initialize(options={})
@styles = Styles.new
@@ -136,9 +151,18 @@ require 'axlsx/workbook/worksheet/worksheet.rb'
r << Relationship.new(WORKSHEET_R, WORKSHEET_PN % (r.size+1))
end
r << Relationship.new(STYLES_R, STYLES_PN)
+ if use_shared_strings
+ r << Relationship.new(SHARED_STRINGS_R, SHARED_STRINGS_PN)
+ end
r
end
+ # generates a shared string object against all cells in all worksheets.
+ # @return [SharedStringTable]
+ def shared_strings
+ SharedStringsTable.new(worksheets.collect { |ws| ws.cells })
+ end
+
# returns a range of cells in a worksheet
# @param [String] cell_def The excel style reference defining the worksheet and cells. The range must specify the sheet to
# 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.
diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb
index 64f6e6ad..8b20af94 100644
--- a/lib/axlsx/workbook/worksheet/cell.rb
+++ b/lib/axlsx/workbook/worksheet/cell.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# A cell in a worksheet.
# Cell stores inforamation requried to serialize a single worksheet cell to xml. You must provde the Row that the cell belongs to and the cells value. The data type will automatically be determed if you do not specify the :type option. The default style will be applied if you do not supply the :style option. Changing the cell's type will recast the value to the type specified. Altering the cell's value via the property accessor will also automatically cast the provided value to the cell's type.
@@ -23,6 +23,12 @@ module Axlsx
# @see Worksheet#add_row
class Cell
+ # The index of this string in the shared string table
+ # This is only set when the use_shared_strings option on the package is true.
+ # @return [Integer]
+ # @see Package#use_shared_strings
+ attr_accessor :shared_string_index
+
# The index of the cellXfs item to be applied to this cell.
# @return [Integer]
# @see Axlsx::Styles
@@ -186,6 +192,26 @@ module Axlsx
@value = cast_value(value)
end
+ # The Shared Strings Table index for this cell
+ attr_reader :ssti
+
+ # equality comparison to test value, type and inline style attributes
+ # this is how we work out if the cell needs to be added or already exists in the shared strings table
+ def shareable(v)
+ comparable_keys = [:value, :type, :font_name, :charset,
+ :family, :b, :i, :strike,:outline,
+ :shadow, :condense, :extend, :u,
+ :vertAlign, :sz, :color, :scheme]
+ v_hash = v.instance_values.select { |k, v| comparable_keys.include? k.to_sym }
+
+ self_hash = self.instance_values.select { |k, v| comparable_keys.include? k.to_sym }
+ # required as color is an object, and the comparison will fail even though both use the same color.
+ v_hash['color'] = v_hash['color'].instance_values if v_hash['color']
+ self_hash['color'] = self_hash['color'].instance_values if self_hash['color']
+
+ v_hash == self_hash
+ end
+
# @return [Integer] The index of the cell in the containing row.
def index
@row.cells.index(self)
@@ -250,33 +276,39 @@ module Axlsx
#xml.c(:r => r, :t=>:inlineStr, :s=>style) {
# xml.is { xml.t @value.to_s }
#}
- #parse styled string
- xml.c(:r => r, :s=>style, :t => :inlineStr) {
- xml.is {
- xml.r {
- xml.rPr {
- xml.rFont(:val=>@font_name) if @font_name
- xml.charset(:val=>@charset) if @charset
- xml.family(:val=>@family) if @family
- xml.b(:val=>@b) if @b
- xml.i(:val=>@i) if @i
- xml.strike(:val=>@strike) if @strike
- xml.outline(:val=>@outline) if @outline
- xml.shadow(:val=>@shadow) if @shadow
- xml.condense(:val=>@condense) if @condense
- xml.extend(:val=>@extend) if @extend
- @color.to_xml(xml) if @color
- xml.sz(:val=>@sz) if @sz
- xml.u(:val=>@u) if @u
- # :baseline, :subscript, :superscript
- xml.vertAlign(:val=>@vertAlign) if @verAlign
- # :none, major, :minor
- xml.scheme(:val=>@scheme) if @scheme
+ if @ssti
+ xml.c(:r => r, :s=>style, :t => :s) {
+ xml.v ssti
+ }
+ else
+ #parse inline string
+ xml.c(:r => r, :s=>style, :t => :inlineStr) {
+ xml.is {
+ xml.r {
+ xml.rPr {
+ xml.rFont(:val=>@font_name) if @font_name
+ xml.charset(:val=>@charset) if @charset
+ xml.family(:val=>@family) if @family
+ xml.b(:val=>@b) if @b
+ xml.i(:val=>@i) if @i
+ xml.strike(:val=>@strike) if @strike
+ xml.outline(:val=>@outline) if @outline
+ xml.shadow(:val=>@shadow) if @shadow
+ xml.condense(:val=>@condense) if @condense
+ xml.extend(:val=>@extend) if @extend
+ @color.to_xml(xml) if @color
+ xml.sz(:val=>@sz) if @sz
+ xml.u(:val=>@u) if @u
+ # :baseline, :subscript, :superscript
+ xml.vertAlign(:val=>@vertAlign) if @verAlign
+ # :none, major, :minor
+ xml.scheme(:val=>@scheme) if @scheme
+ }
+ xml.t @value.to_s
}
- xml.t @value.to_s
}
}
- }
+ end
end
elsif @type == :time
# Using hardcoded offsets here as some operating systems will not except a 'negative' offset from the ruby epoc.
@@ -294,6 +326,12 @@ module Axlsx
private
+ # @see ssti
+ def ssti=(v)
+ Axlsx::validate_unsigned_int(v)
+ @ssti = v
+ end
+
# assigns the owning row for this cell.
def row=(v) DataTypeValidator.validate "Cell.row", Row, v; @row=v end
diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb
index f5528f8b..61bd4d8f 100644
--- a/lib/axlsx/workbook/worksheet/row.rb
+++ b/lib/axlsx/workbook/worksheet/row.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
module Axlsx
# A Row is a single row in a worksheet.
# @note The recommended way to manage rows and cells is to use Worksheet#add_row
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 5256ceb9..29bea8d3 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -1,4 +1,4 @@
-# -*- coding: utf-8 -*-
+# encoding: UTF-8
module Axlsx
# The Worksheet class represents a worksheet in the workbook.
@@ -51,6 +51,12 @@ module Axlsx
@merged_cells = []
end
+ # convinience method to access all cells in this worksheet
+ # @return [Array] cells
+ def cells
+ rows.flatten
+ end
+
# Creates merge information for this worksheet.
# Cells can be merged by calling the merge_cells method on a worksheet.
# @example This would merge the three cells C1..E1 #
@@ -70,6 +76,14 @@ module Axlsx
end
+ # The demensions of a worksheet. This is not actually a required element by the spec,
+ # but at least a few other document readers expect this for conversion
+ # @return [String] the A1:B2 style reference for the first and last row column intersection in the workbook
+ def dimension
+ "#{rows.first.cells.first.r}:#{rows.last.cells.last.r}"
+ end
+
+
# Returns the cell or cells defined using excel style A1:B3 references.
# @param [String] cell_def the string defining the cell or range of cells
# @return [Cell, Array]
@@ -238,6 +252,7 @@ module Axlsx
builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
xml.worksheet(:xmlns => XML_NS,
:'xmlns:r' => XML_NS_R) {
+ xml.dimension :ref=>dimension unless rows.size == 0
if @auto_fit_data.size > 0
xml.cols {
@auto_fit_data.each_with_index do |col, index|
diff --git a/test/content_type/tc_content_type.rb b/test/content_type/tc_content_type.rb
index 62ea05e8..9fcbcd70 100644
--- a/test/content_type/tc_content_type.rb
+++ b/test/content_type/tc_content_type.rb
@@ -1,3 +1,5 @@
+# encoding: UTF-8
+
require 'test/unit'
require 'axlsx.rb'
diff --git a/test/content_type/tc_default.rb b/test/content_type/tc_default.rb
index ce024de8..90c0934e 100644
--- a/test/content_type/tc_default.rb
+++ b/test/content_type/tc_default.rb
@@ -1,3 +1,5 @@
+# encoding: UTF-8
+
require 'test/unit'
require 'axlsx.rb'
diff --git a/test/content_type/tc_override.rb b/test/content_type/tc_override.rb
index 3f478a3f..911ec649 100644
--- a/test/content_type/tc_override.rb
+++ b/test/content_type/tc_override.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
require 'test/unit'
require 'axlsx.rb'
diff --git a/test/tc_package.rb b/test/tc_package.rb
index e831f52a..90c3cb84 100644
--- a/test/tc_package.rb
+++ b/test/tc_package.rb
@@ -21,6 +21,13 @@ class TestPackage < Test::Unit::TestCase
assert_raise(NoMethodError) {@package.app = nil }
end
+ def test_use_shared_strings
+ assert_equal(@package.use_shared_strings, nil)
+ assert_raise(ArgumentError) {@package.use_shared_strings 9}
+ assert_nothing_raised {@package.use_shared_strings = true}
+ assert_equal(@package.use_shared_strings, @package.workbook.use_shared_strings)
+ end
+
def test_default_objects_are_created
assert(@package.instance_values["app"].is_a?(Axlsx::App), 'App object not created')
assert(@package.instance_values["core"].is_a?(Axlsx::Core), 'Core object not created')
@@ -69,10 +76,34 @@ class TestPackage < Test::Unit::TestCase
#no mystery parts
assert_equal(p.size, 12)
+
+ end
+ def test_shared_strings_requires_part
+ @package.use_shared_strings = true
+ p = @package.send(:parts)
+ assert_equal(p.select{ |part| part[:entry] =~/xl\/sharedStrings.xml/}.size, 1, "shared strings table missing")
end
def test_workbook_is_a_workbook
assert @package.workbook.is_a? Axlsx::Workbook
end
+
+ def test_base_content_types
+ ct = @package.send(:base_content_types)
+ assert(ct.select { |ct| ct.ContentType == Axlsx::RELS_CT }.size == 1, "rels content type missing")
+ assert(ct.select { |ct| ct.ContentType == Axlsx::XML_CT }.size == 1, "xml content type missing")
+ assert(ct.select { |ct| ct.ContentType == Axlsx::APP_CT }.size == 1, "app content type missing")
+ assert(ct.select { |ct| ct.ContentType == Axlsx::CORE_CT }.size == 1, "core content type missing")
+ assert(ct.select { |ct| ct.ContentType == Axlsx::STYLES_CT }.size == 1, "styles content type missing")
+ assert(ct.select { |ct| ct.ContentType == Axlsx::WORKBOOK_CT }.size == 1, "workbook content type missing")
+ assert(ct.size == 6)
+ end
+
+ def test_content_type_added_with_shared_strings
+ @package.use_shared_strings = true
+ ct = @package.send(:content_types)
+ assert(ct.select { |ct| ct.ContentType == Axlsx::SHARED_STRINGS_CT }.size == 1)
+ end
+
end
diff --git a/test/workbook/tc_shared_strings_table.rb b/test/workbook/tc_shared_strings_table.rb
new file mode 100644
index 00000000..b0e9de05
--- /dev/null
+++ b/test/workbook/tc_shared_strings_table.rb
@@ -0,0 +1,39 @@
+require 'test/unit'
+require 'axlsx.rb'
+
+class TestSharedStringsTable < Test::Unit::TestCase
+
+ def setup
+ @p = Axlsx::Package.new :use_shared_strings=>true
+ ws = @p.workbook.add_worksheet
+ ws.add_row ['a', 1, 'b']
+ ws.add_row ['b', 1, 'c']
+ ws.add_row ['c', 1, 'd']
+ end
+
+ def test_workbook_has_shared_strings
+ assert(@p.workbook.shared_strings.is_a?(Axlsx::SharedStringsTable), "shared string table was not created")
+ end
+
+ def test_count
+ sst = @p.workbook.shared_strings
+ assert_equal(sst.count, 6)
+ end
+
+ def test_unique_count
+ sst = @p.workbook.shared_strings
+ assert_equal(sst.unique_count, 4)
+ end
+
+ def test_valid_document
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
+ doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml)
+ errors = []
+ schema.validate(doc).each do |error|
+ puts error.message
+ errors << error
+ end
+ assert_equal(errors.size, 0, "sharedStirngs.xml Invalid" + errors.map{ |e| e.message }.to_s)
+ end
+
+end
diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb
index 7f5139cb..e068a6d0 100644
--- a/test/workbook/tc_workbook.rb
+++ b/test/workbook/tc_workbook.rb
@@ -18,6 +18,12 @@ class TestWorkbook < Test::Unit::TestCase
assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
end
+ def test_shared_strings
+ assert_equal(@wb.use_shared_strings, nil)
+ assert_raise(ArgumentError) {@wb.use_shared_strings = 'bpb'}
+ assert_nothing_raised {@wb.use_shared_strings = :true}
+ end
+
def test_add_worksheet
assert(@wb.worksheets.empty?, "worbook has no worksheets by default")
ws = @wb.add_worksheet(:name=>"bob")
@@ -30,6 +36,8 @@ class TestWorkbook < Test::Unit::TestCase
assert(@wb.relationships.size == 1)
@wb.add_worksheet
assert(@wb.relationships.size == 2)
+ @wb.use_shared_strings = true
+ assert(@wb.relationships.size == 3)
end
def test_to_xml
diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb
index 2859ffec..ed781e0f 100644
--- a/test/workbook/worksheet/tc_cell.rb
+++ b/test/workbook/worksheet/tc_cell.rb
@@ -18,6 +18,7 @@ class TestCell < Test::Unit::TestCase
assert_equal(@c.value, 1.0, "type option is applied and value is casted")
end
+
def test_style_date_data
c = Axlsx::Cell.new(@c.row, Time.now)
assert_equal(Axlsx::STYLE_DATE, c.style)
@@ -191,4 +192,16 @@ class TestCell < Test::Unit::TestCase
assert_equal(@c.row.worksheet.merged_cells.last, "A1:C1")
end
+ def test_equality
+ c2 = @row.add_cell 1, :type=>:float, :style=>1
+ assert(c2.shareable(@c))
+ c3 = @row.add_cell 2, :type=>:float, :style=>1
+ c4 = @row.add_cell 1, :type=>:float, :style=>1, :color => "#FFFFFFFF"
+ assert_equal(c4.shareable(c2) ,false)
+ c5 = @row.add_cell 1, :type=>:float, :style=>1, :color => "#FFFFFFFF"
+ assert(c5.shareable(c4))
+
+ end
+
+
end
diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb
index 24e26155..b7f93d97 100644
--- a/test/workbook/worksheet/tc_worksheet.rb
+++ b/test/workbook/worksheet/tc_worksheet.rb
@@ -28,7 +28,13 @@ class TestWorksheet < Test::Unit::TestCase
def test_index
assert_equal(@ws.index, @ws.workbook.worksheets.index(@ws))
end
-
+
+ def test_dimension
+ @ws.add_row [1, 2, 3]
+ @ws.add_row [4, 5, 6]
+ assert_equal @ws.dimension, "A1:C2"
+ end
+
def test_referencing
@ws.add_row [1, 2, 3]
@ws.add_row [4, 5, 6]