lib/write_xlsx/workbook.rb in write_xlsx-0.0.3 vs lib/write_xlsx/workbook.rb in write_xlsx-0.0.4

- old
+ new

@@ -842,11 +842,11 @@ attributes << 'defaultThemeVersion' << 124226 @writer.empty_tag('workbookPr', attributes) end def write_book_views #:nodoc: - @writer.start_tag('bookViews') << write_workbook_view << @writer.end_tag('bookViews') + @writer.tag_elements('bookViews') { write_workbook_view } end def write_workbook_view #:nodoc: attributes = [ 'xWindow', 240, @@ -862,17 +862,17 @@ end @writer.empty_tag('workbookView', attributes) end def write_sheets #:nodoc: - str = @writer.start_tag('sheets') - id_num = 1 - @worksheets.each do |sheet| - str << write_sheet(sheet.name, id_num, sheet.hidden?) - id_num += 1 + @writer.tag_elements('sheets') do + id_num = 1 + @worksheets.each do |sheet| + write_sheet(sheet.name, id_num, sheet.hidden?) + id_num += 1 + end end - str << @writer.end_tag('sheets') end def write_sheet(name, sheet_id, hidden = false) #:nodoc: attributes = [ 'name', name, @@ -891,32 +891,32 @@ @writer.empty_tag('calcPr', attributes) end def write_ext_lst #:nodoc: tag = 'extLst' - @writer.start_tag(tag) << write_ext << @writer.end_tag(tag) + @writer.tag_elements(tag) { write_ext } end def write_ext #:nodoc: tag = 'ext' attributes = [ 'xmlns:mx', 'http://schemas.microsoft.com/office/mac/excel/2008/main', 'uri', 'http://schemas.microsoft.com/office/mac/excel/2008/main' ] - @writer.start_tag(tag, attributes) << write_mx_arch_id << @writer.end_tag(tag) + @writer.tag_elements(tag, attributes) { write_mx_arch_id } end def write_mx_arch_id #:nodoc: @writer.empty_tag('mx:ArchID', ['Flags', 2]) end def write_defined_names #:nodoc: return if @defined_names.nil? || @defined_names.empty? tag = 'definedNames' - str = @writer.start_tag(tag) - @defined_names.each { |defined_name| str << write_defined_name(defined_name) } - str << @writer.end_tag(tag) + @writer.tag_elements(tag) do + @defined_names.each { |defined_name| write_defined_name(defined_name) } + end end def write_defined_name(data) #:nodoc: name, id, range, hidden = data @@ -947,34 +947,26 @@ # # Assemble worksheets into a workbook. # def store_workbook #:nodoc: - packager = Package::Packager.new - # Add a default worksheet if non have been added. add_worksheet if @worksheets.empty? # Ensure that at least one worksheet has been selected. @worksheets.first.select if @activesheet == 0 # Set the active sheet. @worksheets.each { |sheet| sheet.activate if sheet.index == @activesheet } - # Prepare the worksheet cell comments. - prepare_comments + prepare_comments # Prepare the worksheet cell comments. + prepare_defined_names # Set the defined names for the worksheets such as Print Titles. + prepare_drawings # Prepare the drawings, charts and images. + add_chart_data # Add cached data to charts. - # Set the defined names for the worksheets such as Print Titles. - prepare_defined_names - - # Prepare the drawings, charts and images. - prepare_drawings - - # Add cached data to charts. - add_chart_data - # Package the workbook. + packager = Package::Packager.new packager.add_workbook(self) packager.set_package_dir(@tempdir) packager.create_package # Free up the Packager object. @@ -1019,48 +1011,30 @@ @dxf_formats[dxf_index] = format if dxf_index end end # - # Set the default index for each format. This is mainly used for testing. - # - def set_default_xf_indices #:nodoc: - @formats.each { |format| format.get_xf_index } - end - - # # Iterate through the XF Format objects and give them an index to non-default # font elements. # def prepare_fonts #:nodoc: fonts = {} - index = 0 - @xf_formats.each do |format| key = format.get_font_key - - if fonts[key] - # Font has already been used. - format.font_index = fonts[key] - format.has_font = 0 - else - # This is a new font. - fonts[key] = index - format.font_index = index - format.has_font = 1 - index += 1 - end + format.has_font(!fonts[key]) + format.font_index = fonts[key] || fonts.size + fonts[key] ||= fonts.size end - @font_count = index + @font_count = fonts.size # For the DXF formats we only need to check if the properties have changed. @dxf_formats.each do |format| # The only font properties that can change for a DXF format are: color, # bold, italic, underline and strikethrough. if format.color || format.bold? || format.italic? || format.underline? || format.strikeout? - format.has_dxf_font = 1 + format.has_dxf_font(true) end end end # @@ -1108,34 +1082,23 @@ # Iterate through the XF Format objects and give them an index to non-default # border elements. # def prepare_borders #:nodoc: borders = {} - index = 0 - @xf_formats.each do |format| key = format.get_border_key - - if borders[key] - # Border has already been used. - format.border_index = borders[key] - format.has_border = 0 - else - # This is a new border. - borders[key] = index - format.border_index = index - format.has_border = 1 - index += 1 - end + format.has_border(!borders[key]) + format.border_index = borders[key] || borders.size + borders[key] ||= borders.size end - @border_count = index + @border_count = borders.size # For the DXF formats we only need to check if the properties have changed. @dxf_formats.each do |format| key = format.get_border_key - format.has_dxf_border = 1 if key =~ /[^0:]/ + format.has_dxf_border(true) if key =~ /[^0:]/ end end # # Iterate through the XF Format objects and give them an index to non-default @@ -1175,24 +1138,24 @@ key = format.get_fill_key if fills[key] # Fill has already been used. format.fill_index = fills[key] - format.has_fill = 0 + format.has_fill(false) else # This is a new fill. fills[key] = index format.fill_index = index - format.has_fill = 1 + format.has_fill(true) index += 1 end end @fill_count = index # For the DXF formats we only need to check if the properties have changed. @dxf_formats.each do |format| - format.has_dxf_fill = 1 if format.pattern || format.bg_color || format.fg_color + format.has_dxf_fill(true) if format.pattern || format.bg_color || format.fg_color end end # # Iterate through the worksheets and store any defined names in addition to