diff options
| author | Jurriaan Pruis <[email protected]> | 2012-03-27 13:55:42 +0200 |
|---|---|---|
| committer | Jurriaan Pruis <[email protected]> | 2012-03-27 13:55:42 +0200 |
| commit | 3867eb132f20c3258f007ca61a738e90a09087dc (patch) | |
| tree | e8d0b196badf01ceefe56aa02ac40ad82371afce | |
| parent | 5afd30be1774fb5255dad2eb18c700d0d8f4a628 (diff) | |
| parent | be2d7bee9b9236fff4a378db8770c3ae793e8422 (diff) | |
| download | caxlsx-3867eb132f20c3258f007ca61a738e90a09087dc.tar.gz caxlsx-3867eb132f20c3258f007ca61a738e90a09087dc.zip | |
Merge github.com:randym/axlsx
77 files changed, 680 insertions, 365 deletions
@@ -264,6 +264,21 @@ To install Axlsx, use the following command: end end +##Generating A Scatter Chart + + wb.add_worksheet(:name => "Scatter Chart") do |sheet| + sheet.add_row ["First", 1, 5, 7, 9] + sheet.add_row ["", 1, 25, 49, 81] + sheet.add_row ["Second", 5, 2, 14, 9] + sheet.add_row ["", 5, 10, 15, 20] + sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart| + chart.start_at 0, 4 + chart.end_at 10, 19 + chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"] + chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"] + end + end + ##Auto Filter wb.add_worksheet(:name => "Auto Filter") do |sheet| diff --git a/axlsx.gemspec b/axlsx.gemspec index c8fda02f..d67401d3 100644 --- a/axlsx.gemspec +++ b/axlsx.gemspec @@ -29,6 +29,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'yard' s.add_development_dependency 'yard' s.add_development_dependency 'rdiscount' + s.add_development_dependency 'perftools.rb' s.required_ruby_version = '>= 1.8.7' s.require_path = 'lib' diff --git a/examples/example.rb b/examples/example.rb index 36fe6b37..604ab597 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -w -s # -*- coding: utf-8 -*- -$:.unshift "#{File.dirname(__FILE__)}/../lib" +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" require 'axlsx' p = Axlsx::Package.new @@ -210,6 +210,21 @@ wb.add_worksheet(:name => "Line Chart") do |sheet| end end +##Generating A Scatter Chart + +wb.add_worksheet(:name => "Scatter Chart") do |sheet| + sheet.add_row ["First", 1, 5, 7, 9] + sheet.add_row ["", 1, 25, 49, 81] + sheet.add_row ["Second", 5, 2, 14, 9] + sheet.add_row ["", 5, 10, 15, 20] + sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart| + chart.start_at 0, 4 + chart.end_at 10, 19 + chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"] + chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"] + end +end + ##Auto Filter wb.add_worksheet(:name => "Auto Filter") do |sheet| diff --git a/lib/axlsx.rb b/lib/axlsx.rb index eb5068b5..cf3beb87 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -5,6 +5,9 @@ require 'axlsx/util/simple_typed_list.rb' require 'axlsx/util/constants.rb' require 'axlsx/util/validators.rb' require 'axlsx/util/storage.rb' + +#not even close to being ready but it does not break anything so it stays for now. +# needs a full re-write to use agile-encryption properly require 'axlsx/util/cbf.rb' require 'axlsx/util/ms_off_crypto.rb' diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 73e72493..35ab181a 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -184,7 +184,7 @@ module Axlsx end workbook.tables.each do |table| - @parts << {:entry => "xl/#{table.pn}", :doc => table.to_xml, :schema => SML_XSD} + @parts << {:entry => "xl/#{table.pn}", :doc => table.to_xml, :schema => SML_XSD} end workbook.charts.each do |chart| @@ -196,12 +196,12 @@ module Axlsx end if use_shared_strings - @parts << {:entry => "xl/#{SHARED_STRINGS_PN}", :doc => workbook.shared_strings.to_xml, :schema => SML_XSD} + @parts << {:entry => "xl/#{SHARED_STRINGS_PN}", :doc => workbook.shared_strings.to_xml_string, :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} + @parts << {:entry => "xl/#{sheet.pn}", :doc => sheet.to_xml_string, :schema => SML_XSD} end @parts end diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb index 4e9806ad..5d3e60ca 100644 --- a/lib/axlsx/stylesheet/color.rb +++ b/lib/axlsx/stylesheet/color.rb @@ -5,9 +5,9 @@ module Axlsx # Determines if the color is system color dependant # @return [Boolean] attr_reader :auto - - # The color as defined in rgb terms. - # @note + + # The color as defined in rgb terms. + # @note # rgb colors need to conform to ST_UnsignedIntHex. That basically means put 'FF' before you color # When assigning the rgb value the behavior is much like CSS selectors and can use shorthand versions as follows: # If you provide a two character value it will be repeated for each r, g, b assignment @@ -25,16 +25,16 @@ module Axlsx # no support for theme just yet # @return [Integer] #attr_reader :theme - + # The tint value. # @note valid values are between -1.0 and 1.0 # @return [Float] attr_reader :tint - + # Creates a new Color object # @option options [Boolean] auto # @option options [String] rgb - # @option options [Float] tint + # @option options [Float] tint def initialize(options={}) @rgb = "FF000000" options.each do |o| @@ -42,7 +42,7 @@ module Axlsx end end # @see auto - def auto=(v) Axlsx::validate_boolean v; @auto = v end + def auto=(v) Axlsx::validate_boolean v; @auto = v end # @see color def rgb=(v) Axlsx::validate_string(v) @@ -56,11 +56,19 @@ module Axlsx def tint=(v) Axlsx::validate_float v; @tint = v end # This version does not support themes - # def theme=(v) Axlsx::validate_unsigned_integer v; @theme = v end - + # def theme=(v) Axlsx::validate_unsigned_integer v; @theme = v end + # Indexed colors are for backward compatability which I am choosing not to support - # def indexed=(v) Axlsx::validate_unsigned_integer v; @indexed = v end + # def indexed=(v) Axlsx::validate_unsigned_integer v; @indexed = v end + def to_xml_string + str = ["<color "] + self.instance_values.each do |key, value| + str << "%s='%s' " % [key, value] + end + str << "/>" + str.join + end # Serializes the color # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index 91892c38..5ebfd87f 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -3,7 +3,7 @@ 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 iWorks Numbers - # and Google Docs require that the shared string table is populated in order to interoperate properly. + # 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 @@ -15,14 +15,14 @@ module Axlsx # @return [Integer] attr_reader :count - # The total number of unique strings in the workbook. + # 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. + # each of these unique cells is parsed into the shared string table. # @see Cell#sharable attr_reader :unique_cells @@ -35,10 +35,14 @@ module Axlsx resolve(cells) end + def to_xml_string + str = "<sst xmlns=\"%s\" count='%s' uniqueCount='%s'>%s</sst>" % [XML_NS, count, unique_count, @unique_cells.map {|cell| cell[:data]}.join] + 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| @@ -50,22 +54,25 @@ module Axlsx end private - - # Interate over all of the cells in the array. - # if our unique cells array does not contain a sharable cell, + + # 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) } + cell_hash = cell.shareable_hash + index = @unique_cells.index do |item| + item[:hash] == cell_hash + end if index == nil cell.send :ssti=, @unique_cells.size - @unique_cells << cell + @unique_cells << {:hash => cell_hash, :data => '<si>'<< cell.run_xml_string << '</si>'} else cell.send :ssti=, index end end - end + end end end diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index ff759cdf..a3ec046a 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -25,11 +25,18 @@ module Axlsx # An array of available inline styes. + # TODO change this to a hash where each key defines attr name and validator (and any info the validator requires) + # then move it out to a module so we can re-use in in other classes. + # needs to define bla=(v) and bla methods on the class that hook into a + # set_attr method that kicks the suplied validator and updates the instance_variable + # for the key INLINE_STYLES = ['value', 'type', 'font_name', 'charset', 'family', 'b', 'i', 'strike','outline', 'shadow', 'condense', 'extend', 'u', 'vertAlign', 'sz', 'color', 'scheme'] + INLINE_ATTR = [:font_name => { :validator=>:validate_string}] + # The index of the cellXfs item to be applied to this cell. # @return [Integer] @@ -69,71 +76,79 @@ module Axlsx @value = cast_value(v) end + + # Indicates that the cell has one or more of the custom cell styles applied. + # @return [Boolean] + def is_text_run? + @is_text_run ||= false + end + + # The inline font_name property for the cell # @return [String] attr_reader :font_name # @see font_name - def font_name=(v) Axlsx::validate_string(v); @font_name = v; end + def font_name=(v) set_run_style :validate_string, :font_name, v; end # The inline charset property for the cell # @return [String] attr_reader :charset # @see charset - def charset=(v) Axlsx::validate_unsigned_int(v); @charset = v; end + def charset=(v) set_run_style :validate_unsigned_int, :charset, v; end # The inline family property for the cell # @return [String] attr_reader :family # @see family - def family=(v) Axlsx::validate_string(v); @family = v; end + def family=(v) set_run_style :validate_string, :family, v; end # The inline bold property for the cell # @return [Boolean] attr_reader :b # @see b - def b=(v) Axlsx::validate_boolean(v); @b = v; end + def b=(v) set_run_style :validate_boolean, :b, v; end # The inline italic property for the cell # @return [Boolean] attr_reader :i # @see i - def i=(v) Axlsx::validate_boolean(v); @i = v; end + def i=(v) set_run_style :validate_boolean, :i, v; end # The inline strike property for the cell # @return [Boolean] attr_reader :strike # @see strike - def strike=(v) Axlsx::validate_boolean(v); @strike = v; end + def strike=(v) set_run_style :validate_boolean, :strike, v; end # The inline outline property for the cell # @return [Boolean] attr_reader :outline # @see outline - def outline=(v) Axlsx::validate_boolean(v); @outline = v; end + def outline=(v) set_run_style :validate_boolean, :outline, v; end # The inline shadow property for the cell # @return [Boolean] attr_reader :shadow # @see shadow - def shadow=(v) Axlsx::validate_boolean(v); @shadow = v; end + def shadow=(v) set_run_style :validate_boolean, :shadow, v; end # The inline condense property for the cell # @return [Boolean] attr_reader :condense # @see condense - def condense=(v) Axlsx::validate_boolean(v); @condense = v; end + def condense=(v) set_run_style :validate_boolean, :condense, v; end # The inline extend property for the cell # @return [Boolean] attr_reader :extend # @see extend - def extend=(v) Axlsx::validate_boolean(v); @extend = v; end + def extend=(v) set_run_style :validate_boolean, :extend, v; end # The inline underline property for the cell # @return [Boolean] attr_reader :u # @see u - def u=(v) Axlsx::validate_boolean(v); @u = v; end + def u=(v) set_run_style :validate_boolean, :u, v; end # The inline color property for the cell # @return [Color] @@ -141,27 +156,34 @@ module Axlsx # @param [String] The 8 character representation for an rgb color #FFFFFFFF" def color=(v) @color = v.is_a?(Color) ? v : Color.new(:rgb=>v) + @has_run_style = true end # The inline sz property for the cell # @return [Boolean] attr_reader :sz # @see sz - def sz=(v) Axlsx::validate_unsigned_int(v); @sz = v; end + def sz=(v) set_run_style :validate_unsigned_int, :sz, v; end # The inline vertical alignment property for the cell # this must be one of [:baseline, :subscript, :superscript] # @return [Symbol] attr_reader :vertAlign # @see vertAlign - def vertAlign=(v) RestrictionValidator.validate "Cell.vertAlign", [:baseline, :subscript, :superscript], v; @vertAlign = v; end + def vertAlign=(v) + RestrictionValidator.validate "Cell.vertAlign", [:baseline, :subscript, :superscript], v + set_run_style nil, :vertAlign, v + end # The inline scheme property for the cell # this must be one of [:none, major, minor] # @return [Symbol] attr_reader :scheme # @see scheme - def scheme=(v) RestrictionValidator.validate "Cell.schema", [:none, :major, :minor], v; @scheme = v; end + def scheme=(v) + RestrictionValidator.validate "Cell.schema", [:none, :major, :minor], v + set_run_style nil, :scheme, v + end # @param [Row] row The row this cell belongs to. # @param [Any] value The value associated with this cell. @@ -202,16 +224,10 @@ module Axlsx # 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) - - #using reject becase 1.8.7 select returns an array... - v_hash = v.instance_values.reject { |key, val| !INLINE_STYLES.include?(key) } + def shareable_hash self_hash = self.instance_values.reject { |key, val| !INLINE_STYLES.include?(key) } - # 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 + self_hash end # @return [Integer] The index of the cell in the containing row. @@ -259,6 +275,29 @@ module Axlsx self.row.worksheet.merge_cells "#{self.r}:#{range_end}" unless range_end.nil? end + def run_xml_string + str = "" + if is_text_run? + data = self.instance_values.reject{|key, value| value == nil } + keys = data.keys & INLINE_STYLES + keys.delete ['value', 'type'] + str << "<r><rPr>" + keys.each do |key| + case key + when 'font_name' + str << "<rFont val='"<< @font_name << "'/>" + when 'color' + str << data[key].to_xml_string + else + "<" << key.to_s << " val='" << data[key].to_s << "'/>" + end + end + str << "</rPr>" << "<t>" << value.to_s << "</t></r>" + else + str << "<t>" << value.to_s << "</t>" + end + str + end # builds an xml text run based on this cells attributes. This is extracted from to_xml so that shared strings can use it. # @param [Nokogiri::XML::Builder] xml The document builder instance this output will be added to. # @return [String] the xml for this cell's text run @@ -294,11 +333,37 @@ module Axlsx # Serializes the cell # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] xml text for the cell + def to_xml_string + case @type + when :string + #parse formula + if @value.start_with?('=') + '<c r="' << r << '" t="str" s="' << @style.to_s << '"><f>' << value.to_s.gsub('=', '') << '</f></c>' + else + #parse shared + if @ssti + '<c r="' << r << '" t="s" s="' << @style.to_s << '"><v>' << ssti << '</v></c>' + else + '<c r="' << r << '" t="inlineStr" s="' << @style.to_s << '"><is>' << run_xml_string << '</is></c>' + end + end + when :date + # TODO: See if this is subject to the same restriction as Time below + '<c r="' << r << '" s="' << @style.to_s << '"><v>' << DateTimeConverter::date_to_serial(@value).to_s << '</v></c>' + when :time + '<c r="' << r << '" s="' << @style.to_s << '"><v>' << DateTimeConverter::time_to_serial(@value).to_s << '</v></c>' + when :boolean + '<c r="' << r << '" t="b" s="' << @style.to_s << '"><v>' << @value.to_s << '</v></c>' + else + '<c r="' << r << '" s="' << @style.to_s << '"><v>' << @value.to_s << '</v></c>' + end + end + def to_xml(xml) if @type == :string #parse formula if @value.start_with?('=') - xml.c(:r => r, :t=>:str, :s=>style) { + xml.c(:r => r, :s=>style, :t=>:str) { xml.f @value.to_s.gsub('=', '') } else @@ -330,6 +395,14 @@ module Axlsx private + # Utility method for setting inline style attributes + def set_run_style( validator, attr, value) + return unless INLINE_STYLES.include?(attr.to_s) + Axlsx.send(validator, value) unless validator == nil + self.instance_variable_set :"@#{attr.to_s}", value + @is_text_run = true + end + # @see ssti def ssti=(v) Axlsx::validate_unsigned_int(v) diff --git a/lib/axlsx/workbook/worksheet/page_margins.rb b/lib/axlsx/workbook/worksheet/page_margins.rb index f41e3426..1c456f2a 100644 --- a/lib/axlsx/workbook/worksheet/page_margins.rb +++ b/lib/axlsx/workbook/worksheet/page_margins.rb @@ -12,13 +12,13 @@ module Axlsx # Default left and right margin (in inches) DEFAULT_LEFT_RIGHT = 0.75 - + # Default top and bottom margins (in inches) DEFAULT_TOP_BOTTOM = 1.00 - + # Default header and footer margins (in inches) DEFAULT_HEADER_FOOTER = 0.50 - + # Left margin (in inches) # @return [Float] attr_reader :left @@ -26,23 +26,23 @@ module Axlsx # Right margin (in inches) # @return [Float] attr_reader :right - + # Top margin (in inches) # @return [Float] attr_reader :top - + # Bottom margin (in inches) # @return [Float] attr_reader :bottom - + # Header margin (in inches) # @return [Float] attr_reader :header - + # Footer margin (in inches) # @return [Float] attr_reader :footer - + # Creates a new PageMargins object # @option options [Numeric] left The left margin in inches # @option options [Numeric] right The right margin in inches @@ -60,7 +60,7 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end - + # Set some or all margins at once. # @param [Hash] margins the margins to set (possible keys are :left, :right, :top, :bottom, :header and :footer). def set(margins) @@ -69,7 +69,7 @@ module Axlsx send("#{k}=", v) end end - + # @see left def left=(v); Axlsx::validate_unsigned_numeric(v); @left = v end # @see right @@ -83,6 +83,9 @@ module Axlsx # @see footer def footer=(v); Axlsx::validate_unsigned_numeric(v); @footer = v end + def to_xml_string + "<pageMargins left='%s' right='%s' top='%s' bottom='%s' header='%s' footer='%s'/>" % [left, right, top, bottom, header, footer] + end # Serializes the page margins element # @note For compatibility, this is a noop unless custom margins have been specified. # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb index bb6a92a8..30101a28 100644 --- a/lib/axlsx/workbook/worksheet/row.rb +++ b/lib/axlsx/workbook/worksheet/row.rb @@ -59,6 +59,13 @@ module Axlsx worksheet.rows.index(self) end + def to_xml_string + if custom_height? + '<row r="' << (index+1).to_s << '" customHeight="1" ht="' << height.to_s << '">' << @cells.map { |cell| cell.to_xml_string }.join << '</row>' + else + '<row r="' << (index+1).to_s << '">' << (@cells.map { |cell| cell.to_xml_string }.join) << '</row>' + end + end # Serializes the row # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 3f651078..64fae531 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -168,27 +168,6 @@ module Axlsx @fit_to_page = v 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 - 1] 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 # returns the column and row index for a named based cell # @param [String] name The cell or cell range to return. "A1" will return the first cell of the first row. @@ -381,6 +360,7 @@ module Axlsx chart end + # needs documentation def add_table(ref, options={}) table = Table.new(ref, self, options) @tables << table @@ -397,14 +377,40 @@ module Axlsx image end + def to_xml_string + str = "<worksheet xmlns=\"%s\" xmlns:r=\"%s\">" % [XML_NS, XML_NS_R] + str.concat "<sheetPr><pageSetUpPr fitToPage=\"%s\"></pageSetUpPr></sheetPr>" % fit_to_page if fit_to_page + str.concat "<dimension ref=\"%s\"></dimension>" % dimension unless rows.size == 0 + str.concat "<sheetViews><sheetView tabSelected='%s' workbookViewId='0' showGridLines='%s'><selection activeCell=\"A1\" sqref=\"A1\"/></sheetView></sheetViews>" % [@selected, show_gridlines] + + if @auto_fit_data.size > 0 + str.concat "<cols>" + @auto_fit_data.each_with_index do |col, index| + min_max = index+1 + str.concat "<col min='%s' max='%s' width='%s' customWidth='1'></col>" % [min_max, min_max, auto_width(col)] + end + str.concat '</cols>' + end + + str.concat "<sheetData>%s</sheetData>" % @rows.map { |obj| obj.to_xml_string }.join + str.concat page_margins.to_xml_string if @page_margins + str.concat "<autoFilter ref='%s'></autoFilter>" % @auto_filter if @auto_filter + str.concat "<mergeCells count='%s'>%s</mergeCells>" % [@merged_cells.size, @merged_cells.reduce('') { |memo, obj| "<mergeCell ref='%s'></mergeCell>" % obj } ] unless @merged_cells.empty? + str.concat "<drawing r:id='rId1'></drawing>" if @drawing + unless @tables.empty? + str.concat "<tableParts count='%s'>%s</tableParts>" % [@tables.size, @tables.reduce('') { |memo, obj| memo += "<tablePart r:id='%s'/>" % obj.rId }] + end + str + '</worksheet>' + end + # Serializes the worksheet document # @return [String] def to_xml builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| xml.worksheet(:xmlns => XML_NS, :'xmlns:r' => XML_NS_R) { - xml.sheetPr { - xml.pageSetUpPr :fitToPage => fit_to_page if fit_to_page + xml.sheetPr { + xml.pageSetUpPr :fitToPage => fit_to_page if fit_to_page } # another patch for the folks at rubyXL as thier parser depends on this optional element. xml.dimension :ref=>dimension unless rows.size == 0 @@ -456,6 +462,29 @@ module Axlsx 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 @@ -484,6 +513,7 @@ module Axlsx next if width == :ignore || (item.value.is_a?(String) && item.value.start_with?('=')) # make sure we can turn that fixed with off! col[:fixed] = nil if width == :auto + next unless self.workbook.use_autowidth cell_xf = cellXfs[item.style] font = fonts[cell_xf.fontId || 0] diff --git a/test/benchmark.rb b/test/benchmark.rb new file mode 100644 index 00000000..bb9a7b7d --- /dev/null +++ b/test/benchmark.rb @@ -0,0 +1,81 @@ +#!/usr/bin/env ruby -s +# -*- coding: utf-8 -*- +$:.unshift "#{File.dirname(__FILE__)}/../lib" +require 'axlsx' +require 'csv' + +require 'benchmark' +row = [] +input = (32..126).to_a.pack('U*').chars.to_a +20.times { row << input.shuffle.join} +times = 1000 +Benchmark.bm(100) do |x| + # No Autowidth + x.report('axlsx_noautowidth') { + + p = Axlsx::Package.new + p.use_autowidth = false + wb = p.workbook + + #A Simple Workbook + + wb.add_worksheet do |sheet| + times.times do + sheet << row + end + end + p.serialize("example.xlsx") + } + + x.report('axlsx') { + p = Axlsx::Package.new + wb = p.workbook + + #A Simple Workbook + + wb.add_worksheet do |sheet| + times.times do + sheet << row + end + end + p.serialize("example.xlsx") + } + + x.report('axlsx_shared') { + p = Axlsx::Package.new + wb = p.workbook + + #A Simple Workbook + + wb.add_worksheet do |sheet| + times.times do + sheet << row + end + end + p.use_shared_strings = true + p.serialize("example.xlsx") + } + + x.report('axlsx_stream') { + p = Axlsx::Package.new + wb = p.workbook + + #A Simple Workbook + + wb.add_worksheet do |sheet| + times.times do + sheet << row + end + end + + s = p.to_stream() + File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) } + } + x.report('csv') { + CSV.open("example.csv", "wb") do |csv| + times.times do + csv << row + end + end + } +end diff --git a/test/content_type/tc_content_type.rb b/test/content_type/tc_content_type.rb index 5757b980..79141748 100644 --- a/test/content_type/tc_content_type.rb +++ b/test/content_type/tc_content_type.rb @@ -1,7 +1,5 @@ # encoding: UTF-8 - -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestContentType < Test::Unit::TestCase def setup diff --git a/test/content_type/tc_default.rb b/test/content_type/tc_default.rb index 90c0934e..e5245b38 100644 --- a/test/content_type/tc_default.rb +++ b/test/content_type/tc_default.rb @@ -1,10 +1,8 @@ # encoding: UTF-8 - -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestDefault < Test::Unit::TestCase - def setup + def setup end def teardown end @@ -19,7 +17,7 @@ class TestDefault < Test::Unit::TestCase def test_content_type_restriction assert_raise(ArgumentError, "raises argument error if invlalid ContentType is") { Axlsx::Default.new :ContentType=>"asdf" } end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::CONTENT_TYPES_XSD)) type = Axlsx::Default.new :Extension=>"xml", :ContentType=>Axlsx::XML_CT @@ -35,7 +33,7 @@ class TestDefault < Test::Unit::TestCase errors << error end assert_equal(errors.size, 0, "[Content Types].xml Invalid" + errors.map{ |e| e.message }.to_s) - + end diff --git a/test/content_type/tc_override.rb b/test/content_type/tc_override.rb index 911ec649..5005d12d 100644 --- a/test/content_type/tc_override.rb +++ b/test/content_type/tc_override.rb @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestOverride < Test::Unit::TestCase - def setup + def setup end def teardown end @@ -18,7 +17,7 @@ class TestOverride < Test::Unit::TestCase def test_content_type_restriction assert_raise(ArgumentError, "requires known content type") { Axlsx::Override.new :ContentType=>"asdf" } end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::CONTENT_TYPES_XSD)) type = Axlsx::Override.new :PartName=>"somechart.xml", :ContentType=>Axlsx::CHART_CT @@ -34,7 +33,7 @@ class TestOverride < Test::Unit::TestCase errors << error end assert_equal(errors.size, 0, "Override content type caused invalid content_type doc" + errors.map{ |e| e.message }.to_s) - + end diff --git a/test/doc_props/tc_app.rb b/test/doc_props/tc_app.rb index 546896a0..31c87c41 100644 --- a/test/doc_props/tc_app.rb +++ b/test/doc_props/tc_app.rb @@ -1,12 +1,11 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestApp < Test::Unit::TestCase - def setup + def setup end def teardown end - + def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::APP_XSD)) doc = Nokogiri::XML(Axlsx::App.new.to_xml) diff --git a/test/doc_props/tc_core.rb b/test/doc_props/tc_core.rb index 5dd490a8..5e75d812 100644 --- a/test/doc_props/tc_core.rb +++ b/test/doc_props/tc_core.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCore < Test::Unit::TestCase - - def setup + + def setup @core = Axlsx::Core.new @doc = Nokogiri::XML(@core.to_xml) end @@ -21,7 +20,7 @@ class TestCore < Test::Unit::TestCase def test_populates_created assert_equal(@doc.xpath('//dcterms:created').text, Time.now.strftime('%Y-%m-%dT%H:%M:%S'), "dcterms:created incorrect") end - + def test_populates_default_name assert_equal(@doc.xpath('//dc:creator').text, "axlsx", "Default name not populated") end diff --git a/test/drawing/tc_axis.rb b/test/drawing/tc_axis.rb index cad6b07a..234eba9c 100644 --- a/test/drawing/tc_axis.rb +++ b/test/drawing/tc_axis.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestAxis < Test::Unit::TestCase - def setup + def setup @axis = Axlsx::Axis.new 12345, 54321 end def teardown diff --git a/test/drawing/tc_bar_3D_chart.rb b/test/drawing/tc_bar_3D_chart.rb index ff01464c..93997730 100644 --- a/test/drawing/tc_bar_3D_chart.rb +++ b/test/drawing/tc_bar_3D_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestBar3DChart < Test::Unit::TestCase @@ -13,7 +12,7 @@ class TestBar3DChart < Test::Unit::TestCase def teardown end - def test_initialization + def test_initialization assert_equal(@chart.grouping, :clustered, "grouping defualt incorrect") assert_equal(@chart.series_type, Axlsx::BarSeries, "series type incorrect") assert_equal(@chart.barDir, :bar, " bar direction incorrect") @@ -61,6 +60,6 @@ class TestBar3DChart < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_bar_series.rb b/test/drawing/tc_bar_series.rb index afd6004a..b07b42a4 100644 --- a/test/drawing/tc_bar_series.rb +++ b/test/drawing/tc_bar_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestBarSeries < Test::Unit::TestCase @@ -9,12 +8,12 @@ class TestBarSeries < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Bar3DChart, :title => "fishery" @series = chart.add_series :data=>[0,1,2], :labels=>["zero", "one", "two"], :title=>"bob" end - + def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") - assert_equal(@series.shape, :box, "series shape has been applied") + assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") + assert_equal(@series.shape, :box, "series shape has been applied") end def test_data diff --git a/test/drawing/tc_cat_axis.rb b/test/drawing/tc_cat_axis.rb index 23ed2eca..a4f25a1c 100644 --- a/test/drawing/tc_cat_axis.rb +++ b/test/drawing/tc_cat_axis.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCatAxis < Test::Unit::TestCase - def setup + def setup @axis = Axlsx::CatAxis.new 12345, 54321 end def teardown diff --git a/test/drawing/tc_cat_axis_data.rb b/test/drawing/tc_cat_axis_data.rb index e8a71fb1..b176c0f0 100644 --- a/test/drawing/tc_cat_axis_data.rb +++ b/test/drawing/tc_cat_axis_data.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCatAxisData < Test::Unit::TestCase @@ -9,7 +8,7 @@ class TestCatAxisData < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Bar3DChart @series = chart.add_series :labels=>["zero", "one", "two"] end - + def test_initialize assert(@series.labels.is_a?Axlsx::SimpleTypedList) assert_equal(@series.labels, ["zero", "one", "two"]) diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 474266d5..c0e938cd 100644 --- a/test/drawing/tc_chart.rb +++ b/test/drawing/tc_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestChart < Test::Unit::TestCase @@ -16,7 +15,7 @@ class TestChart < Test::Unit::TestCase def test_initialization assert_equal(@p.workbook.charts.last,@chart, "the chart is in the workbook") assert_equal(@chart.title.text, "fishery", "the title option has been applied") - assert((@chart.series.is_a?(Axlsx::SimpleTypedList) && @chart.series.empty?), "The series is initialized and empty") + assert((@chart.series.is_a?(Axlsx::SimpleTypedList) && @chart.series.empty?), "The series is initialized and empty") end def test_title @@ -47,18 +46,18 @@ class TestChart < Test::Unit::TestCase assert_equal(@chart.graphic_frame.anchor.to.row, 90) end - def test_add_series + def test_add_series s = @chart.add_series :data=>[0,1,2,3], :labels => ["one", 1, "anything"], :title=>"bob" assert_equal(@chart.series.last, s, "series has been added to chart series collection") assert_equal(s.title.text, "bob", "series title has been applied") assert_equal(s.data, [0,1,2,3], "data option applied") - assert_equal(s.labels, ["one",1,"anything"], "labels option applied") - end - + assert_equal(s.labels, ["one",1,"anything"], "labels option applied") + end + def test_pn assert_equal(@chart.pn, "charts/chart1.xml") end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) doc = Nokogiri::XML(@chart.to_xml) @@ -68,6 +67,6 @@ class TestChart < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_drawing.rb b/test/drawing/tc_drawing.rb index 7dd21c32..ac2dddbe 100644 --- a/test/drawing/tc_drawing.rb +++ b/test/drawing/tc_drawing.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestDrawing < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet @@ -24,14 +23,14 @@ class TestDrawing < Test::Unit::TestCase assert_equal([anchor.to.row, anchor.to.col], [1,1], "options for start at are applied") assert_equal(chart.title.text, "bob", "option for title is applied") end - + def test_add_image src = File.dirname(__FILE__) + "/../../examples/image1.jpeg" image = @ws.add_image(:image_src => src, :start_at=>[0,0], :width=>600, :height=>400) assert(@ws.drawing.anchors.last.is_a?(Axlsx::OneCellAnchor)) assert(image.is_a?(Axlsx::Pic)) assert_equal(600, image.width) - assert_equal(400, image.height) + assert_equal(400, image.height) end def test_charts @@ -39,7 +38,7 @@ class TestDrawing < Test::Unit::TestCase chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1]) assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"nancy", :start_at=>[1,5], :end_at=>[5,10]) - assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") + assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") end def test_pn @@ -53,7 +52,7 @@ class TestDrawing < Test::Unit::TestCase def test_rId assert_equal(@ws.drawing.rId, "rId1") end - + def test_index assert_equal(@ws.drawing.index, @ws.workbook.drawings.index(@ws.drawing)) end diff --git a/test/drawing/tc_graphic_frame.rb b/test/drawing/tc_graphic_frame.rb index 30017d2b..fe0b901d 100644 --- a/test/drawing/tc_graphic_frame.rb +++ b/test/drawing/tc_graphic_frame.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestGraphicFrame < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet @chart = @ws.add_chart Axlsx::Chart @@ -20,7 +19,7 @@ class TestGraphicFrame < Test::Unit::TestCase def test_rId assert_equal(@frame.rId, "rId1") chart = @ws.add_chart Axlsx::Chart - assert_equal(chart.graphic_frame.rId, "rId2") + assert_equal(chart.graphic_frame.rId, "rId2") end end diff --git a/test/drawing/tc_hyperlink.rb b/test/drawing/tc_hyperlink.rb index a5564c96..251c7967 100644 --- a/test/drawing/tc_hyperlink.rb +++ b/test/drawing/tc_hyperlink.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestHyperlink < Test::Unit::TestCase @@ -66,5 +65,5 @@ class TestHyperlink < Test::Unit::TestCase assert_raise(ArgumentError) {@hyperlink.history = "bob"} assert_equal(@hyperlink.history, false ) end - + end diff --git a/test/drawing/tc_line_3d_chart.rb b/test/drawing/tc_line_3d_chart.rb index 5c4a3f26..22dd6158 100644 --- a/test/drawing/tc_line_3d_chart.rb +++ b/test/drawing/tc_line_3d_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestLine3DChart < Test::Unit::TestCase @@ -13,7 +12,7 @@ class TestLine3DChart < Test::Unit::TestCase def teardown end - def test_initialization + def test_initialization assert_equal(@chart.grouping, :standard, "grouping defualt incorrect") assert_equal(@chart.series_type, Axlsx::LineSeries, "series type incorrect") assert(@chart.catAxis.is_a?(Axlsx::CatAxis), "category axis not created") @@ -43,6 +42,6 @@ class TestLine3DChart < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_line_series.rb b/test/drawing/tc_line_series.rb index 602e7770..a2417549 100644 --- a/test/drawing/tc_line_series.rb +++ b/test/drawing/tc_line_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestLineSeries < Test::Unit::TestCase @@ -9,11 +8,11 @@ class TestLineSeries < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Line3DChart, :title => "fishery" @series = chart.add_series :data=>[0,1,2], :labels=>["zero", "one", "two"], :title=>"bob" end - + def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") + assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") end def test_data diff --git a/test/drawing/tc_marker.rb b/test/drawing/tc_marker.rb index 8bd9983f..05137ae7 100644 --- a/test/drawing/tc_marker.rb +++ b/test/drawing/tc_marker.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestMarker < Test::Unit::TestCase - def setup + def setup @marker = Axlsx::Marker.new end @@ -41,5 +40,5 @@ class TestMarker < Test::Unit::TestCase assert_equal(@marker.col, 5) assert_equal(@marker.row, 10) end - + end diff --git a/test/drawing/tc_one_cell_anchor.rb b/test/drawing/tc_one_cell_anchor.rb index cab22840..cba66112 100644 --- a/test/drawing/tc_one_cell_anchor.rb +++ b/test/drawing/tc_one_cell_anchor.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestOneCellAnchor < Test::Unit::TestCase - def setup + def setup @p = Axlsx::Package.new @ws = @p.workbook.add_worksheet @test_img = File.dirname(__FILE__) + "/../../examples/image1.jpeg" @@ -22,7 +21,7 @@ class TestOneCellAnchor < Test::Unit::TestCase end def test_from - assert(@anchor.from.is_a?(Axlsx::Marker)) + assert(@anchor.from.is_a?(Axlsx::Marker)) end def test_object @@ -36,13 +35,13 @@ class TestOneCellAnchor < Test::Unit::TestCase def test_width assert_raise(ArgumentError) { @anchor.width = "a" } assert_nothing_raised { @anchor.width = 600 } - assert_equal(@anchor.width, 600) + assert_equal(@anchor.width, 600) end def test_height assert_raise(ArgumentError) { @anchor.height = "a" } assert_nothing_raised { @anchor.height = 400 } - assert_equal(400, @anchor.height) + assert_equal(400, @anchor.height) end def test_ext @@ -63,5 +62,5 @@ class TestOneCellAnchor < Test::Unit::TestCase assert_equal(2, i.anchor.from.row) assert_equal(@test_img, i.image_src) end - + end diff --git a/test/drawing/tc_pic.rb b/test/drawing/tc_pic.rb index 9e4d62e6..83ed4e1d 100644 --- a/test/drawing/tc_pic.rb +++ b/test/drawing/tc_pic.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPic < Test::Unit::TestCase @@ -17,7 +16,7 @@ class TestPic < Test::Unit::TestCase assert_equal(@p.workbook.images.first, @image) assert_equal(@image.image_src, @test_img) end - + def test_hyperlink assert_equal(@image.hyperlink, nil) @image.hyperlink = "http://axlsx.blogspot.com" @@ -40,13 +39,13 @@ class TestPic < Test::Unit::TestCase def test_width assert_raise(ArgumentError) { @image.width = "a" } assert_nothing_raised { @image.width = 600 } - assert_equal(@image.width, 600) + assert_equal(@image.width, 600) end def test_height assert_raise(ArgumentError) { @image.height = "a" } assert_nothing_raised { @image.height = 600 } - assert_equal(600, @image.height) + assert_equal(600, @image.height) end def test_image_src @@ -62,7 +61,7 @@ class TestPic < Test::Unit::TestCase assert_nothing_raised { @image.descr = "test" } assert_equal(@image.descr, "test") end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) doc = Nokogiri::XML(@image.anchor.drawing.to_xml) @@ -72,6 +71,6 @@ class TestPic < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_picture_locking.rb b/test/drawing/tc_picture_locking.rb index 15b89e22..4c531d1a 100644 --- a/test/drawing/tc_picture_locking.rb +++ b/test/drawing/tc_picture_locking.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPictureLocking < Test::Unit::TestCase - def setup + def setup @item = Axlsx::PictureLocking.new end def teardown diff --git a/test/drawing/tc_pie_3D_chart.rb b/test/drawing/tc_pie_3D_chart.rb index e8a42962..76bf49c5 100644 --- a/test/drawing/tc_pie_3D_chart.rb +++ b/test/drawing/tc_pie_3D_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPie3DChart < Test::Unit::TestCase @@ -18,7 +17,7 @@ class TestPie3DChart < Test::Unit::TestCase assert_equal(@chart.view3D.perspective, 30, "view 3d default perspective incorrect") assert_equal(@chart.series_type, Axlsx::PieSeries, "series type incorrect") end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) doc = Nokogiri::XML(@chart.to_xml) @@ -28,6 +27,6 @@ class TestPie3DChart < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_pie_series.rb b/test/drawing/tc_pie_series.rb index 53799c72..66c8da81 100644 --- a/test/drawing/tc_pie_series.rb +++ b/test/drawing/tc_pie_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPieSeries < Test::Unit::TestCase @@ -9,12 +8,12 @@ class TestPieSeries < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Pie3DChart, :title => "fishery" @series = chart.add_series :data=>[0,1,2], :labels=>["zero", "one", "two"], :title=>"bob" end - + def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") - assert_equal(@series.explosion, nil, "series shape has been applied") + assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") + assert_equal(@series.explosion, nil, "series shape has been applied") end def test_data diff --git a/test/drawing/tc_scaling.rb b/test/drawing/tc_scaling.rb index 3bf39c53..01e21a5d 100644 --- a/test/drawing/tc_scaling.rb +++ b/test/drawing/tc_scaling.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestScaling < Test::Unit::TestCase - def setup + def setup @scaling = Axlsx::Scaling.new end @@ -33,5 +32,5 @@ class TestScaling < Test::Unit::TestCase assert_raise(ArgumentError) { @scaling.min = 1} assert_nothing_raised {@scaling.min = 10.5} end - + end diff --git a/test/drawing/tc_scatter_chart.rb b/test/drawing/tc_scatter_chart.rb index 478fbf49..367cf56d 100644 --- a/test/drawing/tc_scatter_chart.rb +++ b/test/drawing/tc_scatter_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestScatterChart < Test::Unit::TestCase def setup diff --git a/test/drawing/tc_scatter_series.rb b/test/drawing/tc_scatter_series.rb index 37ef4949..a815c9e3 100644 --- a/test/drawing/tc_scatter_series.rb +++ b/test/drawing/tc_scatter_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestScatterSeries < Test::Unit::TestCase diff --git a/test/drawing/tc_ser_axis.rb b/test/drawing/tc_ser_axis.rb index 3d744907..2f30ceb8 100644 --- a/test/drawing/tc_ser_axis.rb +++ b/test/drawing/tc_ser_axis.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSerAxis < Test::Unit::TestCase - def setup + def setup @axis = Axlsx::SerAxis.new 12345, 54321 end def teardown diff --git a/test/drawing/tc_series.rb b/test/drawing/tc_series.rb index cbbd93d4..6b83015a 100644 --- a/test/drawing/tc_series.rb +++ b/test/drawing/tc_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSeries < Test::Unit::TestCase @@ -9,16 +8,16 @@ class TestSeries < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Chart, :title => "fishery" @series = chart.add_series :title=>"bob" end - + def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") assert_equal(@series.order, @series.index, "order is index by default") - assert_equal(@series.index, @series.chart.series.index(@series), "index is applied") + assert_equal(@series.index, @series.chart.series.index(@series), "index is applied") end def test_order @series.order = 2 assert_equal(@series.order, 2) end - + end diff --git a/test/drawing/tc_series_title.rb b/test/drawing/tc_series_title.rb index f10370b7..90427e9c 100644 --- a/test/drawing/tc_series_title.rb +++ b/test/drawing/tc_series_title.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSeriesTitle < Test::Unit::TestCase - def setup + def setup @p = Axlsx::Package.new ws = @p.workbook.add_worksheet @row = ws.add_row ["one", 1, Time.now] @@ -21,7 +20,7 @@ class TestSeriesTitle < Test::Unit::TestCase def test_text assert_raise(ArgumentError, "text must be a string") { @title.text = 123 } @title.cell = @row.cells.first - @title.text = "bob" + @title.text = "bob" assert(@title.cell == nil, "setting title with text clears the cell") end @@ -30,5 +29,5 @@ class TestSeriesTitle < Test::Unit::TestCase @title.cell = @row.cells.first assert(@title.text == "one") end - + end diff --git a/test/drawing/tc_title.rb b/test/drawing/tc_title.rb index 7fb6044b..e53c260a 100644 --- a/test/drawing/tc_title.rb +++ b/test/drawing/tc_title.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTitle < Test::Unit::TestCase - def setup + def setup @p = Axlsx::Package.new ws = @p.workbook.add_worksheet @row = ws.add_row ["one", 1, Time.now] @@ -21,7 +20,7 @@ class TestTitle < Test::Unit::TestCase def test_text assert_raise(ArgumentError, "text must be a string") { @title.text = 123 } @title.cell = @row.cells.first - @title.text = "bob" + @title.text = "bob" assert(@title.cell == nil, "setting title with text clears the cell") end @@ -30,5 +29,5 @@ class TestTitle < Test::Unit::TestCase @title.cell = @row.cells.first assert(@title.text == "one") end - + end diff --git a/test/drawing/tc_two_cell_anchor.rb b/test/drawing/tc_two_cell_anchor.rb index 2d87b6a7..e22acce9 100644 --- a/test/drawing/tc_two_cell_anchor.rb +++ b/test/drawing/tc_two_cell_anchor.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTwoCellAnchor < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet row = @ws.add_row ["one", 1, Time.now] @@ -22,7 +21,7 @@ class TestTwoCellAnchor < Test::Unit::TestCase assert(@anchor.to.row == 10) end - + def test_options assert_raise(ArgumentError, 'invalid start_at') { @ws.add_chart Axlsx::Chart, :start_at=>[1] } assert_raise(ArgumentError, 'invalid end_at') { @ws.add_chart Axlsx::Chart, :start_at=>[1,2], :end_at => ["a", 4] } @@ -34,5 +33,5 @@ class TestTwoCellAnchor < Test::Unit::TestCase assert_equal(a.graphic_frame.anchor.to.col, 90) assert_equal(a.graphic_frame.anchor.to.row, 45) end - + end diff --git a/test/drawing/tc_val_axis.rb b/test/drawing/tc_val_axis.rb index 4bd8b883..f3f55421 100644 --- a/test/drawing/tc_val_axis.rb +++ b/test/drawing/tc_val_axis.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestValAxis < Test::Unit::TestCase - def setup + def setup @axis = Axlsx::ValAxis.new 12345, 54321 end def teardown diff --git a/test/drawing/tc_val_axis_data.rb b/test/drawing/tc_val_axis_data.rb index cb39176d..b49cc7c0 100644 --- a/test/drawing/tc_val_axis_data.rb +++ b/test/drawing/tc_val_axis_data.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestValAxisData < Test::Unit::TestCase @@ -9,7 +8,7 @@ class TestValAxisData < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Line3DChart @series = chart.add_series :data=>[0,1,2] end - + def test_initialize assert(@series.data.is_a?Axlsx::SimpleTypedList) assert_equal(@series.data, [0,1,2]) diff --git a/test/drawing/tc_view_3D.rb b/test/drawing/tc_view_3D.rb index 40d8982f..e9626c75 100644 --- a/test/drawing/tc_view_3D.rb +++ b/test/drawing/tc_view_3D.rb @@ -1,14 +1,13 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestView3D < Test::Unit::TestCase - def setup + def setup @view = Axlsx::View3D.new end def teardown end - + def test_options v = Axlsx::View3D.new :rotX => 10, :rotY => 5, :hPercent => "30%", :depthPercent => "45%", :rAngAx => false, :perspective => 10 assert_equal(v.rotX, 10) @@ -18,7 +17,7 @@ class TestView3D < Test::Unit::TestCase assert_equal(v.rAngAx, false) assert_equal(v.perspective, 10) end - + def test_rotX assert_raise(ArgumentError) {@view.rotX = "bob"} assert_nothing_raised {@view.rotX = -90} @@ -44,12 +43,12 @@ class TestView3D < Test::Unit::TestCase assert_raise(ArgumentError) {@view.rAngAx = "bob"} assert_nothing_raised {@view.rAngAx = true} end - + def test_perspective assert_raise(ArgumentError) {@view.perspective = "bob"} assert_nothing_raised {@view.perspective = 30} end - + end diff --git a/test/profile.rb b/test/profile.rb new file mode 100644 index 00000000..97d2e2bc --- /dev/null +++ b/test/profile.rb @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby -s +# -*- coding: utf-8 -*- + +# Usage: +# > ruby test/profile.rb +# > pprof.rb --gif /tmp/axlsx_noautowidth > /tmp/axlsx_noautowidth.gif +# > open /tmp/axlsx_noautowidth.gif + +$:.unshift "#{File.dirname(__FILE__)}/../lib" +require 'axlsx' +require 'csv' + +# require 'benchmark' +require 'perftools' +row = [] +input = (32..126).to_a.pack('U*').chars.to_a +20.times { row << input.shuffle.join} +times = 1000 + +PerfTools::CpuProfiler.start("/tmp/axlsx_noautowidth") do + p = Axlsx::Package.new + p.use_autowidth = false + wb = p.workbook + + #A Simple Workbook + + wb.add_worksheet do |sheet| + times.times do + sheet << row + end + end + p.serialize("example.xlsx") +end diff --git a/test/rels/tc_relationship.rb b/test/rels/tc_relationship.rb index 60ac5021..98f21b0b 100644 --- a/test/rels/tc_relationship.rb +++ b/test/rels/tc_relationship.rb @@ -1,12 +1,11 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestRelationships < Test::Unit::TestCase - def setup + def setup + end + + def teardown end - - def teardown - end def test_type assert_raise(ArgumentError) { Axlsx::Relationship.new 'type', 'target' } diff --git a/test/rels/tc_relationships.rb b/test/rels/tc_relationships.rb index c6b4860d..9a76d1e5 100644 --- a/test/rels/tc_relationships.rb +++ b/test/rels/tc_relationships.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestRelationships < Test::Unit::TestCase @@ -21,7 +20,7 @@ class TestRelationships < Test::Unit::TestCase errors << error end - assert(errors.size == 0) + assert(errors.size == 0) end end diff --git a/test/stylesheet/tc_border.rb b/test/stylesheet/tc_border.rb index ba268e07..ae191dd0 100644 --- a/test/stylesheet/tc_border.rb +++ b/test/stylesheet/tc_border.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestBorder < Test::Unit::TestCase - def setup + def setup @b = Axlsx::Border.new end def teardown @@ -10,7 +9,7 @@ class TestBorder < Test::Unit::TestCase def test_initialiation assert_equal(@b.diagonalUp, nil) assert_equal(@b.diagonalDown, nil) - assert_equal(@b.outline, nil) + assert_equal(@b.outline, nil) assert(@b.prs.is_a?(Axlsx::SimpleTypedList)) end diff --git a/test/stylesheet/tc_border_pr.rb b/test/stylesheet/tc_border_pr.rb index 18388186..dc0fa9da 100644 --- a/test/stylesheet/tc_border_pr.rb +++ b/test/stylesheet/tc_border_pr.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestBorderPr < Test::Unit::TestCase - def setup + def setup @bpr = Axlsx::BorderPr.new end def teardown @@ -10,7 +9,7 @@ class TestBorderPr < Test::Unit::TestCase def test_initialiation assert_equal(@bpr.color, nil) assert_equal(@bpr.style, nil) - assert_equal(@bpr.name, nil) + assert_equal(@bpr.name, nil) end def test_color diff --git a/test/stylesheet/tc_cell_alignment.rb b/test/stylesheet/tc_cell_alignment.rb index 66f95518..6b8cd5cd 100644 --- a/test/stylesheet/tc_cell_alignment.rb +++ b/test/stylesheet/tc_cell_alignment.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCellAlignment < Test::Unit::TestCase - def setup + def setup @item = Axlsx::CellAlignment.new end @@ -16,8 +15,8 @@ class TestCellAlignment < Test::Unit::TestCase assert_equal(@item.justifyLastLine, nil) assert_equal(@item.shrinkToFit, nil) assert_equal(@item.readingOrder, nil) - options = { :horizontal => :left, :vertical => :top, :textRotation => 3, - :wrapText => true, :indent => 2, :relativeIndent => 5, + options = { :horizontal => :left, :vertical => :top, :textRotation => 3, + :wrapText => true, :indent => 2, :relativeIndent => 5, :justifyLastLine => true, :shrinkToFit => true, :readingOrder => 2 } ca = Axlsx::CellAlignment.new options options.each do |key, value| diff --git a/test/stylesheet/tc_cell_protection.rb b/test/stylesheet/tc_cell_protection.rb index 665ec761..b2161fa6 100644 --- a/test/stylesheet/tc_cell_protection.rb +++ b/test/stylesheet/tc_cell_protection.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCellProtection < Test::Unit::TestCase - def setup + def setup @item = Axlsx::CellProtection.new end diff --git a/test/stylesheet/tc_cell_style.rb b/test/stylesheet/tc_cell_style.rb index 878c7aab..8700d8c3 100644 --- a/test/stylesheet/tc_cell_style.rb +++ b/test/stylesheet/tc_cell_style.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCellStyle < Test::Unit::TestCase - def setup + def setup @item = Axlsx::CellStyle.new end diff --git a/test/stylesheet/tc_color.rb b/test/stylesheet/tc_color.rb index 55f69448..affa2227 100644 --- a/test/stylesheet/tc_color.rb +++ b/test/stylesheet/tc_color.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestColor < Test::Unit::TestCase - def setup + def setup @item = Axlsx::Color.new end diff --git a/test/stylesheet/tc_fill.rb b/test/stylesheet/tc_fill.rb index 090a395b..9aadef29 100644 --- a/test/stylesheet/tc_fill.rb +++ b/test/stylesheet/tc_fill.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestFill < Test::Unit::TestCase diff --git a/test/stylesheet/tc_font.rb b/test/stylesheet/tc_font.rb index 141c285e..7b3da25a 100644 --- a/test/stylesheet/tc_font.rb +++ b/test/stylesheet/tc_font.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestFont < Test::Unit::TestCase @@ -29,7 +28,7 @@ class TestFont < Test::Unit::TestCase - # def name=(v) Axlsx::validate_string v; @name = v end + # def name=(v) Axlsx::validate_string v; @name = v end def test_name assert_raise(ArgumentError) { @item.name = 7 } assert_nothing_raised { @item.name = "bob" } @@ -49,7 +48,7 @@ class TestFont < Test::Unit::TestCase assert_equal(@item.family, 5) end - # def b=(v) Axlsx::validate_boolean v; @b = v end + # def b=(v) Axlsx::validate_boolean v; @b = v end def test_b assert_raise(ArgumentError) { @item.b = -7 } assert_nothing_raised { @item.b = true } @@ -62,7 +61,7 @@ class TestFont < Test::Unit::TestCase assert_nothing_raised { @item.i = true } assert_equal(@item.i, true) end - + # def u=(v) Axlsx::validate_boolean v; @u = v end def test_u assert_raise(ArgumentError) { @item.u = -7 } @@ -84,7 +83,7 @@ class TestFont < Test::Unit::TestCase assert_equal(@item.outline, true) end - # def shadow=(v) Axlsx::validate_boolean v; @shadow = v end + # def shadow=(v) Axlsx::validate_boolean v; @shadow = v end def test_shadow assert_raise(ArgumentError) { @item.shadow = -7 } assert_nothing_raised { @item.shadow = true } diff --git a/test/stylesheet/tc_gradient_fill.rb b/test/stylesheet/tc_gradient_fill.rb index f5c87771..c252a498 100644 --- a/test/stylesheet/tc_gradient_fill.rb +++ b/test/stylesheet/tc_gradient_fill.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestGradientFill < Test::Unit::TestCase diff --git a/test/stylesheet/tc_gradient_stop.rb b/test/stylesheet/tc_gradient_stop.rb index d7735231..f1cf5183 100644 --- a/test/stylesheet/tc_gradient_stop.rb +++ b/test/stylesheet/tc_gradient_stop.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestGradientStop < Test::Unit::TestCase diff --git a/test/stylesheet/tc_num_fmt.rb b/test/stylesheet/tc_num_fmt.rb index 281773ce..b5b73929 100644 --- a/test/stylesheet/tc_num_fmt.rb +++ b/test/stylesheet/tc_num_fmt.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestNumFmt < Test::Unit::TestCase diff --git a/test/stylesheet/tc_pattern_fill.rb b/test/stylesheet/tc_pattern_fill.rb index a9b6a113..65ed0dd1 100644 --- a/test/stylesheet/tc_pattern_fill.rb +++ b/test/stylesheet/tc_pattern_fill.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPatternFill < Test::Unit::TestCase @@ -17,13 +16,13 @@ class TestPatternFill < Test::Unit::TestCase assert_equal(@item.fgColor, nil) end - def test_bgColor + def test_bgColor assert_raise(ArgumentError) { @item.bgColor = -1.1 } assert_nothing_raised { @item.bgColor = Axlsx::Color.new } assert_equal(@item.bgColor.rgb, "FF000000") end - def test_fgColor + def test_fgColor assert_raise(ArgumentError) { @item.fgColor = -1.1 } assert_nothing_raised { @item.fgColor = Axlsx::Color.new } assert_equal(@item.fgColor.rgb, "FF000000") diff --git a/test/stylesheet/tc_styles.rb b/test/stylesheet/tc_styles.rb index 61a87bf7..9db37157 100644 --- a/test/stylesheet/tc_styles.rb +++ b/test/stylesheet/tc_styles.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestStyles < Test::Unit::TestCase def setup diff --git a/test/stylesheet/tc_table_style.rb b/test/stylesheet/tc_table_style.rb index 54bebb5b..8b219a93 100644 --- a/test/stylesheet/tc_table_style.rb +++ b/test/stylesheet/tc_table_style.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTableStyle < Test::Unit::TestCase @@ -16,7 +15,7 @@ class TestTableStyle < Test::Unit::TestCase assert_equal(@item.table, nil) end - def test_name + def test_name assert_raise(ArgumentError) { @item.name = -1.1 } assert_nothing_raised { @item.name = "lovely table style" } assert_equal(@item.name, "lovely table style") diff --git a/test/stylesheet/tc_table_style_element.rb b/test/stylesheet/tc_table_style_element.rb index 9cfa5204..2e6e763f 100644 --- a/test/stylesheet/tc_table_style_element.rb +++ b/test/stylesheet/tc_table_style_element.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTableStyleElement < Test::Unit::TestCase @@ -16,7 +15,7 @@ class TestTableStyleElement < Test::Unit::TestCase assert_equal(@item.dxfId, nil) end - def test_type + def test_type assert_raise(ArgumentError) { @item.type = -1.1 } assert_nothing_raised { @item.type = :blankRow } assert_equal(@item.type, :blankRow) diff --git a/test/stylesheet/tc_table_styles.rb b/test/stylesheet/tc_table_styles.rb index 825a18d8..cc8da9d5 100644 --- a/test/stylesheet/tc_table_styles.rb +++ b/test/stylesheet/tc_table_styles.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTableStyles < Test::Unit::TestCase @@ -15,13 +14,13 @@ class TestTableStyles < Test::Unit::TestCase assert_equal(@item.defaultPivotStyle, "PivotStyleLight16") end - def test_defaultTableStyle + def test_defaultTableStyle assert_raise(ArgumentError) { @item.defaultTableStyle = -1.1 } assert_nothing_raised { @item.defaultTableStyle = "anyones guess" } assert_equal(@item.defaultTableStyle, "anyones guess") end - def test_defaultPivotStyle + def test_defaultPivotStyle assert_raise(ArgumentError) { @item.defaultPivotStyle = -1.1 } assert_nothing_raised { @item.defaultPivotStyle = "anyones guess" } assert_equal(@item.defaultPivotStyle, "anyones guess") diff --git a/test/stylesheet/tc_xf.rb b/test/stylesheet/tc_xf.rb index 9ca53562..fdaef970 100644 --- a/test/stylesheet/tc_xf.rb +++ b/test/stylesheet/tc_xf.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestXf < Test::Unit::TestCase @@ -28,91 +27,91 @@ class TestXf < Test::Unit::TestCase assert_equal(@item.applyProtection, nil) end - def test_alignment + def test_alignment assert_raise(ArgumentError) { @item.alignment = -1.1 } assert_nothing_raised { @item.alignment = Axlsx::CellAlignment.new } assert(@item.alignment.is_a?(Axlsx::CellAlignment)) end - def test_protection + def test_protection assert_raise(ArgumentError) { @item.protection = -1.1 } assert_nothing_raised { @item.protection = Axlsx::CellProtection.new } assert(@item.protection.is_a?(Axlsx::CellProtection)) end - def test_numFmtId + def test_numFmtId assert_raise(ArgumentError) { @item.numFmtId = -1.1 } assert_nothing_raised { @item.numFmtId = 0 } assert_equal(@item.numFmtId, 0) end - def test_fillId + def test_fillId assert_raise(ArgumentError) { @item.fillId = -1.1 } assert_nothing_raised { @item.fillId = 0 } assert_equal(@item.fillId, 0) end - def test_fontId + def test_fontId assert_raise(ArgumentError) { @item.fontId = -1.1 } assert_nothing_raised { @item.fontId = 0 } assert_equal(@item.fontId, 0) end - def test_borderId + def test_borderId assert_raise(ArgumentError) { @item.borderId = -1.1 } assert_nothing_raised { @item.borderId = 0 } assert_equal(@item.borderId, 0) end - def test_xfId + def test_xfId assert_raise(ArgumentError) { @item.xfId = -1.1 } assert_nothing_raised { @item.xfId = 0 } assert_equal(@item.xfId, 0) end - def test_quotePrefix + def test_quotePrefix assert_raise(ArgumentError) { @item.quotePrefix = -1.1 } assert_nothing_raised { @item.quotePrefix = false } assert_equal(@item.quotePrefix, false) end - def test_pivotButton + def test_pivotButton assert_raise(ArgumentError) { @item.pivotButton = -1.1 } assert_nothing_raised { @item.pivotButton = false } assert_equal(@item.pivotButton, false) end - def test_applyNumberFormat + def test_applyNumberFormat assert_raise(ArgumentError) { @item.applyNumberFormat = -1.1 } assert_nothing_raised { @item.applyNumberFormat = false } assert_equal(@item.applyNumberFormat, false) end - def test_applyFont + def test_applyFont assert_raise(ArgumentError) { @item.applyFont = -1.1 } assert_nothing_raised { @item.applyFont = false } assert_equal(@item.applyFont, false) end - def test_applyFill + def test_applyFill assert_raise(ArgumentError) { @item.applyFill = -1.1 } assert_nothing_raised { @item.applyFill = false } assert_equal(@item.applyFill, false) end - def test_applyBorder + def test_applyBorder assert_raise(ArgumentError) { @item.applyBorder = -1.1 } assert_nothing_raised { @item.applyBorder = false } assert_equal(@item.applyBorder, false) end - def test_applyAlignment + def test_applyAlignment assert_raise(ArgumentError) { @item.applyAlignment = -1.1 } assert_nothing_raised { @item.applyAlignment = false } assert_equal(@item.applyAlignment, false) end - def test_applyProtection + def test_applyProtection assert_raise(ArgumentError) { @item.applyProtection = -1.1 } assert_nothing_raised { @item.applyProtection = false } assert_equal(@item.applyProtection, false) diff --git a/test/tc_helper.rb b/test/tc_helper.rb new file mode 100644 index 00000000..e5606a09 --- /dev/null +++ b/test/tc_helper.rb @@ -0,0 +1,3 @@ +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" +require 'test/unit' +require "axlsx.rb" diff --git a/test/tc_package.rb b/test/tc_package.rb index a3f2bef5..86a00236 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPackage < Test::Unit::TestCase def setup diff --git a/test/util/tc_simple_typed_list.rb b/test/util/tc_simple_typed_list.rb index 6195153e..482ea8e3 100644 --- a/test/util/tc_simple_typed_list.rb +++ b/test/util/tc_simple_typed_list.rb @@ -1,11 +1,10 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSimpleTypedList < Test::Unit::TestCase - def setup + def setup @list = Axlsx::SimpleTypedList.new Fixnum end - def teardown + def teardown end def test_type_is_a_class_or_array_of_class @@ -15,18 +14,18 @@ class TestSimpleTypedList < Test::Unit::TestCase assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new "1" } assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new [Integer, "Class"] } end - + def test_indexed_based_assignment #should not allow nil assignment assert_raise(ArgumentError) { @list[0] = nil } assert_raise(ArgumentError) { @list[0] = "1" } assert_nothing_raised { @list[0] = 1 } end - + def test_concat_assignment assert_raise(ArgumentError) { @list << nil } assert_raise(ArgumentError) { @list << "1" } - assert_nothing_raised { @list << 1 } + assert_nothing_raised { @list << 1 } end def test_concat_should_return_index @@ -59,8 +58,8 @@ class TestSimpleTypedList < Test::Unit::TestCase assert_nothing_raised { @list.delete_at 3 } @list.unlock #ignore garbage - assert_nothing_raised { @list.delete 0 } - assert_nothing_raised { @list.delete 9 } + assert_nothing_raised { @list.delete 0 } + assert_nothing_raised { @list.delete 9 } end end diff --git a/test/util/tc_validators.rb b/test/util/tc_validators.rb index 09afcc27..d4f98d05 100644 --- a/test/util/tc_validators.rb +++ b/test/util/tc_validators.rb @@ -1,30 +1,29 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestValidators < Test::Unit::TestCase - def setup + def setup end def teardown end - + def test_validators #unsigned_int assert_nothing_raised { Axlsx.validate_unsigned_int 1 } assert_nothing_raised { Axlsx.validate_unsigned_int +1 } assert_raise(ArgumentError) { Axlsx.validate_unsigned_int -1 } assert_raise(ArgumentError) { Axlsx.validate_unsigned_int '1' } - + #int assert_nothing_raised { Axlsx.validate_int 1 } assert_nothing_raised { Axlsx.validate_int -1 } assert_raise(ArgumentError) { Axlsx.validate_int 'a' } assert_raise(ArgumentError) { Axlsx.validate_int Array } - + #boolean (as 0 or 1, :true, :false, true, false, or "true," "false") [0,1,:true, :false, true, false, "true", "false"].each do |v| assert_nothing_raised { Axlsx.validate_boolean 0 } end assert_raise(ArgumentError) { Axlsx.validate_boolean 2 } - + #string assert_nothing_raised { Axlsx.validate_string "1" } assert_raise(ArgumentError) { Axlsx.validate_string 2 } @@ -64,7 +63,7 @@ class TestValidators < Test::Unit::TestCase assert_raise(ArgumentError) { Axlsx.validate_content_type nil } assert_raise(ArgumentError) { Axlsx.validate_content_type "http://some.url" } assert_raise(ArgumentError) { Axlsx.validate_content_type false } - + #relationshipType assert_nothing_raised { Axlsx.validate_relationship_type Axlsx::WORKBOOK_R } assert_raise(ArgumentError) { Axlsx.validate_relationship_type nil } diff --git a/test/workbook/tc_shared_strings_table.rb b/test/workbook/tc_shared_strings_table.rb index b0e9de05..e3c9bf7b 100644 --- a/test/workbook/tc_shared_strings_table.rb +++ b/test/workbook/tc_shared_strings_table.rb @@ -1,14 +1,13 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSharedStringsTable < Test::Unit::TestCase - def setup + 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'] + ws.add_row ['c', 1, 'd'] end def test_workbook_has_shared_strings @@ -25,9 +24,9 @@ class TestSharedStringsTable < Test::Unit::TestCase assert_equal(sst.unique_count, 4) end - def test_valid_document + def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) - doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml) + doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml_string) errors = [] schema.validate(doc).each do |error| puts error.message diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb index 1382a12d..af2a7889 100644 --- a/test/workbook/tc_workbook.rb +++ b/test/workbook/tc_workbook.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestWorkbook < Test::Unit::TestCase def setup diff --git a/test/table/tc_table.rb b/test/workbook/worksheet/table/tc_table.rb index d6bc95d0..fc29bf66 100644 --- a/test/table/tc_table.rb +++ b/test/workbook/worksheet/table/tc_table.rb @@ -1,11 +1,10 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTable < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet - 40.times do + 40.times do @ws << ["aa","aa","aa","aa","aa","aa"] end end @@ -13,7 +12,7 @@ class TestTable < Test::Unit::TestCase def test_initialization assert(@ws.workbook.tables.empty?) assert(@ws.tables.empty?) - + end def test_add_table @@ -25,13 +24,13 @@ class TestTable < Test::Unit::TestCase assert_equal(table.name, name, "options for name are applied") end - + def test_charts assert(@ws.drawing.charts.empty?) chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1]) assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"nancy", :start_at=>[1,5], :end_at=>[5,10]) - assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") + assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") end def test_pn @@ -43,7 +42,7 @@ class TestTable < Test::Unit::TestCase table = @ws.add_table("A1:D5") assert_equal(@ws.tables.first.rId, "rId1") end - + def test_index table = @ws.add_table("A1:D5") assert_equal(@ws.tables.first.index, @ws.workbook.tables.index(@ws.tables.first)) diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index e111f7cd..991181e7 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCell < Test::Unit::TestCase @@ -207,13 +206,14 @@ class TestCell < Test::Unit::TestCase end def test_equality - c2 = @row.add_cell 1, :type=>:float, :style=>1 - assert(c2.shareable(@c)) + c2 = @row.add_cell 1, :type=>:float, :style=>1 + + assert_equal(c2.shareable_hash,@c.shareable_hash) 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) + assert_equal(c4.shareable_hash == c2.shareable_hash,false) c5 = @row.add_cell 1, :type=>:float, :style=>1, :color => "#FFFFFFFF" - assert(c5.shareable(c4)) + assert_equal(c5.shareable_hash, c4.shareable_hash) end @@ -223,6 +223,13 @@ class TestCell < Test::Unit::TestCase assert_equal(@c.ssti, 1) end + def test_to_xml_string + builder = Nokogiri::XML::Builder.new(:encoding => Axlsx::ENCODING) do |xml| + @c.to_xml(xml) + end + c_xml = Nokogiri::XML(builder.to_xml(:save_with => 0)) + assert_equal(c_xml.xpath("/c[@s=1]").size, 1) + end def test_to_xml # TODO This could use some much more stringent testing related to the xml content generated! row = @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)"] diff --git a/test/workbook/worksheet/tc_date_time_converter.rb b/test/workbook/worksheet/tc_date_time_converter.rb index c78e51eb..adeae92b 100644 --- a/test/workbook/worksheet/tc_date_time_converter.rb +++ b/test/workbook/worksheet/tc_date_time_converter.rb @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestDateTimeConverter < Test::Unit::TestCase def setup diff --git a/test/workbook/worksheet/tc_page_margins.rb b/test/workbook/worksheet/tc_page_margins.rb index 3368129c..4c5e43fa 100644 --- a/test/workbook/worksheet/tc_page_margins.rb +++ b/test/workbook/worksheet/tc_page_margins.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPageMargins < Test::Unit::TestCase @@ -8,7 +7,7 @@ class TestPageMargins < Test::Unit::TestCase ws = p.workbook.add_worksheet :name=>"hmmm" @pm = ws.page_margins end - + def test_initialize assert_equal(Axlsx::PageMargins::DEFAULT_LEFT_RIGHT, @pm.left) assert_equal(Axlsx::PageMargins::DEFAULT_LEFT_RIGHT, @pm.right) @@ -25,7 +24,7 @@ class TestPageMargins < Test::Unit::TestCase assert_equal(2, optioned.top) assert_equal(1, optioned.bottom) assert_equal(0.1, optioned.header) - assert_equal(0.1, optioned.footer) + assert_equal(0.1, optioned.footer) end @@ -48,7 +47,7 @@ class TestPageMargins < Test::Unit::TestCase assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.header) assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.footer) end - + def test_to_xml @pm.left = 1.1 @pm.right = 1.2 @@ -61,7 +60,7 @@ class TestPageMargins < Test::Unit::TestCase doc = Nokogiri::XML.parse(xml.to_xml) assert_equal(1, doc.xpath(".//pageMargins[@left=1.1][@right=1.2][@top=1.3][@bottom=1.4][@header=0.8][@footer=0.9]").size) end - + def test_left assert_raise(ArgumentError) { @pm.left = -1.2 } assert_nothing_raised { @pm.left = 1.5 } diff --git a/test/workbook/worksheet/tc_row.rb b/test/workbook/worksheet/tc_row.rb index 38c910c5..b77715c5 100644 --- a/test/workbook/worksheet/tc_row.rb +++ b/test/workbook/worksheet/tc_row.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestRow < Test::Unit::TestCase @@ -8,7 +7,7 @@ class TestRow < Test::Unit::TestCase @ws = p.workbook.add_worksheet :name=>"hmmm" @row = @ws.add_row end - + def test_initialize assert(@row.cells.empty?, "no cells by default") assert_equal(@row.worksheet, @ws, "has a reference to the worksheet") @@ -25,7 +24,7 @@ class TestRow < Test::Unit::TestCase def test_style r = @ws.add_row([1,2,3,4,5]) r.style=1 - r.cells.each { |c| assert_equal(c.style,1) } + r.cells.each { |c| assert_equal(c.style,1) } end def test_index @@ -61,6 +60,18 @@ class TestRow < Test::Unit::TestCase assert_equal(0, doc.xpath(".//row[@customHeight]").size) end + def test_to_xml_string + r_s_xml = Nokogiri::XML(@row.to_xml_string) + assert_equal(r_s_xml.xpath(".//row[@r=1]").size, 1) + end + + def test_to_xml_string_with_custom_height + @row.add_cell 1 + @row.height = 20 + r_s_xml = Nokogiri::XML(@row.to_xml_string) + assert_equal(r_s_xml.xpath(".//row[@r=1][@ht=20][@customHeight=1]").size, 1) + end + def test_to_xml_with_custom_height @row.height = 20 xml = Nokogiri::XML::Builder.new diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index c9aa7069..d9a89a69 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestWorksheet < Test::Unit::TestCase def setup @@ -84,8 +83,8 @@ class TestWorksheet < Test::Unit::TestCase @ws.add_row [1, 2, 3] @ws.add_row [4, 5, 6] range = @ws["A1:C2"] - first_row = @ws[1] - last_row = @ws[2] + first_row = @ws[0] + last_row = @ws[1] assert_equal(@ws.rows[0],first_row) assert_equal(@ws.rows[1],last_row) assert_equal(range.size, 6) @@ -154,6 +153,87 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(@ws.rows[2].cells[1].style, 0) end + def test_to_xml_string_fit_to_page + @ws.fit_to_page = true + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:pageSetUpPr[@fitToPage="true"]').size, 1) + end + + def test_to_xml_string_dimensions + @ws.add_row [1,2,3] + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:dimension[@ref="A1:C1"]').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="true"]').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="false"]').size, 1) + end + + + def test_to_xml_string_show_selection + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView/xmlns:selection[@activeCell="A1"]').size, 1) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView/xmlns:selection[@sqref="A1"]').size, 1) + end + + def test_to_xml_string_auto_fit_data + @ws.add_row [1, "two"] + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:cols/xmlns:col').size, 2) + end + + def test_to_xml_string_sheet_data + @ws.add_row [1, "two"] + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetData/xmlns:row').size, 1) + end + + def test_to_xml_string_auto_filter + @ws.add_row [1, "two"] + @ws.auto_filter = "A1:B1" + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:autoFilter[@ref="A1:B1"]').size, 1) + end + + def test_to_xml_string_merge_cells + @ws.add_row [1, "two"] + @ws.merge_cells "A1:D1" + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:mergeCells/xmlns:mergeCell[@ref="A1:D1"]').size, 1) + end + + def test_to_xml_string_page_margins + @ws.page_margins do |pm| + pm.left = 9 + pm.right = 7 + end + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:pageMargins[@left="9"][@right="7"]').size, 1) + end + + def test_to_xml_string_drawing + c = @ws.add_chart Axlsx::Pie3DChart + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:drawing[@r:id="rId1"]').size, 1) + end + + def test_to_xml_string_tables + @ws.add_row ["one", "two"] + @ws.add_row [1, 2] + @ws.add_table "A1:B2" + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:tableParts[@count="1"]').size, 1) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:tableParts/xmlns:tablePart[@r:id="rId1"]').size, 1) + end + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@ws.to_xml) |
