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

- old
+ new

@@ -27,10 +27,18 @@ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end yield self if block_given? end + # Shortcut to specify that the workbook should use autowidth + # @see Workbook#use_autowidth + def use_autowidth=(v) + Axlsx::validate_boolean(v); + workbook.use_autowidth = v + end + + # Shortcut to specify that the workbook should use shared strings # @see Workbook#use_shared_strings def use_shared_strings=(v) Axlsx::validate_boolean(v); workbook.use_shared_strings = v @@ -129,11 +137,13 @@ # p = Axlsx::Package.new # # ... code to create sheets, charts, styles etc. # p.validate.each { |error| puts error.message } def validate errors = [] - parts.each { |part| errors.concat validate_single_doc(part[:schema], part[:doc]) unless part[:schema].nil? } + parts.each do |part| + errors.concat validate_single_doc(part[:schema], part[:doc]) unless part[:schema].nil? + end errors end private @@ -160,38 +170,42 @@ # The parts of a package # @return [Array] An array of hashes that define the entry, document and schema for each part of the package. # @private def parts @parts = [ - {:entry => RELS_PN, :doc => relationships.to_xml, :schema => RELS_XSD}, - {:entry => "xl/#{STYLES_PN}", :doc => workbook.styles.to_xml, :schema => SML_XSD}, - {:entry => CORE_PN, :doc => @core.to_xml, :schema => CORE_XSD}, - {:entry => APP_PN, :doc => @app.to_xml, :schema => APP_XSD}, - {:entry => WORKBOOK_RELS_PN, :doc => workbook.relationships.to_xml, :schema => RELS_XSD}, - {:entry => CONTENT_TYPES_PN, :doc => content_types.to_xml, :schema => CONTENT_TYPES_XSD}, - {:entry => WORKBOOK_PN, :doc => workbook.to_xml, :schema => SML_XSD} + {:entry => RELS_PN, :doc => relationships.to_xml_string, :schema => RELS_XSD}, + {:entry => "xl/#{STYLES_PN}", :doc => workbook.styles.to_xml_string, :schema => SML_XSD}, + {:entry => CORE_PN, :doc => @core.to_xml_string, :schema => CORE_XSD}, + {:entry => APP_PN, :doc => @app.to_xml_string, :schema => APP_XSD}, + {:entry => WORKBOOK_RELS_PN, :doc => workbook.relationships.to_xml_string, :schema => RELS_XSD}, + {:entry => CONTENT_TYPES_PN, :doc => content_types.to_xml_string, :schema => CONTENT_TYPES_XSD}, + {:entry => WORKBOOK_PN, :doc => workbook.to_xml_string, :schema => SML_XSD} ] workbook.drawings.each do |drawing| - @parts << {:entry => "xl/#{drawing.rels_pn}", :doc => drawing.relationships.to_xml, :schema => RELS_XSD} - @parts << {:entry => "xl/#{drawing.pn}", :doc => drawing.to_xml, :schema => DRAWING_XSD} + @parts << {:entry => "xl/#{drawing.rels_pn}", :doc => drawing.relationships.to_xml_string, :schema => RELS_XSD} + @parts << {:entry => "xl/#{drawing.pn}", :doc => drawing.to_xml_string, :schema => DRAWING_XSD} end + workbook.tables.each do |table| + @parts << {:entry => "xl/#{table.pn}", :doc => table.to_xml_string, :schema => SML_XSD} + end + workbook.charts.each do |chart| - @parts << {:entry => "xl/#{chart.pn}", :doc => chart.to_xml, :schema => DRAWING_XSD} + @parts << {:entry => "xl/#{chart.pn}", :doc => chart.to_xml_string, :schema => DRAWING_XSD} end workbook.images.each do |image| @parts << {:entry => "xl/#{image.pn}", :path => image.image_src} end if use_shared_strings - @parts << {:entry => "xl/#{SHARED_STRINGS_PN}", :doc => workbook.shared_strings.to_xml, :schema => SML_XSD} + @parts << {:entry => "xl/#{SHARED_STRINGS_PN}", :doc => workbook.shared_strings.to_xml_string, :schema => SML_XSD} end workbook.worksheets.each do |sheet| - @parts << {:entry => "xl/#{sheet.rels_pn}", :doc => sheet.relationships.to_xml, :schema => RELS_XSD} - @parts << {:entry => "xl/#{sheet.pn}", :doc => sheet.to_xml, :schema => SML_XSD} + @parts << {:entry => "xl/#{sheet.rels_pn}", :doc => sheet.relationships.to_xml_string, :schema => RELS_XSD} + @parts << {:entry => "xl/#{sheet.pn}", :doc => sheet.to_xml_string, :schema => SML_XSD} end @parts end # Performs xsd validation for a signle document @@ -201,11 +215,10 @@ # @return [Array] An array of all validation errors encountered. # @private def validate_single_doc(schema, doc) schema = Nokogiri::XML::Schema(File.open(schema)) doc = Nokogiri::XML(doc) - errors = [] schema.validate(doc).each do |error| errors << error end errors @@ -221,9 +234,13 @@ :ContentType => DRAWING_CT) end workbook.charts.each do |chart| c_types << Axlsx::Override.new(:PartName => "/xl/#{chart.pn}", :ContentType => CHART_CT) + end + workbook.tables.each do |table| + c_types << Axlsx::Override.new(:PartName => "/xl/#{table.pn}", + :ContentType => TABLE_CT) end workbook.worksheets.each do |sheet| c_types << Axlsx::Override.new(:PartName => "/xl/#{sheet.pn}", :ContentType => WORKSHEET_CT) end