lib/axlsx/workbook/workbook.rb in axlsx-1.0.18 vs lib/axlsx/workbook/workbook.rb in axlsx-1.1.0

- old
+ new

@@ -3,12 +3,14 @@ require 'axlsx/workbook/worksheet/date_time_converter.rb' require 'axlsx/workbook/worksheet/cell.rb' require 'axlsx/workbook/worksheet/page_margins.rb' require 'axlsx/workbook/worksheet/row.rb' +require 'axlsx/workbook/worksheet/col.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. # # bookViews @@ -44,11 +46,11 @@ Axlsx::validate_boolean(v) @use_shared_strings = v end - # A collection of worksheets associated with this workbook. + # A collection of worksheets associated with this workbook. # @note The recommended way to manage worksheets is add_worksheet # @see Workbook#add_worksheet # @see Worksheet # @return [SimpleTypedList] attr_reader :worksheets @@ -72,10 +74,18 @@ # @see Worksheet#add_chart # @see Drawing # @return [SimpleTypedList] attr_reader :drawings + # A colllection of tables associated with this workbook + # @note The recommended way to manage drawings is Worksheet#add_table + # @see Worksheet#add_table + # @see Table + # @return [SimpleTypedList] + attr_reader :tables + + # The styles associated with this workbook # @note The recommended way to manage styles is Styles#add_style # @see Style#add_style # @see Style # @return [Styles] @@ -86,10 +96,11 @@ # Indicates if the epoc date for serialization should be 1904. If false, 1900 is used. @@date1904 = false + # lets come back to this later when we are ready for parsing. #def self.parse entry # io = entry.get_input_stream # w = self.new # w.parser_xml = Nokogiri::XML(io.read) @@ -104,10 +115,13 @@ @styles = Styles.new @worksheets = SimpleTypedList.new Worksheet @drawings = SimpleTypedList.new Drawing @charts = SimpleTypedList.new Chart @images = SimpleTypedList.new Pic + @tables = SimpleTypedList.new Table + @use_autowidth = true + self.date1904= !options[:date1904].nil? && options[:date1904] yield self if block_given? end # Instance level access to the class variable 1904 @@ -123,10 +137,18 @@ # retrieves the date1904 attribute # @return [Boolean] def self.date1904() @@date1904; end + # Indicates if the workbook should use autowidths or not. + # this must be set before instantiating a worksheet to avoid Rmagix inclusion + # @return [Boolean] + def use_autowidth() @use_autowidth; end + + # see @use_autowidth + def use_autowidth=(v) Axlsx::validate_boolean v; @use_autowidth = v; end + # Adds a worksheet to this workbook # @return [Worksheet] # @option options [String] name The name of the worksheet. # @option options [Hash] page_margins The page margins for the worksheet. # @see Worksheet#initialize @@ -165,29 +187,31 @@ worksheet = self.worksheets.select { |s| s.name == sheet_name }.first raise ArgumentError, 'Unknown Sheet' unless sheet_name && worksheet.is_a?(Worksheet) worksheet[cell_def.gsub(/.+!/,"")] end - # Serializes the workbook document + # Serialize the workbook + # @param [String] str # @return [String] - def to_xml() + def to_xml_string(str='') add_worksheet unless worksheets.size > 0 - builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| - xml.workbook(:xmlns => XML_NS, :'xmlns:r' => XML_NS_R) { - xml.workbookPr(:date1904=>@@date1904) - #<x:workbookProtection workbookPassword="xsd:hexBinary data" lockStructure="1" lockWindows="1" /> - # Required to support rubyXL parsing as it requires sheetView, which requires this. - # and removed because it seems to cause some odd [Grouped] behaviour in excel. - # xml.bookViews { - # xml.workbookView :activeTab=>0 - # } - xml.sheets { - @worksheets.each_with_index do |sheet, index| - xml.sheet(:name=>sheet.name, :sheetId=>index+1, :"r:id"=>sheet.rId) - end - } - } + str << '<?xml version="1.0" encoding="UTF-8"?>' + str << '<workbook xmlns="' << XML_NS << '" xmlns:r="' << XML_NS_R << '">' + str << '<workbookPr date1904="' << @@date1904.to_s << '"/>' + str << '<sheets>' + @worksheets.each_with_index do |sheet, index| + str << '<sheet name="' << sheet.name << '" sheetId="' << (index+1).to_s << '" r:id="' << sheet.rId << '"/>' end - builder.to_xml(:save_with => 0) + str << '</sheets>' + str << '<definedNames>' + @worksheets.each_with_index do |sheet, index| + if sheet.auto_filter + str << '<definedName name="_xlnm._FilterDatabase" localSheetId="' << index.to_s << '" hidden="1">' + str << sheet.abs_auto_filter << '</definedName>' + end + end + str << '</definedNames>' + str << '</workbook>' end + end end