diff options
| author | Randy Morgan <[email protected]> | 2012-07-10 20:27:42 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-07-10 20:27:42 +0900 |
| commit | e82f217d5d94d5e07c195323b61f9e3195ac6634 (patch) | |
| tree | 1cfccdba9f632722ee3464742ee5083923845e44 | |
| parent | 8f0e868940fe7fd56e2c43e2b1102ec1d3e58a0e (diff) | |
| download | caxlsx-e82f217d5d94d5e07c195323b61f9e3195ac6634.tar.gz caxlsx-e82f217d5d94d5e07c195323b61f9e3195ac6634.zip | |
more cleanup
| -rw-r--r-- | lib/axlsx/content_type/content_type.rb | 1 | ||||
| -rw-r--r-- | lib/axlsx/content_type/default.rb | 5 | ||||
| -rw-r--r-- | lib/axlsx/content_type/override.rb | 33 | ||||
| -rw-r--r-- | lib/axlsx/doc_props/app.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/doc_props/core.rb | 13 | ||||
| -rw-r--r-- | lib/axlsx/drawing/ax_data_source.rb | 13 | ||||
| -rw-r--r-- | lib/axlsx/drawing/axis.rb | 63 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 159 |
8 files changed, 159 insertions, 134 deletions
diff --git a/lib/axlsx/content_type/content_type.rb b/lib/axlsx/content_type/content_type.rb index 6fa6f944..1d528097 100644 --- a/lib/axlsx/content_type/content_type.rb +++ b/lib/axlsx/content_type/content_type.rb @@ -22,4 +22,5 @@ module Axlsx end end + end diff --git a/lib/axlsx/content_type/default.rb b/lib/axlsx/content_type/default.rb index 8baafb77..23406018 100644 --- a/lib/axlsx/content_type/default.rb +++ b/lib/axlsx/content_type/default.rb @@ -15,8 +15,9 @@ module Axlsx end end + # Error string for option validation INVALID_ARGUMENTS = "extension and content_type are required" - # + # The extension of the content type. # @return [String] attr_reader :extension @@ -49,5 +50,7 @@ module Axlsx def validate_options(options) (options[:Extension] || options[:extension]) && (options[:content_type] || options[:ContentType]) end + end + end diff --git a/lib/axlsx/content_type/override.rb b/lib/axlsx/content_type/override.rb index d3911a6f..d3768ccd 100644 --- a/lib/axlsx/content_type/override.rb +++ b/lib/axlsx/content_type/override.rb @@ -1,8 +1,23 @@ # 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 + #Creates a new Override object + # @option options [String] PartName + # @option options [String] ContentType + # @raise [ArgumentError] An argument error is raised if both PartName and ContentType are not specified. + def initialize(options={}) + raise ArgumentError, INVALID_ARGUMENTS unless validate_options(options) + options.each do |name, value| + self.send("#{name}=", value) if self.respond_to? "#{name}=" + end + end + + # Error message for invalid options + INVALID_ARGUMENTS = 'part_name and content_type are required' + # The type of content. # @return [String] attr_reader :content_type @@ -13,17 +28,6 @@ module Axlsx attr_reader :part_name alias :PartName :part_name - #Creates a new Override object - # @option options [String] PartName - # @option options [String] ContentType - # @raise [ArgumentError] An argument error is raised if both PartName and ContentType are not specified. - def initialize(options={}) - raise ArgumentError, "PartName and ContentType are required" unless (options[:PartName] || options[:part_name]) && (options[:ContentType] || options[:content_type]) - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end - # The name and location of the part. def part_name=(v) Axlsx::validate_string v; @part_name = v end alias :PartName= :part_name= @@ -42,5 +46,12 @@ module Axlsx str << '/>' end + private + + def validate_options(options) + (options[:PartName] || options[:part_name]) && (options[:ContentType] || options[:content_type]) + end + end + end diff --git a/lib/axlsx/doc_props/app.rb b/lib/axlsx/doc_props/app.rb index 52891e28..39f86eb4 100644 --- a/lib/axlsx/doc_props/app.rb +++ b/lib/axlsx/doc_props/app.rb @@ -1,5 +1,6 @@ # 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 # @note Support is not implemented for the following complex types: @@ -118,7 +119,7 @@ module Axlsx # @return [Boolean] Indicates that the hyper links in the document have been changed. attr_reader :hyperlinks_changed alias :HyperlinksChanged :hyperlinks_changed - + # @return [String] The name of the application attr_reader :application alias :Applicatoin :application @@ -216,7 +217,7 @@ module Axlsx # Sets the doc_security property of your app.xml file def doc_security=(v) Axlsx::validate_int v; @doc_security = v; end alias :DocSecurity= :doc_security= - + # Serialize the app.xml document # @return [String] def to_xml_string(str = '') @@ -227,4 +228,5 @@ module Axlsx end end + end diff --git a/lib/axlsx/doc_props/core.rb b/lib/axlsx/doc_props/core.rb index 0b62aa7c..d71300e0 100644 --- a/lib/axlsx/doc_props/core.rb +++ b/lib/axlsx/doc_props/core.rb @@ -1,20 +1,21 @@ # encoding: UTF-8 module Axlsx + # The core object for the package. # @note Packages manage their own core object. # @see Package#core class Core - # - # The author of the document. By default this is 'axlsx' - # @return [String] - attr_accessor :creator - + # Creates a new Core object. # @option options [String] creator def initialize(options={}) @creator = options[:creator] || 'axlsx' end + # The author of the document. By default this is 'axlsx' + # @return [String] + attr_accessor :creator + # serializes the core.xml document # @return [String] def to_xml_string(str = '') @@ -27,5 +28,7 @@ module Axlsx str << '<cp:revision>0</cp:revision>' str << '</cp:coreProperties>' end + end + end diff --git a/lib/axlsx/drawing/ax_data_source.rb b/lib/axlsx/drawing/ax_data_source.rb index 5bbfd55b..ebbfe2cd 100644 --- a/lib/axlsx/drawing/ax_data_source.rb +++ b/lib/axlsx/drawing/ax_data_source.rb @@ -1,14 +1,9 @@ module Axlsx + # An axis data source that can contain referenced or literal strings or numbers # @note only string data types are supported - mainly because we have not implemented a chart type that requires a numerical axis value class AxDataSource < NumDataSource - # allowed element tag names - # @return [Array] - def self.allowed_tag_names - [:xVal, :cat] - end - # creates a new NumDataSource object # @option options [Array] data An array of Cells or Numeric objects # @option options [Symbol] tag_name see tag_name @@ -19,6 +14,12 @@ module Axlsx super(options) end + # allowed element tag names for serialization + # @return [Array] + def self.allowed_tag_names + [:xVal, :cat] + end + end end diff --git a/lib/axlsx/drawing/axis.rb b/lib/axlsx/drawing/axis.rb index 5706f4ad..e13426cd 100644 --- a/lib/axlsx/drawing/axis.rb +++ b/lib/axlsx/drawing/axis.rb @@ -1,9 +1,34 @@ # encoding: UTF-8 module Axlsx - # the access class defines common properties and values for a chart axis. - class Axis + # the access class defines common properties and values for a chart axis. + class Axis + # Creates an Axis object + # @param [Integer] ax_id the id of this axis + # @param [Integer] cross_ax the id of the perpendicular axis + # @option options [Symbol] ax_pos + # @option options [Symbol] crosses + # @option options [Symbol] tick_lbl_pos + # @raise [ArgumentError] If axi_id or cross_ax are not unsigned integers + def initialize(ax_id, cross_ax, options={}) + Axlsx::validate_unsigned_int(ax_id) + Axlsx::validate_unsigned_int(cross_ax) + @ax_id = ax_id + @cross_ax = cross_ax + @format_code = "General" + @delete = @label_rotation = 0 + @scaling = Scaling.new(:orientation=>:minMax) + @title = @color = nil + self.ax_pos = :b + self.tick_lbl_pos = :nextTo + self.format_code = "General" + self.crosses = :autoZero + self.gridlines = true + options.each do |name, value| + self.send("#{name}=", value) if self.respond_to? "#{name}=" + end + end # the fill color to use in the axis shape properties. This should be a 6 character long hex string # e.g. FF0000 for red @@ -62,35 +87,14 @@ module Axlsx # the title for the axis. This can be a cell or a fixed string. attr_reader :title - # Creates an Axis object - # @param [Integer] ax_id the id of this axis - # @param [Integer] cross_ax the id of the perpendicular axis - # @option options [Symbol] ax_pos - # @option options [Symbol] crosses - # @option options [Symbol] tick_lbl_pos - # @raise [ArgumentError] If axi_id or cross_ax are not unsigned integers - def initialize(ax_id, cross_ax, options={}) - Axlsx::validate_unsigned_int(ax_id) - Axlsx::validate_unsigned_int(cross_ax) - @ax_id = ax_id - @cross_ax = cross_ax - @format_code = "General" - @delete = @label_rotation = 0 - @scaling = Scaling.new(:orientation=>:minMax) - @title = @color = nil - self.ax_pos = :b - self.tick_lbl_pos = :nextTo - self.format_code = "General" - self.crosses = :autoZero - self.gridlines = true - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end - + # The color for this axis. This value is used when rendering the axis line in the chart. + # colors should be in 6 character rbg format + # @return [String] the rbg color assinged. + # @see color def color=(color_rgb) @color = color_rgb end + # The position of the axis # must be one of [:l, :r, :t, :b] def ax_pos=(v) RestrictionValidator.validate "#{self.class}.ax_pos", [:l, :r, :b, :t], v; @ax_pos = v; end @@ -109,7 +113,6 @@ module Axlsx # default true def gridlines=(v) Axlsx::validate_boolean(v); @gridlines = v; end - # Specify if axis should be removed from the chart # default false def delete=(v) Axlsx::validate_boolean(v); @delete = v; end @@ -127,7 +130,6 @@ module Axlsx @label_rotation = adjusted end - # The title object for the chart. # @param [String, Cell] v # @return [Title] @@ -178,4 +180,5 @@ module Axlsx end end + end diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 1abfcd0d..70762c96 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -522,7 +522,86 @@ module Axlsx yield image if block_given? image end - + + # Serializes the worksheet object to an xml string + # This intentionally does not use nokogiri for performance reasons + # @return [String] + def to_xml_string + rels = relationships + str = '<?xml version="1.0" encoding="UTF-8"?>' + str << worksheet_node + str << sheet_pr_node + str << dimension_node + @sheet_view.to_xml_string(str) if @sheet_view + str << cols_node + str << sheet_data_node + + str << auto_filter_node + @sheet_protection.to_xml_string(str) if @sheet_protection + str << protected_ranges_node + str << merged_cells_node + @print_options.to_xml_string(str) if @print_options + page_margins.to_xml_string(str) if @page_margins + page_setup.to_xml_string(str) if @page_setup + str << drawing_node + str << legacy_drawing_node + str << table_parts_node + str << conditional_formattings_node + str << data_validations_node + str << '</worksheet>' + # User reported that when parsing some old data that had control characters excel chokes. + # All of the following are defined as illegal xml characters in the xml spec, but for now I am only dealing with control + # characters. Thanks to asakusarb and @hsbt's flash of code on the screen! + # [#x1-#x8], [#xB-#xC], [#xE-#x1F], [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF], + # [#x1FFFE-#x1FFFF], [#x2FFFE-#x2FFFF], [#x3FFFE-#x3FFFF], + # [#x4FFFE-#x4FFFF], [#x5FFFE-#x5FFFF], [#x6FFFE-#x6FFFF], + # [#x7FFFE-#x7FFFF], [#x8FFFE-#x8FFFF], [#x9FFFE-#x9FFFF], + # [#xAFFFE-#xAFFFF], [#xBFFFE-#xBFFFF], [#xCFFFE-#xCFFFF], + # [#xDFFFE-#xDFFFF], [#xEFFFE-#xEFFFF], [#xFFFFE-#xFFFFF], + # [#x10FFFE-#x10FFFF]. + str.gsub(/[[:cntrl:]]/,'') + end + + # The worksheet relationships. This is managed automatically by the worksheet + # @return [Relationships] + def relationships + r = Relationships.new + @tables.each do |table| + r << Relationship.new(TABLE_R, "../#{table.pn}") + end + + r << Relationship.new(VML_DRAWING_R, "../#{@comments.vml_drawing.pn}") if @comments.size > 0 + r << Relationship.new(COMMENT_R, "../#{@comments.pn}") if @comments.size > 0 + r << Relationship.new(COMMENT_R_NULL, "NULL") if @comments.size > 0 + + r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing + r + end + + # Returns the cell or cells defined using excel style A1:B3 references. + # @param [String|Integer] cell_def the string defining the cell or range of cells, or the rownumber + # @return [Cell, Array] + def [] (cell_def) + return rows[cell_def] if cell_def.is_a?(Integer) + + parts = cell_def.split(':') + first = name_to_cell parts[0] + if parts.size == 1 + first + else + cells = [] + last = name_to_cell(parts[1]) + rows[(first.row.index..last.row.index)].each do |r| + r.cells[(first.index..last.index)].each do |c| + cells << c + end + end + cells + end + end + + private + # Helper method for parsingout the root node for worksheet # @return [String] def worksheet_node @@ -626,84 +705,6 @@ module Axlsx str << '</dataValidations>' end - # Serializes the worksheet object to an xml string - # This intentionally does not use nokogiri for performance reasons - # @return [String] - def to_xml_string - rels = relationships - str = '<?xml version="1.0" encoding="UTF-8"?>' - str << worksheet_node - str << sheet_pr_node - str << dimension_node - @sheet_view.to_xml_string(str) if @sheet_view - str << cols_node - str << sheet_data_node - - str << auto_filter_node - @sheet_protection.to_xml_string(str) if @sheet_protection - str << protected_ranges_node - str << merged_cells_node - @print_options.to_xml_string(str) if @print_options - page_margins.to_xml_string(str) if @page_margins - page_setup.to_xml_string(str) if @page_setup - str << drawing_node - str << legacy_drawing_node - str << table_parts_node - str << conditional_formattings_node - str << data_validations_node - str << '</worksheet>' - # User reported that when parsing some old data that had control characters excel chokes. - # All of the following are defined as illegal xml characters in the xml spec, but for now I am only dealing with control - # characters. Thanks to asakusarb and @hsbt's flash of code on the screen! - # [#x1-#x8], [#xB-#xC], [#xE-#x1F], [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF], - # [#x1FFFE-#x1FFFF], [#x2FFFE-#x2FFFF], [#x3FFFE-#x3FFFF], - # [#x4FFFE-#x4FFFF], [#x5FFFE-#x5FFFF], [#x6FFFE-#x6FFFF], - # [#x7FFFE-#x7FFFF], [#x8FFFE-#x8FFFF], [#x9FFFE-#x9FFFF], - # [#xAFFFE-#xAFFFF], [#xBFFFE-#xBFFFF], [#xCFFFE-#xCFFFF], - # [#xDFFFE-#xDFFFF], [#xEFFFE-#xEFFFF], [#xFFFFE-#xFFFFF], - # [#x10FFFE-#x10FFFF]. - str.gsub(/[[:cntrl:]]/,'') - end - - # The worksheet relationships. This is managed automatically by the worksheet - # @return [Relationships] - def relationships - r = Relationships.new - @tables.each do |table| - r << Relationship.new(TABLE_R, "../#{table.pn}") - end - - r << Relationship.new(VML_DRAWING_R, "../#{@comments.vml_drawing.pn}") if @comments.size > 0 - r << Relationship.new(COMMENT_R, "../#{@comments.pn}") if @comments.size > 0 - r << Relationship.new(COMMENT_R_NULL, "NULL") if @comments.size > 0 - - r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing - r - end - - # Returns the cell or cells defined using excel style A1:B3 references. - # @param [String|Integer] cell_def the string defining the cell or range of cells, or the rownumber - # @return [Cell, Array] - def [] (cell_def) - return rows[cell_def] if cell_def.is_a?(Integer) - - parts = cell_def.split(':') - first = name_to_cell parts[0] - if parts.size == 1 - first - else - cells = [] - last = name_to_cell(parts[1]) - rows[(first.row.index..last.row.index)].each do |r| - r.cells[(first.index..last.index)].each do |c| - cells << c - end - end - cells - end - end - - private # assigns the owner workbook for this worksheet def workbook=(v) DataTypeValidator.validate "Worksheet.workbook", Workbook, v; @workbook = v; end |
