lib/axlsx/workbook/workbook.rb in axlsx-2.0.1 vs lib/axlsx/workbook/workbook.rb in axlsx-2.1.0.pre

- old
+ new

@@ -3,10 +3,12 @@ require 'axlsx/workbook/worksheet/sheet_calc_pr.rb' require 'axlsx/workbook/worksheet/auto_filter/auto_filter.rb' require 'axlsx/workbook/worksheet/date_time_converter.rb' require 'axlsx/workbook/worksheet/protected_range.rb' require 'axlsx/workbook/worksheet/protected_ranges.rb' +require 'axlsx/workbook/worksheet/rich_text_run' +require 'axlsx/workbook/worksheet/rich_text' require 'axlsx/workbook/worksheet/cell_serializer.rb' require 'axlsx/workbook/worksheet/cell.rb' require 'axlsx/workbook/worksheet/page_margins.rb' require 'axlsx/workbook/worksheet/page_set_up_pr.rb' require 'axlsx/workbook/worksheet/page_setup.rb' @@ -35,13 +37,14 @@ require 'axlsx/workbook/worksheet/worksheet_hyperlink' require 'axlsx/workbook/worksheet/worksheet_hyperlinks' require 'axlsx/workbook/worksheet/break' require 'axlsx/workbook/worksheet/row_breaks' require 'axlsx/workbook/worksheet/col_breaks' +require 'axlsx/workbook/workbook_view' +require 'axlsx/workbook/workbook_views' - require 'axlsx/workbook/worksheet/worksheet.rb' require 'axlsx/workbook/shared_strings_table.rb' require 'axlsx/workbook/defined_name.rb' require 'axlsx/workbook/defined_names.rb' require 'axlsx/workbook/worksheet/table_style_info.rb' @@ -137,10 +140,14 @@ # @see Worksheet#add_table # @see Table # @return [SimpleTypedList] attr_reader :pivot_tables + # A collection of views for this workbook + def views + @views ||= WorkbookViews.new + end # A collection of defined names for this workbook # @note The recommended way to manage defined names is Workbook#add_defined_name # @see DefinedName # @return [DefinedNames] @@ -261,10 +268,14 @@ worksheet = Worksheet.new(self, options) yield worksheet if block_given? worksheet end + def add_view(options={}) + views << WorkbookView.new(options) + end + # Adds a defined name to this workbook # @return [DefinedName] # @param [String] formula @see DefinedName # @param [Hash] options @see DefinedName def add_defined_name(formula, options) @@ -281,11 +292,11 @@ pivot_tables.each_with_index do |pivot_table, index| r << Relationship.new(pivot_table.cache_definition, PIVOT_TABLE_CACHE_DEFINITION_R, PIVOT_TABLE_CACHE_DEFINITION_PN % (index+1)) end r << Relationship.new(self, STYLES_R, STYLES_PN) if use_shared_strings - r << Relationship.new(self, SHARED_STRINGS_R, SHARED_STRINGS_PN) + r << Relationship.new(self, SHARED_STRINGS_R, SHARED_STRINGS_PN) end r end # generates a shared string object against all cells in all worksheets. @@ -325,26 +336,22 @@ # Serialize the workbook # @param [String] str # @return [String] def to_xml_string(str='') - add_worksheet unless worksheets.size > 0 + add_worksheet(name: 'Sheet1') unless worksheets.size > 0 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 << ('<workbook xmlns="' << XML_NS << '" xmlns:r="' << XML_NS_R << '">') + str << ('<workbookPr date1904="' << @@date1904.to_s << '"/>') + views.to_xml_string(str) str << '<sheets>' - @worksheets.each_with_index do |sheet, index| - str << '<sheet name="' << sheet.name << '" sheetId="' << (index+1).to_s << '" r:id="' << sheet.rId << '"/>' - if defined_name = sheet.auto_filter.defined_name - add_defined_name defined_name, :name => '_xlnm._FilterDatabase', :local_sheet_id => index, :hidden => 1 - end - end + worksheets.each { |sheet| sheet.to_sheet_node_xml_string(str) } str << '</sheets>' defined_names.to_xml_string(str) unless pivot_tables.empty? str << '<pivotCaches>' pivot_tables.each do |pivot_table| - str << '<pivotCache cacheId="' << pivot_table.cache_definition.cache_id.to_s << '" r:id="' << pivot_table.cache_definition.rId << '"/>' + str << ('<pivotCache cacheId="' << pivot_table.cache_definition.cache_id.to_s << '" r:id="' << pivot_table.cache_definition.rId << '"/>') end str << '</pivotCaches>' end str << '</workbook>' end