From ec9d3b896a2b07cdcb8198c2227df8dac3b83cdb Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Wed, 28 Mar 2012 00:52:30 +0900 Subject: Still not fast enough? ``` user system total real axlsx_noautowidth 0.760000 0.020000 0.780000 ( 0.885482) axlsx 3.560000 0.130000 3.690000 ( 4.158594) axlsx_shared 11.610000 0.180000 11.790000 ( 13.208945) axlsx_stream 3.450000 0.120000 3.570000 ( 3.920745) csv 0.240000 0.010000 0.250000 ( 0.269822) --- lib/axlsx.rb | 21 ++++++++++++++++ lib/axlsx/stylesheet/color.rb | 5 ++-- lib/axlsx/workbook/shared_strings_table.rb | 8 +++--- lib/axlsx/workbook/worksheet/cell.rb | 39 +++++++++++------------------- lib/axlsx/workbook/worksheet/row.rb | 10 +++++--- lib/axlsx/workbook/worksheet/worksheet.rb | 4 ++- 6 files changed, 52 insertions(+), 35 deletions(-) (limited to 'lib') diff --git a/lib/axlsx.rb b/lib/axlsx.rb index cf3beb87..09f351fc 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -63,4 +63,25 @@ module Axlsx [v[:i]-1, ((name[/[1-9][0-9]*/]).to_i)-1] end + + # converts the column index into alphabetical values. + # @note This follows the standard spreadsheet convention of naming columns A to Z, followed by AA to AZ etc. + # @return [String] + def self.col_ref(index) + chars = [] + while index >= 26 do + chars << ((index % 26) + 65).chr + index /= 26 + end + chars << ((chars.empty? ? index : index-1) + 65).chr + chars.reverse.join + end + + # @return [String] The alpha(column)numeric(row) reference for this sell. + # @example Relative Cell Reference + # ws.rows.first.cells.first.r #=> "A1" + def self.cell_r(c_index, r_index) + Axlsx::col_ref(c_index).to_s << (r_index+1).to_s + end + end diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb index 5d3e60ca..2a08fd3c 100644 --- a/lib/axlsx/stylesheet/color.rb +++ b/lib/axlsx/stylesheet/color.rb @@ -62,12 +62,11 @@ module Axlsx # def indexed=(v) Axlsx::validate_unsigned_integer v; @indexed = v end def to_xml_string - str = ["" - str.join end # Serializes the color diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index 5ebfd87f..0bdd7936 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -32,11 +32,12 @@ module Axlsx cells = cells.flatten.reject { |c| c.type != :string || c.value.start_with?('=') } @count = cells.size @unique_cells = [] + @shared_xml_string = "" resolve(cells) end def to_xml_string - str = "%s" % [XML_NS, count, unique_count, @unique_cells.map {|cell| cell[:data]}.join] + '' << @shared_xml_string << '' end # Generate the xml document for the Shared Strings Table @@ -64,11 +65,12 @@ module Axlsx cells.each do |cell| cell_hash = cell.shareable_hash index = @unique_cells.index do |item| - item[:hash] == cell_hash + item == cell_hash end if index == nil cell.send :ssti=, @unique_cells.size - @unique_cells << {:hash => cell_hash, :data => ''<< cell.run_xml_string << ''} + @shared_xml_string << '' << cell.run_xml_string << '' + @unique_cells << cell_hash else cell.send :ssti=, index end diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index a3ec046a..24b380cb 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -238,8 +238,9 @@ module Axlsx # @return [String] The alpha(column)numeric(row) reference for this sell. # @example Relative Cell Reference # ws.rows.first.cells.first.r #=> "A1" + # @note this will be discontinued in 1.1.0 - prefer Axlsx.cell_r def r - "#{col_ref}#{@row.index+1}" + "#{Axlsx::col_ref(index)}#{@row.index+1}" end # @return [String] The absolute alpha(column)numeric(row) reference for this sell. @@ -275,9 +276,9 @@ module Axlsx self.row.worksheet.merge_cells "#{self.r}:#{range_end}" unless range_end.nil? end - def run_xml_string - str = "" + def run_xml_string(str = '') if is_text_run? + puts 'text run' data = self.instance_values.reject{|key, value| value == nil } keys = data.keys & INLINE_STYLES keys.delete ['value', 'type'] @@ -333,30 +334,32 @@ 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 + def to_xml_string(r_index, c_index, str = '') + str << '' << value.to_s.gsub('=', '') << '' + str << 't="str">' << value.to_s.gsub('=', '') << '' else #parse shared if @ssti - '' << ssti << '' + str << 't="s">' << ssti << '' else - '' << run_xml_string << '' + str << 't="inlineStr">' << run_xml_string << '' end end when :date # TODO: See if this is subject to the same restriction as Time below - '' << DateTimeConverter::date_to_serial(@value).to_s << '' + str << '>' << DateTimeConverter::date_to_serial(@value).to_s << '' when :time - '' << DateTimeConverter::time_to_serial(@value).to_s << '' + str << '>' << DateTimeConverter::time_to_serial(@value).to_s << '' when :boolean - '' << @value.to_s << '' + str << 't="b">' << @value.to_s << '' else - '' << @value.to_s << '' + str << '>' << @value.to_s << '' end + str << '' end def to_xml(xml) @@ -412,20 +415,6 @@ module Axlsx # assigns the owning row for this cell. def row=(v) DataTypeValidator.validate "Cell.row", Row, v; @row=v end - # converts the column index into alphabetical values. - # @note This follows the standard spreadsheet convention of naming columns A to Z, followed by AA to AZ etc. - # @return [String] - def col_ref - chars = [] - index = self.index - while index >= 26 do - chars << ((index % 26) + 65).chr - index /= 26 - end - chars << ((chars.empty? ? index : index-1) + 65).chr - chars.reverse.join - end - # Determines the cell type based on the cell value. # @note This is only used when a cell is created but no :type option is specified, the following rules apply: # 1. If the value is an instance of Date, the type is set to :date diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb index 30101a28..ac251ed8 100644 --- a/lib/axlsx/workbook/worksheet/row.rb +++ b/lib/axlsx/workbook/worksheet/row.rb @@ -59,12 +59,16 @@ module Axlsx worksheet.rows.index(self) end - def to_xml_string + def to_xml_string(r_index, str = '') + str << '' << @cells.map { |cell| cell.to_xml_string }.join << '' + str << 'customHeight="1" ht="' << height.to_s << '">' else - '' << (@cells.map { |cell| cell.to_xml_string }.join) << '' + str << '>' end + @cells.each_with_index { |cell, c_index| cell.to_xml_string(r_index, c_index, str) } + str << '' + str end # Serializes the row # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 6a029f19..20c13d8b 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -386,7 +386,9 @@ module Axlsx str.concat '' end - str.concat "%s" % @rows.map { |obj| obj.to_xml_string }.join + str.concat '' + @rows.each_with_index { |row, index| row.to_xml_string(index, str) } + str.concat '' str.concat page_margins.to_xml_string if @page_margins str.concat "" % @auto_filter if @auto_filter str.concat "%s" % [@merged_cells.size, @merged_cells.reduce('') { |memo, obj| "" % obj } ] unless @merged_cells.empty? -- cgit v1.2.3