From bdf77ceb217753b7e09dd54ef1e0a3842859d1b1 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Wed, 21 Mar 2012 23:00:28 +0900 Subject: tiny bit of restructuring --- lib/axlsx.rb | 1 - lib/axlsx/table/table.rb | 97 ----------------------------------- lib/axlsx/workbook/workbook.rb | 3 +- lib/axlsx/workbook/worksheet/table.rb | 97 +++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 99 deletions(-) delete mode 100644 lib/axlsx/table/table.rb create mode 100644 lib/axlsx/workbook/worksheet/table.rb diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 399e740e..eb5068b5 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -22,7 +22,6 @@ require 'axlsx/rels/relationships.rb' require 'axlsx/drawing/drawing.rb' require 'axlsx/workbook/workbook.rb' require 'axlsx/package.rb' -require 'axlsx/table/table.rb' #required gems require 'nokogiri' require 'zip/zip' diff --git a/lib/axlsx/table/table.rb b/lib/axlsx/table/table.rb deleted file mode 100644 index be78d4a0..00000000 --- a/lib/axlsx/table/table.rb +++ /dev/null @@ -1,97 +0,0 @@ -# encoding: UTF-8 -module Axlsx - # Table - # @note Worksheet#add_table is the recommended way to create charts for your worksheets. - # @see README for examples - class Table - - - # The reference to the table data - # @return [String] - attr_reader :ref - - # The name of the table. - # @return [String] - attr_reader :name - - # The style for the table. - # @return [TableStyle] - attr_reader :style - - # Creates a new Table object - # @param [String] ref The reference to the table data. - # @param [Sheet] ref The sheet containing the table data. - # @option options [Cell, String] name - # @option options [TableStyle] style - def initialize(ref, sheet, options={}) - @ref = ref - @sheet = sheet - @style = nil - @sheet.workbook.tables << self - @name = "Table#{index+1}" - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - yield self if block_given? - end - - # The index of this chart in the workbooks charts collection - # @return [Integer] - def index - @sheet.workbook.tables.index(self) - end - - # The part name for this table - # @return [String] - def pn - "#{TABLE_PN % (index+1)}" - end - - # The relation reference id for this table - # @return [String] - def rId - "rId#{index+1}" - end - - # The name of the Table. - # @param [String, Cell] v - # @return [Title] - def name=(v) - DataTypeValidator.validate "#{self.class}.name", [String], v - if v.is_a?(String) - @name = v - end - end - - - # The style for the table. - # TODO - # def style=(v) DataTypeValidator.validate "Chart.style", Integer, v, lambda { |arg| arg >= 1 && arg <= 48 }; @style = v; end - - # Table Serialization - # serializes the table - def to_xml - builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| - xml.table(:xmlns => XML_NS, :id => index+1, :name => @name, :displayName => @name.gsub(/\s/,'_'), :ref => @ref, :totalsRowShown => 0) { - xml.autoFilter :ref=>@ref - xml.tableColumns(:count => header_cells.length) { - header_cells.each_with_index do |cell,index| - xml.tableColumn :id => index+1, :name => cell.value - end - } - xml.tableStyleInfo :showFirstColumn=>"0", :showLastColumn=>"0", :showRowStripes=>"1", :showColumnStripes=>"0", :name=>"TableStyleMedium9" - #TODO implement tableStyleInfo - } - end - builder.to_xml(:save_with => 0) - end - - private - - # get the header cells (hackish) - def header_cells - header = @ref.gsub(/^(\w+)(\d+)\:(\w+)\d+$/, '\1\2:\3\2') - @sheet[header] - end - end -end diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index b6e0a9db..a8e0a1af 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -7,6 +7,7 @@ require 'axlsx/workbook/worksheet/page_margins.rb' require 'axlsx/workbook/worksheet/row.rb' require 'axlsx/workbook/worksheet/worksheet.rb' require 'axlsx/workbook/shared_strings_table.rb' +require 'axlsx/workbook/worksheet/table.rb' # The Workbook class is an xlsx workbook that manages worksheets, charts, drawings and styles. # The following parts of the Office Open XML spreadsheet specification are not implimented in this version. @@ -117,7 +118,7 @@ require 'axlsx/workbook/shared_strings_table.rb' @images = SimpleTypedList.new Pic @tables = SimpleTypedList.new Table @use_autowidth = true - + self.date1904= !options[:date1904].nil? && options[:date1904] yield self if block_given? end diff --git a/lib/axlsx/workbook/worksheet/table.rb b/lib/axlsx/workbook/worksheet/table.rb new file mode 100644 index 00000000..831040f3 --- /dev/null +++ b/lib/axlsx/workbook/worksheet/table.rb @@ -0,0 +1,97 @@ +# encoding: UTF-8 +module Axlsx + # Table + # @note Worksheet#add_table is the recommended way to create tables for your worksheets. + # @see README for examples + class Table + + + # The reference to the table data + # @return [String] + attr_reader :ref + + # The name of the table. + # @return [String] + attr_reader :name + + # The style for the table. + # @return [TableStyle] + attr_reader :style + + # Creates a new Table object + # @param [String] ref The reference to the table data. + # @param [Sheet] ref The sheet containing the table data. + # @option options [Cell, String] name + # @option options [TableStyle] style + def initialize(ref, sheet, options={}) + @ref = ref + @sheet = sheet + @style = nil + @sheet.workbook.tables << self + @name = "Table#{index+1}" + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + yield self if block_given? + end + + # The index of this chart in the workbooks charts collection + # @return [Integer] + def index + @sheet.workbook.tables.index(self) + end + + # The part name for this table + # @return [String] + def pn + "#{TABLE_PN % (index+1)}" + end + + # The relation reference id for this table + # @return [String] + def rId + "rId#{index+1}" + end + + # The name of the Table. + # @param [String, Cell] v + # @return [Title] + def name=(v) + DataTypeValidator.validate "#{self.class}.name", [String], v + if v.is_a?(String) + @name = v + end + end + + + # The style for the table. + # TODO + # def style=(v) DataTypeValidator.validate "Table.style", Integer, v, lambda { |arg| arg >= 1 && arg <= 48 }; @style = v; end + + # Table Serialization + # serializes the table + def to_xml + builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| + xml.table(:xmlns => XML_NS, :id => index+1, :name => @name, :displayName => @name.gsub(/\s/,'_'), :ref => @ref, :totalsRowShown => 0) { + xml.autoFilter :ref=>@ref + xml.tableColumns(:count => header_cells.length) { + header_cells.each_with_index do |cell,index| + xml.tableColumn :id => index+1, :name => cell.value + end + } + xml.tableStyleInfo :showFirstColumn=>"0", :showLastColumn=>"0", :showRowStripes=>"1", :showColumnStripes=>"0", :name=>"TableStyleMedium9" + #TODO implement tableStyleInfo + } + end + builder.to_xml(:save_with => 0) + end + + private + + # get the header cells (hackish) + def header_cells + header = @ref.gsub(/^(\w+)(\d+)\:(\w+)\d+$/, '\1\2:\3\2') + @sheet[header] + end + end +end -- cgit v1.2.3