diff options
29 files changed, 179 insertions, 133 deletions
diff --git a/.travis.yml b/.travis.yml index 657f9173..7f6dc1e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ dist: trusty group: beta cache: bundler bundler_args: --without profile - + notifications: irc: "irc.freenode.org#axlsx" email: @@ -17,7 +17,6 @@ matrix: - rvm: 2.1 - rvm: 2.2.8 - rvm: 2.3.5 - - rvm: 2.0.0 - rvm: 2.4.2 - rvm: rbx-3 - rvm: jruby-19mode diff --git a/CHANGELOG.md b/CHANGELOG.md index 0639f3ef..901ab193 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ CHANGELOG --------- +- **April.1.17**:3.0.0-pre + - Support for ruby versions limited to officially supported version + - Updates to dependency gems, especially nokogiri and ruby-zip + - Patch options parsing for two cell anchor + - Remove support for depreciated worksheet members - **November.25.12**:1.3.4 - Support for headers and footers for worksheets - bug fix: Properly escape hyperlink urls @@ -8,7 +8,7 @@ group :test do else gem 'rake' end - gem 'simplecov' + gem 'simplecov', '>= 0.14.1' gem 'test-unit' end @@ -17,17 +17,17 @@ appreciation for the gem, please don't hesitate to make a donation. **Author**: Randy Morgan -**Copyright**: 2011 - 2013 +**Copyright**: 2011 - 2017 **License**: MIT License -**Latest Version**: 2.0.1 +**Latest Version**: 3.0.0 -**Ruby Version**: 1.9.2, 1.9.3, 2.0.0 +**Ruby Version**: 2.2.7, 2.3.4, 2.4.1 **JRuby Version**: 1.9 modes -**Rubinius Version**: rubinius 2.0.0dev * lower versions may run, this gem always tests against head. +**Rubinius Version**: rubinius 3 * lower versions may run, this gem always tests against head. **Release Date**: September 12th 2013 diff --git a/axlsx.gemspec b/axlsx.gemspec index 228d5a98..0b4ebf78 100644 --- a/axlsx.gemspec +++ b/axlsx.gemspec @@ -17,14 +17,14 @@ Gem::Specification.new do |s| s.files = Dir.glob("{lib/**/*,examples/**/*.rb,examples/**/*.jpeg}") + %w{ LICENSE README.md Rakefile CHANGELOG.md .yardopts .yardopts_guide } s.test_files = Dir.glob("{test/**/*}") - s.add_runtime_dependency 'nokogiri', '>= 1.6.6' - s.add_runtime_dependency 'rubyzip', '>= 1.2.1' + s.add_runtime_dependency 'nokogiri', '>= 1.7.1' + s.add_runtime_dependency 'rubyzip','~> 1.2', '>= 1.2.1' s.add_runtime_dependency "htmlentities", "~> 4.3.4" s.add_runtime_dependency "mimemagic", "~> 0.3" - s.add_development_dependency 'yard' + s.add_development_dependency 'yard', "~> 0.9.8" s.add_development_dependency 'kramdown' - s.add_development_dependency 'timecop', "~> 0.6.1" + s.add_development_dependency 'timecop', "~> 0.8.1" s.required_ruby_version = '>= 1.9.2' s.require_path = 'lib' end diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 586562f6..23fc6c8b 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -11,7 +11,6 @@ require 'axlsx/util/serialized_attributes' require 'axlsx/util/options_parser' # to be included with parsable intitites. #require 'axlsx/util/parser.rb' -require 'axlsx/util/string' require 'axlsx/util/mime_type_utils' require 'axlsx/stylesheet/styles.rb' diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb index de87b91a..d3a7e9ff 100644 --- a/lib/axlsx/drawing/chart.rb +++ b/lib/axlsx/drawing/chart.rb @@ -56,7 +56,7 @@ module Axlsx # Indicates that colors should be varied by datum # @return [Boolean] attr_reader :vary_colors - + # Configures the vary_colors options for this chart # @param [Boolean] v The value to set def vary_colors=(v) Axlsx::validate_boolean(v); @vary_colors = v; end @@ -129,6 +129,8 @@ module Axlsx end # The size of the Title object of the chart. + # @param [String] v The size for the title object + # @see Title def title_size=(v) @title.text_size = v unless v.to_s.empty? end diff --git a/lib/axlsx/drawing/one_cell_anchor.rb b/lib/axlsx/drawing/one_cell_anchor.rb index 1ae7c1b9..ffa10a7d 100644 --- a/lib/axlsx/drawing/one_cell_anchor.rb +++ b/lib/axlsx/drawing/one_cell_anchor.rb @@ -7,7 +7,7 @@ module Axlsx class OneCellAnchor include Axlsx::OptionsParser - + # Creates a new OneCellAnchor object and an Pic associated with it. # @param [Drawing] drawing # @option options [Array] start_at the col, row to start at @@ -23,6 +23,7 @@ module Axlsx drawing.anchors << self @from = Marker.new parse_options options + start_at(*options[:start_at]) if options[:start_at] @object = Pic.new(self, options) end diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index 5a418df0..e8d0ebd7 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -115,7 +115,6 @@ module Axlsx end # providing access to the anchor's width attribute - # @param [Integer] v # @see OneCellAnchor.width def width return unless @anchor.is_a?(OneCellAnchor) @@ -129,7 +128,6 @@ module Axlsx end # providing access to update the anchor's height attribute - # @param [Integer] v # @see OneCellAnchor.width # @note this is a noop if you are using a TwoCellAnchor def height diff --git a/lib/axlsx/drawing/two_cell_anchor.rb b/lib/axlsx/drawing/two_cell_anchor.rb index b18268f7..d422c0ce 100644 --- a/lib/axlsx/drawing/two_cell_anchor.rb +++ b/lib/axlsx/drawing/two_cell_anchor.rb @@ -37,11 +37,16 @@ module Axlsx drawing.anchors << self @from, @to = Marker.new, Marker.new(:col => 5, :row=>10) parse_options options + + # bit of a hack to work around the fact that the coords for start at and end at + # are passed in as an array when specified in intialization options - however + start_at(*options[:start_at]) if options[:start_at] + end_at(*options[:end_at]) if options[:end_at] end # sets the col, row attributes for the from marker. # @note The recommended way to set the start position for graphical - # objects is directly thru the object. + # objects is directly thru the object. # @see Chart#start_at def start_at(x, y=nil) from.coord x, y diff --git a/lib/axlsx/rels/relationships.rb b/lib/axlsx/rels/relationships.rb index b855abcd..5d07f612 100644 --- a/lib/axlsx/rels/relationships.rb +++ b/lib/axlsx/rels/relationships.rb @@ -10,14 +10,17 @@ require 'axlsx/rels/relationship.rb' def initialize super Relationship end - + # The relationship instance for the given source object, or nil if none exists. # @see Relationship#source_obj # @return [Relationship] def for(source_obj) find{ |rel| rel.source_obj == source_obj } end - + + # serialize relationships + # @param [String] str + # @return [String] def to_xml_string(str = '') str << '<?xml version="1.0" encoding="UTF-8"?>' str << ('<Relationships xmlns="' << RELS_R << '">') diff --git a/lib/axlsx/util/constants.rb b/lib/axlsx/util/constants.rb index 64bae270..aa8eb626 100644 --- a/lib/axlsx/util/constants.rb +++ b/lib/axlsx/util/constants.rb @@ -385,13 +385,16 @@ module Axlsx # @see http://www.codetable.net/asciikeycodes pattern = "\x0-\x08\x0B\x0C\x0E-\x1F" pattern = pattern.respond_to?(:encode) ? pattern.encode('UTF-8') : pattern - + # The regular expression used to remove control characters from worksheets CONTROL_CHARS = pattern.freeze - + + # ISO 8601 date recognition ISO_8601_REGEX = /\A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z/.freeze - + + # FLOAT recognition FLOAT_REGEX = /\A[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\Z/.freeze - + + # Numeric recognition NUMERIC_REGEX = /\A[+-]?\d+?\Z/.freeze end diff --git a/lib/axlsx/util/string.rb b/lib/axlsx/util/string.rb deleted file mode 100644 index 8def6e5b..00000000 --- a/lib/axlsx/util/string.rb +++ /dev/null @@ -1,7 +0,0 @@ -unless String.method_defined? :prepend - class String - def prepend(other_str) - insert(0, other_str) - end - end -end
\ No newline at end of file diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb index 2b4afd8d..71652ef2 100644 --- a/lib/axlsx/util/validators.rb +++ b/lib/axlsx/util/validators.rb @@ -56,13 +56,13 @@ module Axlsx raise ArgumentError, (ERR_TYPE % [v.inspect, name, types.inspect]) unless other.call(v) end v_class = v.is_a?(Class) ? v : v.class - Array(types).each do |t| + Array(types).each do |t| return if v_class <= t end raise ArgumentError, (ERR_TYPE % [v.inspect, name, types.inspect]) end end - + # Requires that the value can be converted to an integer # @para, [Any] v the value to validate @@ -78,7 +78,8 @@ module Axlsx def self.validate_angle(v) raise ArgumentError, (ERR_ANGLE % v.inspect) unless (v.to_i >= -5400000 && v.to_i <= 5400000) end - + + # Validates an unsigned intger UINT_VALIDATOR = lambda { |arg| arg.respond_to?(:>=) && arg >= 0 } # Requires that the value is a Integer and is greater or equal to 0 @@ -148,7 +149,7 @@ module Axlsx RestrictionValidator.validate "cell run style u", [:none, :single, :double, :singleAccounting, :doubleAccounting], v end - # validates cell style family which must be between 1 and 5 + # validates cell style family which must be between 1 and 5 def self.validate_family(v) RestrictionValidator.validate "cell run style family", 1..5, v end diff --git a/lib/axlsx/version.rb b/lib/axlsx/version.rb index df4bd9ed..10f78875 100644 --- a/lib/axlsx/version.rb +++ b/lib/axlsx/version.rb @@ -1,5 +1,5 @@ module Axlsx # The current version - VERSION = "2.1.0.pre" + VERSION = "3.0.0.pre" end diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 4ba44adb..7572e51f 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -280,6 +280,10 @@ require 'axlsx/workbook/worksheet/selection.rb' worksheet end + # Adds a new WorkbookView + # @return WorkbookViews + # @option options [Hash] options passed into the added WorkbookView + # @see WorkbookView#initialize def add_view(options={}) views << WorkbookView.new(options) end diff --git a/lib/axlsx/workbook/workbook_view.rb b/lib/axlsx/workbook/workbook_view.rb index 11eae571..e0d38cae 100644 --- a/lib/axlsx/workbook/workbook_view.rb +++ b/lib/axlsx/workbook/workbook_view.rb @@ -33,7 +33,7 @@ module Axlsx # Creates a new BookView object - # @params [Hash] options A hash of key/value pairs that will be mapped to this instances attributes. + # @param [Hash] options A hash of key/value pairs that will be mapped to this instances attributes. # @option [Symbol] visibility Specifies visible state of the workbook window. The default value for this attribute is :visible. # @option [Boolean] minimized Specifies a boolean value that indicates whether the workbook window is minimized. # @option [Boolean] show_horizontal_scroll Specifies a boolean value that indicates whether to display the horizontal scroll bar in the user interface. @@ -41,7 +41,7 @@ module Axlsx # @option [Boolean] show_sheet_tabs Specifies a boolean value that indicates whether to display the sheet tabs in the user interface. # @option [Integer] tab_ratio Specifies ratio between the workbook tabs bar and the horizontal scroll bar. # @option [Integer] first_sheet Specifies the index to the first sheet in this book view. - # @option [Integer] active_tab Specifies an unsignedInt that contains the index to the active sheet in this book view. + # @option [Integer] active_tab Specifies an unsignedInt that contains the index to the active sheet in this book view. # @option [Integer] x_window Specifies the X coordinate for the upper left corner of the workbook window. The unit of measurement for this value is twips. # @option [Integer] y_window Specifies the Y coordinate for the upper left corner of the workbook window. The unit of measurement for this value is twips. # @option [Integer] window_width Specifies the width of the workbook window. The unit of measurement for this value is twips. @@ -68,7 +68,9 @@ module Axlsx :show_sheet_tabs, :auto_filter_date_grouping - + # Serialize the WorkbookView + # @param [String] str + # @return [String] def to_xml_string(str = '') str << '<workbookView ' serialized_attributes str diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 777b7812..fb10ad36 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -65,6 +65,7 @@ module Axlsx :shadow, :condense, :extend, :u, :vertAlign, :sz, :color, :scheme].freeze + # An array of valid cell types CELL_TYPES = [:date, :time, :float, :integer, :richtext, :string, :boolean, :iso_8601, :text].freeze @@ -79,7 +80,7 @@ module Axlsx # @return [Row] attr_reader :row - # The cell's data type. Currently only six types are supported, :date, :time, :float, :integer, :string and :boolean. + # The cell's data type. # Changing the type for a cell will recast the value into that type. If no type option is specified in the constructor, the type is # automatically determed. # @see Cell#cell_type_from_value @@ -348,6 +349,8 @@ module Axlsx # returns the name of the cell attr_reader :name + # Attempts to determine the correct width for this cell's content + # @return [Float] def autowidth return if is_formula? || value.nil? if contains_rich_text? diff --git a/lib/axlsx/workbook/worksheet/cfvos.rb b/lib/axlsx/workbook/worksheet/cfvos.rb index 0b5a21b3..eb44f9d8 100644 --- a/lib/axlsx/workbook/worksheet/cfvos.rb +++ b/lib/axlsx/workbook/worksheet/cfvos.rb @@ -8,6 +8,9 @@ module Axlsx super(Cfvo) end + # Serialize the Cfvo object + # @param [String] str + # @return [String] def to_xml_string(str='') each { |cfvo| cfvo.to_xml_string(str) } end diff --git a/lib/axlsx/workbook/worksheet/cols.rb b/lib/axlsx/workbook/worksheet/cols.rb index d4595f29..c0704343 100644 --- a/lib/axlsx/workbook/worksheet/cols.rb +++ b/lib/axlsx/workbook/worksheet/cols.rb @@ -1,6 +1,6 @@ module Axlsx - # The cols class manages the col object used to manage column widths. + # The cols class manages the col object used to manage column widths. # This is where the magic happens with autowidth class Cols < SimpleTypedList @@ -10,11 +10,14 @@ module Axlsx @worksheet = worksheet end + # Serialize the Cols object + # @param [String] str + # @return [String] def to_xml_string(str = '') return if empty? str << '<cols>' each { |item| item.to_xml_string(str) } - str << '</cols>' + str << '</cols>' end end end diff --git a/lib/axlsx/workbook/worksheet/outline_pr.rb b/lib/axlsx/workbook/worksheet/outline_pr.rb index ae9ccf38..f25ef4fa 100644 --- a/lib/axlsx/workbook/worksheet/outline_pr.rb +++ b/lib/axlsx/workbook/worksheet/outline_pr.rb @@ -18,7 +18,7 @@ module Axlsx :apply_styles # Creates a new OutlinePr object - # @param [Worksheet] worksheet The worksheet that owns this OutlinePr object + # @param [Hash] options used to create the outline_pr def initialize(options = {}) parse_options options end diff --git a/lib/axlsx/workbook/worksheet/protected_ranges.rb b/lib/axlsx/workbook/worksheet/protected_ranges.rb index 46a2546b..d274cf80 100644 --- a/lib/axlsx/workbook/worksheet/protected_ranges.rb +++ b/lib/axlsx/workbook/worksheet/protected_ranges.rb @@ -24,11 +24,14 @@ module Axlsx last end + # Serializes the protected ranges + # @param [String] str + # @return [String] def to_xml_string(str = '') return if empty? str << '<protectedRanges>' each { |range| range.to_xml_string(str) } - str << '</protectedRanges>' + str << '</protectedRanges>' end end end diff --git a/lib/axlsx/workbook/worksheet/rich_text.rb b/lib/axlsx/workbook/worksheet/rich_text.rb index 9124dfbe..e8c58c67 100644 --- a/lib/axlsx/workbook/worksheet/rich_text.rb +++ b/lib/axlsx/workbook/worksheet/rich_text.rb @@ -1,5 +1,7 @@ module Axlsx + class RichText < SimpleTypedList + def initialize(text = nil, options={}) super(RichTextRun) add_run(text, options) unless text.nil? @@ -7,12 +9,12 @@ module Axlsx end attr_reader :cell - + def cell=(cell) @cell = cell each { |run| run.cell = cell } end - + def autowidth widtharray = [0] # Are arrays the best way of solving this problem? each { |run| run.autowidth(widtharray) } @@ -22,7 +24,7 @@ module Axlsx def add_run(text, options={}) self << RichTextRun.new(text, options) end - + def runs self end diff --git a/lib/axlsx/workbook/worksheet/rich_text_run.rb b/lib/axlsx/workbook/worksheet/rich_text_run.rb index e56dc3ae..7ee3bf3a 100644 --- a/lib/axlsx/workbook/worksheet/rich_text_run.rb +++ b/lib/axlsx/workbook/worksheet/rich_text_run.rb @@ -1,26 +1,26 @@ module Axlsx class RichTextRun - + include Axlsx::OptionsParser - + attr_reader :value - + INLINE_STYLES = [:font_name, :charset, :family, :b, :i, :strike, :outline, :shadow, :condense, :extend, :u, :vertAlign, :sz, :color, :scheme].freeze - + def initialize(value, options={}) self.value = value - parse_options(options) + parse_options(options) end - + def value=(value) @value = value end - + attr_accessor :cell - + # The inline font_name property for the cell # @return [String] attr_reader :font_name @@ -157,7 +157,7 @@ module Axlsx # The Shared Strings Table index for this cell # @return [Integer] attr_reader :ssti - + # @return [Integer] The cellXfs item index applied to this cell. # @raise [ArgumentError] Invalid cellXfs id if the value provided is not within cellXfs items range. def style=(v) @@ -167,6 +167,9 @@ module Axlsx @style = v end + # Tries to work out the width of the longest line in the run + # @param [Array] widtharray this array is populated with the widths of each line in the run. + # @return [Array] def autowidth(widtharray) return if value.nil? if styles.cellXfs[style].alignment && styles.cellXfs[style].alignment.wrap_text @@ -184,19 +187,19 @@ module Axlsx end widtharray end - + # Utility method for setting inline style attributes def set_run_style(validator, attr, value) return unless INLINE_STYLES.include?(attr.to_sym) Axlsx.send(validator, value) unless validator.nil? self.instance_variable_set :"@#{attr.to_s}", value end - + def to_xml_string(str = '') valid = RichTextRun::INLINE_STYLES - data = Hash[self.instance_values.map{ |k, v| [k.to_sym, v] }] + data = Hash[self.instance_values.map{ |k, v| [k.to_sym, v] }] data = data.select { |key, value| valid.include?(key) && !value.nil? } - + str << '<r><rPr>' data.keys.each do |key| case key @@ -211,9 +214,9 @@ module Axlsx clean_value = Axlsx::trust_input ? @value.to_s : ::CGI.escapeHTML(Axlsx::sanitize(@value.to_s)) str << ('</rPr><t>' << clean_value << '</t></r>') end - + private - + # Returns the width of a string according to the current style # This is still not perfect... # - scaling is not linear as font sizes increase @@ -221,7 +224,7 @@ module Axlsx font_scale = font_size / 10.0 string.count(Worksheet::THIN_CHARS) * font_scale end - + # we scale the font size if bold style is applied to either the style font or # the cell itself. Yes, it is a bit of a hack, but it is much better than using # imagemagick and loading metrics for every character. @@ -230,15 +233,15 @@ module Axlsx font = styles.fonts[styles.cellXfs[style].fontId] || styles.fonts[0] (font.b || (defined?(@b) && @b)) ? (font.sz * 1.5) : font.sz end - + def style cell.style end - + def styles - cell.row.worksheet.styles + cell.row.worksheet.styles end - + # Converts the value to the correct XML representation (fixes issues with # Numbers) def xml_value value diff --git a/lib/axlsx/workbook/worksheet/tables.rb b/lib/axlsx/workbook/worksheet/tables.rb index 624c96c4..d033212c 100644 --- a/lib/axlsx/workbook/worksheet/tables.rb +++ b/lib/axlsx/workbook/worksheet/tables.rb @@ -20,6 +20,9 @@ module Axlsx map{ |table| Relationship.new(table, TABLE_R, "../#{table.pn}") } end + # renders the tables xml + # @param [String] str + # @return [String] def to_xml_string(str = "") return if empty? str << "<tableParts count='#{size}'>" diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index f094c5cd..1ad297e6 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -8,7 +8,7 @@ module Axlsx # definition of characters which are less than the maximum width of 0-9 in the default font for use in String#count. # This is used for autowidth calculations THIN_CHARS = '^.acfijklrstxzFIJL()-'.freeze - + # Creates a new worksheet. # @note the recommended way to manage worksheets is Workbook#add_worksheet # @see Workbook#add_worksheet @@ -327,27 +327,27 @@ module Axlsx # Indicates if the worksheet should show gridlines or not # @return Boolean # @deprecated Use SheetView#show_grid_lines instead. - def show_gridlines - warn('axlsx::DEPRECIATED: Worksheet#show_gridlines has been depreciated. This value can get over SheetView#show_grid_lines.') - sheet_view.show_grid_lines - end + # def show_gridlines + # warn('axlsx::DEPRECIATED: Worksheet#show_gridlines has been depreciated. This value can get over SheetView#show_grid_lines.') + # sheet_view.show_grid_lines + # end # Indicates if the worksheet is selected in the workbook # It is possible to have more than one worksheet selected, however it might cause issues # in some older versions of excel when using copy and paste. # @return Boolean # @deprecated Use SheetView#tab_selected instead. - def selected - warn('axlsx::DEPRECIATED: Worksheet#selected has been depreciated. This value can get over SheetView#tab_selected.') - sheet_view.tab_selected - end + # def selected + # warn('axlsx::DEPRECIATED: Worksheet#selected has been depreciated. This value can get over SheetView#tab_selected.') + # sheet_view.tab_selected + # end # (see #fit_to_page) # @return [Boolean] - def fit_to_page=(v) - warn('axlsx::DEPRECIATED: Worksheet#fit_to_page has been depreciated. This value will automatically be set for you when you use PageSetup#fit_to.') - fit_to_page? - end + # def fit_to_page=(v) + # warn('axlsx::DEPRECIATED: Worksheet#fit_to_page has been depreciated. This value will automatically be set for you when you use PageSetup#fit_to.') + # fit_to_page? + # end # The name of the worksheet # The name of a worksheet must be unique in the workbook, and must not exceed 31 characters @@ -630,7 +630,7 @@ module Axlsx # 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) + def [](cell_def) return rows[cell_def] if cell_def.is_a?(Integer) parts = cell_def.split(':').map{ |part| name_to_cell part } if parts.size == 1 diff --git a/lib/axlsx/workbook/worksheet/worksheet_drawing.rb b/lib/axlsx/workbook/worksheet/worksheet_drawing.rb index 08cad1f7..ae03ac0f 100644 --- a/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +++ b/lib/axlsx/workbook/worksheet/worksheet_drawing.rb @@ -1,5 +1,5 @@ module Axlsx - + # This is a utility class for serialing the drawing node in a # worksheet. Drawing objects have their own serialization that exports # a drawing document. This is only for the single node in the @@ -17,7 +17,7 @@ module Axlsx attr_reader :worksheet attr_reader :drawing - + # adds a chart to the drawing object # @param [Class] chart_type The type of chart to add # @param [Hash] options Options to pass on to the drawing and chart @@ -26,17 +26,17 @@ module Axlsx @drawing ||= Drawing.new worksheet drawing.add_chart(chart_type, options) end - + # adds an image to the drawing object - # @param [Hash] options Options to pass on to the drawing and image + # @param [Hash] options Options to pass on to the drawing and image # @see Worksheet#add_image def add_image(options) - @drawing ||= Drawing.new worksheet + @drawing ||= Drawing.new(worksheet) drawing.add_image(options) - end - + end + # helper method to tell us if the drawing has something in it or not - # @return [Boolean] + # @return [Boolean] def has_drawing? @drawing.is_a? Drawing end @@ -45,13 +45,13 @@ module Axlsx # @return [Relationship] def relationship return unless has_drawing? - Relationship.new(self, DRAWING_R, "../#{drawing.pn}") + Relationship.new(self, DRAWING_R, "../#{drawing.pn}") end # Serialize the drawing for the worksheet # @param [String] str def to_xml_string(str = '') - return unless has_drawing? + return unless has_drawing? str << "<drawing r:id='#{relationship.Id}'/>" end end diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 8b69e73a..64a5435b 100644 --- a/test/drawing/tc_chart.rb +++ b/test/drawing/tc_chart.rb @@ -6,7 +6,7 @@ class TestChart < Test::Unit::TestCase @p = Axlsx::Package.new ws = @p.workbook.add_worksheet @row = ws.add_row ["one", 1, Time.now] - @chart = ws.add_chart Axlsx::Bar3DChart, :title => "fishery" + @chart = ws.add_chart Axlsx::Bar3DChart, :title => "fishery", :bg_color => "000000" end def teardown @@ -27,17 +27,30 @@ class TestChart < Test::Unit::TestCase assert_equal(@chart.title.cell, @row.cells.first) end + def test_style + assert_raise(ArgumentError) { @chart.style = 49 } + assert_nothing_raised { @chart.style = 2 } + assert_equal(@chart.style, 2) + end + def test_to_from_marker_access assert(@chart.to.is_a?(Axlsx::Marker)) assert(@chart.from.is_a?(Axlsx::Marker)) end - def test_style - assert_raise(ArgumentError) { @chart.style = 49 } - assert_nothing_raised { @chart.style = 2 } - assert_equal(@chart.style, 2) + def test_bg_color + assert_raise(ArgumentError) { @chart.bg_color = 2 } + assert_nothing_raised { @chart.bg_color = "FFFFFF" } + assert_equal(@chart.bg_color, "FFFFFF") + end - + + def test_title_size + assert_raise(ArgumentError) { @chart.title_size = 2 } + assert_nothing_raised { @chart.title_size = "100" } + assert_equal(@chart.title.text_size, "100") + end + def test_vary_colors assert_equal(true, @chart.vary_colors) assert_raise(ArgumentError) { @chart.vary_colors = 7 } @@ -63,7 +76,7 @@ class TestChart < Test::Unit::TestCase @chart.start_at [5,6] assert_equal(@chart.graphic_frame.anchor.from.col, 5) assert_equal(@chart.graphic_frame.anchor.from.row, 6) - + end def test_end_at @@ -76,7 +89,7 @@ class TestChart < Test::Unit::TestCase @chart.end_at [10,11] assert_equal(@chart.graphic_frame.anchor.to.col, 10) assert_equal(@chart.graphic_frame.anchor.to.row, 11) - + end def test_add_series @@ -88,13 +101,13 @@ class TestChart < Test::Unit::TestCase def test_pn assert_equal(@chart.pn, "charts/chart1.xml") end - + def test_d_lbls assert_equal(nil, @chart.instance_values[:d_lbls]) @chart.d_lbls.d_lbl_pos = :t assert(@chart.d_lbls.is_a?(Axlsx::DLbls), 'DLbls instantiated on access') end - + def test_to_xml_string schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) doc = Nokogiri::XML(@chart.to_xml_string) diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index 166850e5..c7e963f5 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -93,7 +93,7 @@ class TestWorksheet < Test::Unit::TestCase page_setup = {:fit_to_height => 1, :fit_to_width => 1, :orientation => :landscape, :paper_width => "210mm", :paper_height => "297mm", :scale => 80} print_options = {:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true} header_footer = {:different_first => false, :different_odd_even => false, :odd_header => 'Header'} - optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :page_setup => page_setup, :print_options => print_options, :header_footer => header_footer, :selected => true, :show_gridlines => false) + optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :page_setup => page_setup, :print_options => print_options, :header_footer => header_footer) page_margins.keys.each do |key| assert_equal(page_margins[key], optioned.page_margins.send(key)) end @@ -107,23 +107,21 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(header_footer[key], optioned.header_footer.send(key)) end assert_equal(optioned.name, 'bob') - assert_equal(optioned.selected, true) - assert_equal(optioned.show_gridlines, false) end - def test_use_gridlines - assert_raise(ArgumentError) { @ws.show_gridlines = -1.1 } - assert_nothing_raised { @ws.show_gridlines = false } - assert_equal(@ws.show_gridlines, false) - end + # def test_use_gridlines + # assert_raise(ArgumentError) { @ws.show_gridlines = -1.1 } + # assert_nothing_raised { @ws.show_gridlines = false } + # assert_equal(@ws.show_gridlines, false) + # end - def test_selected - assert_raise(ArgumentError) { @ws.selected = -1.1 } - assert_nothing_raised { @ws.selected = true } - assert_equal(@ws.selected, true) - end + # def test_selected + # assert_raise(ArgumentError) { @ws.selected = -1.1 } + # assert_nothing_raised { @ws.selected = true } + # assert_equal(@ws.selected, true) + # end def test_rels_pn assert_equal(@ws.rels_pn, "worksheets/_rels/sheet1.xml.rels") @@ -266,11 +264,11 @@ class TestWorksheet < Test::Unit::TestCase end end - def test_to_xml_string_fit_to_page - @ws.page_setup.fit_to_width = 1 - doc = Nokogiri::XML(@ws.to_xml_string) - assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:pageSetUpPr[@fitToPage=1]').size, 1) - end + # def test_to_xml_string_fit_to_page + # @ws.page_setup.fit_to_width = 1 + # doc = Nokogiri::XML(@ws.to_xml_string) + # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:pageSetUpPr[@fitToPage=1]').size, 1) + # end def test_to_xml_string_dimensions @ws.add_row [1,2,3] @@ -278,22 +276,22 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(doc.xpath('//xmlns:worksheet/xmlns:dimension[@ref="A1:C1"]').size, 1) end - def test_fit_to_page_assignation_does_nothing - @ws.fit_to_page = true - assert_equal(@ws.fit_to_page?, false) - end + # def test_fit_to_page_assignation_does_nothing + # @ws.fit_to_page = true + # assert_equal(@ws.fit_to_page?, false) + # end - def test_to_xml_string_selected - @ws.selected = true - doc = Nokogiri::XML(@ws.to_xml_string) - assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView[@tabSelected=1]').size, 1) - end + # def test_to_xml_string_selected + # @ws.selected = true + # doc = Nokogiri::XML(@ws.to_xml_string) + # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView[@tabSelected=1]').size, 1) + # end - def test_to_xml_string_show_gridlines - @ws.show_gridlines = false - doc = Nokogiri::XML(@ws.to_xml_string) - assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView[@showGridLines=0]').size, 1) - end + # def test_to_xml_string_show_gridlines + # @ws.show_gridlines = false + # doc = Nokogiri::XML(@ws.to_xml_string) + # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView[@showGridLines=0]').size, 1) + # end def test_to_xml_string_auto_fit_data @ws.add_row [1, "two"] @@ -580,7 +578,7 @@ class TestWorksheet < Test::Unit::TestCase assert_raise(ArgumentError) { @wb.add_worksheet(:name => 'Sheet1') } assert_equal(1, @wb.worksheets.size) end - + def test_worksheet_only_includes_outline_pr_when_set doc = Nokogiri::XML(@ws.to_xml_string) assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr').size, 0) |
