diff options
| author | Randy Morgan <[email protected]> | 2012-04-01 00:35:26 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-04-01 00:35:26 +0900 |
| commit | 22a341841f191a5aa00e87b1f166b4f25cc67f0a (patch) | |
| tree | 505f46708d5cac7d33d0dd6679c125e2eb819075 /lib | |
| parent | bb2117ba17297e02a0fc6d5ad5a22462e72a9a79 (diff) | |
| download | caxlsx-22a341841f191a5aa00e87b1f166b4f25cc67f0a.tar.gz caxlsx-22a341841f191a5aa00e87b1f166b4f25cc67f0a.zip | |
part way through changing all serialization to use string concatenation prior to dropping Nokogiri dep in production.
Diffstat (limited to 'lib')
39 files changed, 547 insertions, 326 deletions
diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 09f351fc..dd628562 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -81,7 +81,7 @@ module Axlsx # @example Relative Cell Reference # ws.rows.first.cells.first.r #=> "A1" def self.cell_r(c_index, r_index) - Axlsx::col_ref(c_index).to_s << (r_index+1).to_s - end + Axlsx::col_ref(c_index).to_s << (r_index+1).to_s + end end diff --git a/lib/axlsx/content_type/content_type.rb b/lib/axlsx/content_type/content_type.rb index 8b58bf04..6b4facd0 100644 --- a/lib/axlsx/content_type/content_type.rb +++ b/lib/axlsx/content_type/content_type.rb @@ -10,15 +10,14 @@ module Axlsx super [Override, Default] end - # Generates the xml document for [Content_Types].xml - # @return [String] The document as a string. - def to_xml() - builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| - xml.Types(:xmlns => Axlsx::XML_NS_T) { - each { |type| type.to_xml(xml) } - } - end - builder.to_xml(:save_with => 0) + # serialize the content types + # @return [String] str + def to_xml_string(str = '') + str << '<?xml version="1.0" encoding="UTF-8"?>' + str << '<Types xmlns="' << XML_NS_T << '">' + each { |type| type.to_xml_string(str) } + str << '</Types>' end + end end diff --git a/lib/axlsx/content_type/default.rb b/lib/axlsx/content_type/default.rb index bd572b14..2ff24527 100644 --- a/lib/axlsx/content_type/default.rb +++ b/lib/axlsx/content_type/default.rb @@ -8,7 +8,7 @@ module Axlsx attr_reader :Extension # The type of content. - # @return [String] + # @return [String] attr_reader :ContentType #Creates a new Default object @@ -19,7 +19,7 @@ module Axlsx raise ArgumentError, "Extension and ContentType are required" unless options[:Extension] && options[:ContentType] options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + end end # Sets the file extension for this content type. def Extension=(v) Axlsx::validate_string v; @Extension = v end @@ -28,11 +28,11 @@ module Axlsx # @see Axlsx#validate_content_type def ContentType=(v) Axlsx::validate_content_type v; @ContentType = v end - # Serializes the object to xml - # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. - # @return [String] - def to_xml(xml) - xml.Default(self.instance_values) + def to_xml_string(str = '') + str << '<Default ' + str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ') + str << '/>' end + end end diff --git a/lib/axlsx/content_type/override.rb b/lib/axlsx/content_type/override.rb index 2513b8ff..665a538a 100644 --- a/lib/axlsx/content_type/override.rb +++ b/lib/axlsx/content_type/override.rb @@ -4,11 +4,11 @@ module Axlsx class Override # The type of content. - # @return [String] + # @return [String] attr_reader :ContentType # The name and location of the part. - # @return [String] + # @return [String] attr_reader :PartName #Creates a new Override object @@ -19,20 +19,22 @@ module Axlsx raise ArgumentError, "PartName and ContentType are required" unless options[:PartName] && options[:ContentType] options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + end end # The name and location of the part. def PartName=(v) Axlsx::validate_string v; @PartName = v end - # The content type. + # The content type. # @see Axlsx#validate_content_type def ContentType=(v) Axlsx::validate_content_type v; @ContentType = v end - # Serializes the Override object to xml - # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. - def to_xml(xml) - xml.Override(self.instance_values) + # Serialize the Override + def to_xml_string(str = '') + str << '<Override ' + str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ') + str << '/>' end + end end diff --git a/lib/axlsx/doc_props/app.rb b/lib/axlsx/doc_props/app.rb index 16966da4..1b5fc2ad 100644 --- a/lib/axlsx/doc_props/app.rb +++ b/lib/axlsx/doc_props/app.rb @@ -17,7 +17,7 @@ module Axlsx attr_reader :Manager # @return [String] The name of the company generating the document. - attr_reader :Company + attr_reader :Company # @return [Integer] The number of pages in the document. attr_reader :Pages @@ -43,7 +43,7 @@ module Axlsx # @return [Integer] The number of slides that have notes. attr_reader :Notes - # @return [Integer] The total amount of time spent editing. + # @return [Integer] The total amount of time spent editing. attr_reader :TotalTime # @return [Integer] The number of hidden slides. @@ -60,11 +60,11 @@ module Axlsx # @return [Integer] The number of characters in the document including spaces. attr_reader :CharactersWithSpaces - + # @return [Boolean] Indicates if the document is shared. attr_reader :ShareDoc - # @return [String] The base for hyper links in the document. + # @return [String] The base for hyper links in the document. attr_reader :HyperLinkBase # @return [Boolean] Indicates that the hyper links in the document have been changed. @@ -96,7 +96,7 @@ module Axlsx # @option options [Boolean] ScaleCrop # @option options [Boolean] LinksUpToDate # @option options [Integer] CharactersWithSpaces - # @option options [Boolean] ShareDoc + # @option options [Boolean] ShareDoc # @option options [String] HyperLinkBase # @option options [String] HyperlinksChanged # @option options [String] Application @@ -109,70 +109,67 @@ module Axlsx end # Sets the Template property of your app.xml file - def Template=(v) Axlsx::validate_string v; @Template = v; end + def Template=(v) Axlsx::validate_string v; @Template = v; end # Sets the Manager property of your app.xml file - def Manager=(v) Axlsx::validate_string v; @Manager = v; end + def Manager=(v) Axlsx::validate_string v; @Manager = v; end # Sets the Company property of your app.xml file - def Company=(v) Axlsx::validate_string v; @Company = v; end + def Company=(v) Axlsx::validate_string v; @Company = v; end # Sets the Pages property of your app.xml file - def Pages=(v) Axlsx::validate_int v; @Pages = v; end + def Pages=(v) Axlsx::validate_int v; @Pages = v; end # Sets the Words property of your app.xml file - def Words=(v) Axlsx::validate_int v; @Words = v; end + def Words=(v) Axlsx::validate_int v; @Words = v; end # Sets the Characters property of your app.xml file - def Characters=(v) Axlsx::validate_int v; @Characters = v; end + def Characters=(v) Axlsx::validate_int v; @Characters = v; end # Sets the PresentationFormat property of your app.xml file - def PresentationFormat=(v) Axlsx::validate_string v; @PresentationFormat = v; end + def PresentationFormat=(v) Axlsx::validate_string v; @PresentationFormat = v; end # Sets the Lines property of your app.xml file - def Lines=(v) Axlsx::validate_int v; @Lines = v; end + def Lines=(v) Axlsx::validate_int v; @Lines = v; end # Sets the Paragraphs property of your app.xml file - def Paragraphs=(v) Axlsx::validate_int v; @Paragraphs = v; end + def Paragraphs=(v) Axlsx::validate_int v; @Paragraphs = v; end # Sets the Slides property of your app.xml file - def Slides=(v) Axlsx::validate_int v; @Slides = v; end + def Slides=(v) Axlsx::validate_int v; @Slides = v; end # Sets the Notes property of your app.xml file - def Notes=(v) Axlsx::validate_int v; @Notes = v; end + def Notes=(v) Axlsx::validate_int v; @Notes = v; end # Sets the TotalTime property of your app.xml file - def TotalTime=(v) Axlsx::validate_int v; @TotalTime = v; end + def TotalTime=(v) Axlsx::validate_int v; @TotalTime = v; end # Sets the HiddenSlides property of your app.xml file - def HiddenSlides=(v) Axlsx::validate_int v; @HiddenSlides = v; end + def HiddenSlides=(v) Axlsx::validate_int v; @HiddenSlides = v; end # Sets the MMClips property of your app.xml file - def MMClips=(v) Axlsx::validate_int v; @MMClips = v; end + def MMClips=(v) Axlsx::validate_int v; @MMClips = v; end # Sets the ScaleCrop property of your app.xml file - def ScaleCrop=(v) Axlsx::validate_boolean v; @ScaleCrop = v; end + def ScaleCrop=(v) Axlsx::validate_boolean v; @ScaleCrop = v; end # Sets the LinksUpToDate property of your app.xml file - def LinksUpToDate=(v) Axlsx::validate_boolean v; @LinksUpToDate = v; end + def LinksUpToDate=(v) Axlsx::validate_boolean v; @LinksUpToDate = v; end # Sets the CharactersWithSpaces property of your app.xml file - def CharactersWithSpaces=(v) Axlsx::validate_int v; @CharactersWithSpaces = v; end + def CharactersWithSpaces=(v) Axlsx::validate_int v; @CharactersWithSpaces = v; end # Sets the ShareDoc property of your app.xml file - def ShareDoc=(v) Axlsx::validate_boolean v; @ShareDoc = v; end + def ShareDoc=(v) Axlsx::validate_boolean v; @ShareDoc = v; end # Sets the HyperLinkBase property of your app.xml file - def HyperLinkBase=(v) Axlsx::validate_string v; @HyperLinkBase = v; end + def HyperLinkBase=(v) Axlsx::validate_string v; @HyperLinkBase = v; end # Sets the HyperLinksChanged property of your app.xml file - def HyperlinksChanged=(v) Axlsx::validate_boolean v; @HyperlinksChanged = v; end + def HyperlinksChanged=(v) Axlsx::validate_boolean v; @HyperlinksChanged = v; end # Sets the Application property of your app.xml file - def Application=(v) Axlsx::validate_string v; @Application = v; end + def Application=(v) Axlsx::validate_string v; @Application = v; end # Sets the AppVersion property of your app.xml file - def AppVersion=(v) Axlsx::validate_string v; @AppVersion = v; end + def AppVersion=(v) Axlsx::validate_string v; @AppVersion = v; end # Sets the DocSecurity property of your app.xml file - def DocSecurity=(v) Axlsx::validate_int v; @DocSecurity = v; end - - # Generate an app.xml document - # @return [String] The document as a string - def to_xml() - builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| - xml.send(:Properties, :xmlns => APP_NS, :'xmlns:vt' => APP_NS_VT) { - self.instance_values.each do |name, value| - xml.send(name, value) - end - } - end - builder.to_xml(:save_with => 0) + def DocSecurity=(v) Axlsx::validate_int v; @DocSecurity = v; end + + # Serialize the app.xml document + # @return [String] + def to_xml_string(str = '') + str << '<?xml version="1.0" encoding="UTF-8"?>' + str << '<Properties xmlns="' << APP_NS << '" xmlns:vt="' << APP_NS_VT << '">' + str << instance_values.map { |key, value| '<' << key.to_s << '>' << value.to_s << '</' << key.to_s << '>' }.join + str << '</Properties>' end + end end diff --git a/lib/axlsx/doc_props/core.rb b/lib/axlsx/doc_props/core.rb index 96716a6b..7d9ba291 100644 --- a/lib/axlsx/doc_props/core.rb +++ b/lib/axlsx/doc_props/core.rb @@ -7,29 +7,23 @@ module Axlsx # The author of the document. By default this is 'axlsx' # @return [String] attr_accessor :creator - + # Creates a new Core object. # @option options [String] creator def initialize(options={}) - @creator = options[:creator] || 'axlsx' + @creator = options[:creator] || 'axlsx' end - # Serializes the core object. The created dcterms item is set to the current time when this method is called. + # serializes the core.xml document # @return [String] - def to_xml() - builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| - xml.send('cp:coreProperties', - :"xmlns:cp" => CORE_NS, - :'xmlns:dc' => CORE_NS_DC, - :'xmlns:dcmitype'=>CORE_NS_DCMIT, - :'xmlns:dcterms'=>CORE_NS_DCT, - :'xmlns:xsi'=>CORE_NS_XSI) { - xml['dc'].creator self.creator - xml['dcterms'].created Time.now.strftime('%Y-%m-%dT%H:%M:%S'), :'xsi:type'=>"dcterms:W3CDTF" - xml['cp'].revision 0 - } - end - builder.to_xml(:save_with => 0) + def to_xml_string(str = '') + str << '<?xml version="1.0" encoding="UTF-8"?>' + str << '<cp:coreProperties xmlns:cp="' << CORE_NS << '" xmlns:dc="' << CORE_NS_DC << '" ' + str << 'xmlns:dcmitype="' << CORE_NS_DCMIT << '" xmlns:dcterms="' << CORE_NS_DCT << '" ' + str << 'xmlns:xsi="' << CORE_NS_XSI << '">' + str << '<dc:creator>' << self.creator << '</dc:creator>' + str << '<dcterms:created xsi:type="dcterms:W3CDTF">' << Time.now.strftime('%Y-%m-%dT%H:%M:%S') << '</dcterms:created>' + str << '<cp:revision>0</cp:revision>' end end end diff --git a/lib/axlsx/drawing/axis.rb b/lib/axlsx/drawing/axis.rb index 183cbcad..8fbc3612 100644 --- a/lib/axlsx/drawing/axis.rb +++ b/lib/axlsx/drawing/axis.rb @@ -83,6 +83,29 @@ module Axlsx # must be one of [:autoZero, :min, :max] def crosses=(v) RestrictionValidator.validate "#{self.class}.crosses", [:autoZero, :min, :max], v; @crosses = v; end + + def to_xml_string(str = '') + str << '<axId val="' << @axId.to_s << '"/>' + @scaling.to_xml_string str + str << '<delete val="0"/>' + str << '<axPos val="' << @axPos.to_s << '"/>' + str << '<majorGridlines>' + if self.gridlines == false + str << '<spPr>' + str << '<a:ln>' + str << '<a:noFill/>' + str << '</a:ln>' + str << '</spPr>' + end + str << '</majorGridlines>' + str << '<numFmt formatCode="' << @format_code << '" sourceLinked="1"/>' + str << '<majorTickMark val="none"/>' + str << '<minorTickMark val="none"/>' + str << '<tickLblPos val="' << @tickLblPos.to_s << '"/>' + str << '<crossAx val="' << @crossAx.to_s << '"/>' + str << '<crosses val="' << @crosses.to_s << '"/>' + end + # Serializes the common axis # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] diff --git a/lib/axlsx/drawing/bar_3D_chart.rb b/lib/axlsx/drawing/bar_3D_chart.rb index de179cb6..dbed026a 100644 --- a/lib/axlsx/drawing/bar_3D_chart.rb +++ b/lib/axlsx/drawing/bar_3D_chart.rb @@ -41,7 +41,7 @@ module Axlsx # validation regex for gap amount percent GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/ - + # Creates a new bar chart object # @param [GraphicFrame] frame The workbook that owns this chart. # @option options [Cell, String] title @@ -67,14 +67,14 @@ module Axlsx @valAxId = rand(8 ** 8) @catAxis = CatAxis.new(@catAxId, @valAxId) @valAxis = ValAxis.new(@valAxId, @catAxId, :tickLblPos => :low) - super(frame, options) + super(frame, options) @series_type = BarSeries @view3D = View3D.new({:rAngAx=>1}.merge(options)) end # The direction of the bars in the chart # must be one of [:bar, :col] - def barDir=(v) + def barDir=(v) RestrictionValidator.validate "Bar3DChart.barDir", [:bar, :col], v @barDir = v end @@ -100,11 +100,37 @@ module Axlsx # The shabe of the bars or columns # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax] - def shape=(v) + def shape=(v) RestrictionValidator.validate "Bar3DChart.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v @shape = v end - + + def to_xml_string(str = '') + super do |str| + str << '<bar3DChart>' + str << '<barDir val="' << barDir.to_s << '"/>' + str << '<grouping val="' << grouping.to_s << '"/>' + str << '<varyColors val="1"/>' + @series.each { |ser| ser.to_xml_str(str) } + str << '<dLbls>' + str << '<showLegendKey val="0"/>' + str << '<showVal val="0"/>' + str << '<showCatName val="0"/>' + str << '<showSerName val="0"/>' + str << '<showPercent val="0"/>' + str << '<showBubbleSize val="0"/>' + str << '</dLbls>' + str << '<gapWidth val="' << @gapWidth.to_s << '"/>' unless @gapWidth.nil? + str << '<gapDepth val="' << @gapDepth.to_s << '"/>' unless @gapDepth.nil? + str << '<shape val="' << @shape.to_s << '"/>' + str << '<axId val="' << @catAxId.to_s << '"/>' + str << '<axId val="' << @valAxId.to_s << '"/>' + str << '<axId val="0"/>' + str << '</bar3DChart>' + @catAxis.to_xml_str str + @valAxis.to_xml_str str + end + end # Serializes the bar chart # @return [String] def to_xml @@ -120,7 +146,7 @@ module Axlsx xml.showCatName :val=>0 xml.showSerName :val=>0 xml.showPercent :val=>0 - xml.showBubbleSize :val=>0 + xml.showBubbleSize :val=>0 } xml.gapWidth :val=>@gapWidth unless @gapWidth.nil? xml.gapDepth :val=>@gapDepth unless @gapDepth.nil? @@ -130,8 +156,8 @@ module Axlsx xml.axId :val=>0 } @catAxis.to_xml(xml) - @valAxis.to_xml(xml) + @valAxis.to_xml(xml) end - end - end + end + end end diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb index 5be8fabc..3a3ea6fa 100644 --- a/lib/axlsx/drawing/bar_series.rb +++ b/lib/axlsx/drawing/bar_series.rb @@ -6,8 +6,8 @@ module Axlsx # @see Chart#add_series class BarSeries < Series - - # The data for this series. + + # The data for this series. # @return [Array, SimpleTypedList] attr_reader :data @@ -31,15 +31,23 @@ module Axlsx super(chart, options) self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? self.data = ValAxisData.new(options[:data]) unless options[:data].nil? - end + end # The shabe of the bars or columns # must be one of [:percentStacked, :clustered, :standard, :stacked] - def shape=(v) + def shape=(v) RestrictionValidator.validate "BarSeries.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v @shape = v end + def to_xml_string(str = '') + super(str) do + @labels.to_xml_string(str) unless @labels.nil? + @data.to_xml_string(str) unless @data.nil? + str << '<shape val="' << @shape.to_s << '"/>' + end + end + # Serializes the series # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] @@ -48,11 +56,11 @@ module Axlsx @labels.to_xml(xml_inner) unless @labels.nil? @data.to_xml(xml_inner) unless @data.nil? xml_inner.shape :val=>@shape - end + end end - - private + + private # assigns the data for this series def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end diff --git a/lib/axlsx/drawing/cat_axis.rb b/lib/axlsx/drawing/cat_axis.rb index ee408c56..242ae698 100644 --- a/lib/axlsx/drawing/cat_axis.rb +++ b/lib/axlsx/drawing/cat_axis.rb @@ -10,7 +10,7 @@ module Axlsx # specifies how the perpendicular axis is crossed # must be one of [:ctr, :l, :r] # @return [Symbol] - attr_reader :lblAlgn + attr_reader :lblAlgn # The offset of the labels # must be between a string between 0 and 1000 @@ -20,7 +20,7 @@ module Axlsx # regex for validating label offset LBL_OFFSET_REGEX = /0*(([0-9])|([1-9][0-9])|([1-9][0-9][0-9])|1000)%/ - # Creates a new CatAxis object + # Creates a new CatAxis object # @param [Integer] axId the id of this axis. Inherited # @param [Integer] crossAx the id of the perpendicular axis. Inherited # @option options [Symbol] axPos. Inherited @@ -28,13 +28,13 @@ module Axlsx # @option options [Symbol] crosses. Inherited # @option options [Boolean] auto # @option options [Symbol] lblAlgn - # @option options [Integer] lblOffset + # @option options [Integer] lblOffset def initialize(axId, crossAx, options={}) self.auto = 1 self.lblAlgn = :ctr self.lblOffset = "100%" super(axId, crossAx, options) - end + end # From the docs: This element specifies that this axis is a date or text axis based on the data that is used for the axis labels, not a specific choice. def auto=(v) Axlsx::validate_boolean(v); @auto = v; end @@ -47,6 +47,14 @@ module Axlsx # must be between a string between 0 and 1000 def lblOffset=(v) RegexValidator.validate "#{self.class}.lblOffset", LBL_OFFSET_REGEX, v; @lblOffset = v; end + + def to_xml_string(str = '') + str << '<catAx>' + super(str) + str << '<auto val="' << @auto.to_s << '</auto>' + str << '</catAx>' + end + # Serializes the category axis # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] @@ -59,6 +67,6 @@ module Axlsx } end end - + end diff --git a/lib/axlsx/drawing/cat_axis_data.rb b/lib/axlsx/drawing/cat_axis_data.rb index b9b57d01..a119282e 100644 --- a/lib/axlsx/drawing/cat_axis_data.rb +++ b/lib/axlsx/drawing/cat_axis_data.rb @@ -11,6 +11,22 @@ module Axlsx data.each { |i| @list << i } if data.is_a?(SimpleTypedList) end + + def to_xml_string(str = '') + str << '<cat>' + str << '<strRef>' + str << '<f>' << Axlsx::cell_range(@list) << '</f>' + str << '<strCache>' + str << '<ptCount val="' << size.to_s << '"/>' + each_with_index do |item, index| + v = item.is_a?(Cell) ? item.value.to_s : item + str << '<pt idx="' << index.to_s << '"><v>' << v << '</v></pt>' + end + str << '</strCache>' + str << '</strRef>' + str << '</cat>' + end + # Serializes the category axis data # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] @@ -24,7 +40,7 @@ module Axlsx v = item.is_a?(Cell) ? item.value : item xml.pt(:idx=>index) { xml.v v - } + } end } } @@ -32,5 +48,5 @@ module Axlsx end end - + end diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb index b612fbed..a0bc75ba 100644 --- a/lib/axlsx/drawing/chart.rb +++ b/lib/axlsx/drawing/chart.rb @@ -113,6 +113,37 @@ module Axlsx @series.last end + + def to_xml_string + str << '<?xml version="1.0" encoding="UTF-8"?>' + str << '<c:chartSpace xmlns:c="' << XML_NS_C << '" xmlns:a="' << XML_NS_A << '">' + str << '<c:date1904 val="' << Axlsx::Workbook.date1904.to_s << '"/>' + str << '<c:style val="' << style.to_s << '"/>' + str << '<c:chart>' + @title.to_xml_string str + # do these need the c: namespace as well??? + str << '<autoTitleDeleted val="0"/>' + @view3D.to_xml_string(str) if @view3D + str << '<floor thickness="0"/>' + str << '<sideWall thickness="0"/>' + str << '<backWall thickness="0"/>' + str << '<plotArea>' + str << '<layout/>' + yield str if block_given? + str << '</plotArea>' + if @show_legend + str << '<legend>' + str << '<legendPos val="r"/>' + str << '<layout/>' + str << '<overlay val="0"/>' + str << '</legend>' + end + str << '<plotVisOnly val="1"/>' + str << '<dispBlanksAs val="zero"/>' + str << '<showDLblsOverMax val="1"/>' + str << '</c:chart>' + str << '</c:chartSpace>' + end # Chart Serialization # serializes the chart def to_xml @@ -133,7 +164,7 @@ module Axlsx yield xml if block_given? } if @show_legend - xml.legend { + xml.legend { xml.legendPos :val => "r" xml.layout xml.overlay :val => 0 diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index 93068a23..08b8531a 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -4,8 +4,8 @@ module Axlsx require 'axlsx/drawing/series_title.rb' require 'axlsx/drawing/series.rb' require 'axlsx/drawing/pie_series.rb' - require 'axlsx/drawing/bar_series.rb' - require 'axlsx/drawing/line_series.rb' + require 'axlsx/drawing/bar_series.rb' + require 'axlsx/drawing/line_series.rb' require 'axlsx/drawing/scatter_series.rb' require 'axlsx/drawing/scaling.rb' @@ -19,7 +19,7 @@ module Axlsx require 'axlsx/drawing/named_axis_data.rb' require 'axlsx/drawing/marker.rb' - + require 'axlsx/drawing/one_cell_anchor.rb' require 'axlsx/drawing/two_cell_anchor.rb' require 'axlsx/drawing/graphic_frame.rb' @@ -46,7 +46,7 @@ module Axlsx # The worksheet that owns the drawing # @return [Worksheet] attr_reader :worksheet - + # A collection of anchors for this drawing # only TwoCellAnchors are supported in this version # @return [SimpleTypedList] @@ -138,13 +138,20 @@ module Axlsx r end + def to_xml_string(str = '') + str << '<?xml version="1.0" encoding="UTF-8"?>' + str << '<xdr:wsDr xmlns:xdr="' << XML_NS_XDR << '" xmlns:a="' << XML_NS_A << '" xmlns:c="' << XML_NS_C << '">' + anchors.each { |anchor| anchor.to_xml_string(str) } + str << '</xdr:wsDr>' + end + # Serializes the drawing # @return [String] def to_xml builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| xml.send('xdr:wsDr', :'xmlns:xdr'=>XML_NS_XDR, :'xmlns:a'=>XML_NS_A, :'xmlns:c'=>XML_NS_C) { anchors.each {|anchor| anchor.to_xml(xml) } - } + } end builder.to_xml(:save_with => 0) end diff --git a/lib/axlsx/drawing/graphic_frame.rb b/lib/axlsx/drawing/graphic_frame.rb index 178e0ea8..7502dfba 100644 --- a/lib/axlsx/drawing/graphic_frame.rb +++ b/lib/axlsx/drawing/graphic_frame.rb @@ -28,6 +28,24 @@ module Axlsx "rId#{@anchor.index+1}" end + def to_xml_string(str = '') + str << '<graphicFrame>' + str << '<nvGraphicFramePr>' + str << '<cNvPr id="2" name="' << chart.title.text << '"/>' + str << '<cNvGraphicFramePr/>' + str << '</nvGraphicFramePr>' + str << '<xfrm>' + str << '<a:off x="0" y="0"/>' + str << '<a:ext cx="0" cy="0"/>' + str << '</xfrm>' + str << '<a:graphic>' + str << '<graphicData uri="' << XML_NS_C << '">' + str << '<c:chart xmlns:c="' << XML_NS_C << '" xmlns:r="' << XML_NS_R << '" r:id="' << rId.to_s << '"/>' + str << '</graphicData>' + str << '</a:graphic>' + str << '</graphicFrame>' + end + # Serializes the graphic frame # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] diff --git a/lib/axlsx/drawing/hyperlink.rb b/lib/axlsx/drawing/hyperlink.rb index 67b08f29..9cfdf705 100644 --- a/lib/axlsx/drawing/hyperlink.rb +++ b/lib/axlsx/drawing/hyperlink.rb @@ -31,12 +31,12 @@ module Axlsx # indicates that the link has already been clicked. # @return [Boolean] attr_reader :highlightClick - + # @see highlightClick # @param [Boolean] v The value to assign def highlightClick=(v) Axlsx::validate_boolean(v); @highlightClick = v end - # From the specs: Specifies whether to add this URI to the history when navigating to it. This allows for the viewing of this presentation without the storing of history information on the viewing machine. If this attribute is omitted, then a value of 1 or true is assumed. + # From the specs: Specifies whether to add this URI to the history when navigating to it. This allows for the viewing of this presentation without the storing of history information on the viewing machine. If this attribute is omitted, then a value of 1 or true is assumed. # @return [Boolean] attr_reader :history @@ -52,12 +52,12 @@ module Axlsx # @return [String] attr_accessor :tooltip - #Creates a hyperlink object + #Creates a hyperlink object # parent must be a Pic for now, although I expect that other object support this tag and its cNvPr parent # @param [Pic] parent # @option options [String] tooltip message shown when hyperlinked object is hovered over with mouse. # @option options [String] tgtFrame Target frame for opening hyperlink - # @option options [String] invalidUrl supposedly use to store the href when we know it is an invalid resource. + # @option options [String] invalidUrl supposedly use to store the href when we know it is an invalid resource. # @option options [String] href the target resource this hyperlink links to. # @option options [String] action A string that can be used to perform specific actions. For excel please see this reference: http://msdn.microsoft.com/en-us/library/ff532419%28v=office.12%29.aspx # @option options [Boolean] endSnd terminate any sound events when processing this link @@ -70,14 +70,22 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end yield self if block_given? - + end + def to_xml_string(str = '') + h = self.instance_values.merge({:'r:id' => "rId#{id}", :'xmlns:r' => XML_NS_R }) + h.delete('href') + h.delete('parent') + str << '<a:hlinkClick ' + str << h.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ') + str << '/>' + end # Serializes the hyperlink # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] - def to_xml(xml) + def to_xml(xml) h = self.instance_values.merge({:'r:id' => "rId#{id}", :'xmlns:r' => XML_NS_R }) h.delete('href') h.delete('parent') @@ -85,7 +93,7 @@ module Axlsx end private - # The relational ID for this hyperlink + # The relational ID for this hyperlink # @return [Integer] def id @parent.anchor.drawing.charts.size + @parent.anchor.drawing.images.size + @parent.anchor.drawing.hyperlinks.index(self) + 1 diff --git a/lib/axlsx/drawing/line_3D_chart.rb b/lib/axlsx/drawing/line_3D_chart.rb index 13e42dff..bd587d1b 100644 --- a/lib/axlsx/drawing/line_3D_chart.rb +++ b/lib/axlsx/drawing/line_3D_chart.rb @@ -44,11 +44,11 @@ module Axlsx # validation regex for gap amount percent GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/ - + # Creates a new line chart object # @param [GraphicFrame] frame The workbook that owns this chart. # @option options [Cell, String] title - # @option options [Boolean] show_legend + # @option options [Boolean] show_legend # @option options [Symbol] grouping # @option options [String] gapDepth # @option options [Integer] rotX @@ -68,7 +68,7 @@ module Axlsx @catAxis = CatAxis.new(@catAxId, @valAxId) @valAxis = ValAxis.new(@valAxId, @catAxId) @serAxis = SerAxis.new(@serAxId, @valAxId) - super(frame, options) + super(frame, options) @series_type = LineSeries @view3D = View3D.new({:perspective=>30}.merge(options)) end @@ -85,6 +85,31 @@ module Axlsx @gapDepth=(v) end + def to_xml_string(str = '') + super do |str| + str << '<line3DChart>' + str << '<grouping val="' << grouping.to_s << '"/>' + str << '<varyColors val="1"/>' + @series.each { |ser| ser.to_xml_str(str) } + str << '<dLbls>' + str << '<showLegendKey val="0"/>' + str << '<showVal val="0"/>' + str << '<showCatName val="0"/>' + str << '<showSerName val="0"/>' + str << '<showPercent val="0"/>' + str << '<showBubbleSize val="0"/>' + str << '</dLbls>' + str << '<gapDepth val="' << @gapDepth.to_s << '"/>' unless @gapDepth.nil? + str << '<axId val="' << @catAxId.to_s << '"/>' + str << '<axId val="' << @valAxId.to_s << '"/>' + str << '<axId val="' << @serAxId.to_s << '"/>' + str << '</line3DChart>' + @catAxis.to_xml_str str + @valAxis.to_xml_str str + @serAxis.to_xml_str str + end + end + # Serializes the bar chart # @return [String] def to_xml @@ -99,7 +124,7 @@ module Axlsx xml.showCatName :val=>0 xml.showSerName :val=>0 xml.showPercent :val=>0 - xml.showBubbleSize :val=>0 + xml.showBubbleSize :val=>0 } xml.gapDepth :val=>@gapDepth unless @gapDepth.nil? xml.axId :val=>@catAxId @@ -110,6 +135,6 @@ module Axlsx @valAxis.to_xml(xml) @serAxis.to_xml(xml) end - end - end + end + end end diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb index a7de888f..c5908f64 100644 --- a/lib/axlsx/drawing/line_series.rb +++ b/lib/axlsx/drawing/line_series.rb @@ -5,8 +5,8 @@ module Axlsx # @see Worksheet#add_chart # @see Chart#add_series class LineSeries < Series - - # The data for this series. + + # The data for this series. # @return [ValAxisData] attr_reader :data @@ -23,7 +23,14 @@ module Axlsx super(chart, options) @labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? @data = ValAxisData.new(options[:data]) unless options[:data].nil? - end + end + + def to_xml_string(str = '') + super(str) do + @labels.to_xml_string(str) unless @labels.nil? + @data.to_xml_string(str) unless @data.nil? + end + end # Serializes the series # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. @@ -32,10 +39,10 @@ module Axlsx super(xml) do |xml_inner| @labels.to_xml(xml_inner) unless @labels.nil? @data.to_xml(xml_inner) unless @data.nil? - end + end end - private + private # assigns the data for this series def data=(v) DataTypeValidator.validate "Series.data", [SimpleTypedList], v; @data = v; end diff --git a/lib/axlsx/drawing/marker.rb b/lib/axlsx/drawing/marker.rb index dac8604e..a2ce4312 100644 --- a/lib/axlsx/drawing/marker.rb +++ b/lib/axlsx/drawing/marker.rb @@ -30,9 +30,9 @@ module Axlsx @col, @colOff, @row, @rowOff = 0, 0, 0, 0 options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] - end + end end - + # @see col def col=(v) Axlsx::validate_unsigned_int v; @col = v end # @see colOff @@ -41,7 +41,7 @@ module Axlsx def row=(v) Axlsx::validate_unsigned_int v; @row = v end # @see rowOff def rowOff=(v) Axlsx::validate_int v; @rowOff = v end - + # shortcut to set the column, row position for this marker # @param col the column for the marker # @param row the row of the marker @@ -49,13 +49,20 @@ module Axlsx self.col = col self.row = row end + + def to_xml_string(str = '') + [:col, :colOff, :row, :rowOff].each do |k| + str << '<' << k.to_s << '>' << self.send(k).to_s << '</' << k.to_s << '>' + end + end + # Serializes the marker # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] def to_xml(xml) [:col, :colOff, :row, :rowOff].each do |k| xml.send(k.to_sym, self.send(k)) - end + end end end diff --git a/lib/axlsx/drawing/named_axis_data.rb b/lib/axlsx/drawing/named_axis_data.rb index b249196f..2ec58e8b 100644 --- a/lib/axlsx/drawing/named_axis_data.rb +++ b/lib/axlsx/drawing/named_axis_data.rb @@ -8,6 +8,23 @@ module Axlsx @name = name end + + def to_xml_string(str = '') + str << '<' << @name << '>' + str << '<numRef>' + str << '<f>' << Axlsx::cell_range(@list) << '</f>' + str << '<numCache>' + str << '<formatCode>General</formatCode>' + str << '<ptCount val="' << size.to_s << '"/>' + each_with_index do |item, index| + v = item.is_a?(Cell) ? item.value.to_s : item + str << '<pt idx="' << index.to_s << '"><v>' << v << '</v></pt>' + end + str << '</numCache>' + str << '</numRef>' + str << '</' << @name << '>' + end + # Serializes the value axis data # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] diff --git a/lib/axlsx/drawing/one_cell_anchor.rb b/lib/axlsx/drawing/one_cell_anchor.rb index 2cd43331..e0574172 100644 --- a/lib/axlsx/drawing/one_cell_anchor.rb +++ b/lib/axlsx/drawing/one_cell_anchor.rb @@ -33,15 +33,15 @@ module Axlsx # @param [Drawing] drawing # @option options [Array] start_at the col, row to start at # @option options [Integer] width - # @option options [Integer] height + # @option options [Integer] height # @option options [String] image_src the file location of the image you will render # @option options [String] name the name attribute for the rendered image - # @option options [String] descr the description of the image rendered + # @option options [String] descr the description of the image rendered def initialize(drawing, options={}) @drawing = drawing @width = 0 @height = 0 - drawing.anchors << self + drawing.anchors << self @from = Marker.new options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" @@ -61,6 +61,18 @@ module Axlsx @drawing.anchors.index(self) end + + def to_xml_string(str = '') + str << '<xdr:oneCellAnchor>' + str << '<from>' + from.to_xml_string(str) + str << '</from>' + str << '<ext>' << ext.to_s << '</ext>' + @object.to_xml_string(str) + str << '<clientData/>' + str << '</xdr:oneCellAnchor>' + end + # Serializes the anchor # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] @@ -73,11 +85,11 @@ module Axlsx @object.to_xml(xml) xml.clientData } - end + end private - # converts the pixel width and height to EMU units and returns a hash of + # converts the pixel width and height to EMU units and returns a hash of # !{:cx=>[Integer], :cy=>[Integer] # @return [Hash] def ext diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index 754a7e35..1783005a 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -28,7 +28,7 @@ module Axlsx # The picture locking attributes for this picture attr_reader :picture_locking - + # Creates a new Pic(ture) object # @param [Anchor] anchor the anchor that holds this image # @option options [String] name @@ -50,7 +50,7 @@ module Axlsx end attr_reader :hyperlink - + # sets or updates a hyperlink for this image. # @param [String] v The href value for the hyper link # @option options @see Hyperlink#initialize All options available to the Hyperlink class apply - however href will be overridden with the v parameter value. @@ -66,7 +66,7 @@ module Axlsx @hyperlink end - def image_src=(v) + def image_src=(v) Axlsx::validate_string(v) RestrictionValidator.validate 'Pic.image_src', ALLOWED_EXTENSIONS, File.extname(v).delete('.') raise ArgumentError, "File does not exist" unless File.exist?(v) @@ -84,8 +84,8 @@ module Axlsx # @return [String] def file_name File.basename(image_src) unless image_src.nil? - end - + end + # returns the extension of image_src without the preceeding '.' # @return [String] def extname @@ -93,7 +93,7 @@ module Axlsx end # The index of this image in the workbooks images collections - # @return [Index] + # @return [Index] def index @anchor.drawing.worksheet.workbook.images.index(self) end @@ -120,7 +120,7 @@ module Axlsx def width=(v) @anchor.width = v end - + # providing access to update the anchor's height attribute # @param [Integer] v # @see OneCellAnchor.width @@ -144,6 +144,10 @@ module Axlsx @anchor.from.row = y end + def to_xml_string(str = '') + + end + # Serializes the picture # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] diff --git a/lib/axlsx/drawing/scaling.rb b/lib/axlsx/drawing/scaling.rb index 628aa016..c90d0377 100644 --- a/lib/axlsx/drawing/scaling.rb +++ b/lib/axlsx/drawing/scaling.rb @@ -33,7 +33,7 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end - + # @see logBase def logBase=(v) DataTypeValidator.validate "Scaling.logBase", [Integer, Fixnum], v, lambda { |arg| arg >= 2 && arg <= 1000}; @logBase = v; end # @see orientation @@ -44,6 +44,15 @@ module Axlsx # @see min def min=(v) DataTypeValidator.validate "Scaling.min", Float, v; @min = v; end + def to_xml_string(str = '') + str << '<c:scaling>' + str << '<c:logBase val="' << @logBase.to_s << '"/>' + str << '<c:orientation val="' << @orientation.to_s << '"/>' + str << '<c:min val="' << @min.to_s << '"/>' + str << '<c:max val="' << @max.to_s << '"/>' + str << '</c:scaling>' + end + # Serializes the axId # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] diff --git a/lib/axlsx/drawing/ser_axis.rb b/lib/axlsx/drawing/ser_axis.rb index 6fc5fec0..54dde640 100644 --- a/lib/axlsx/drawing/ser_axis.rb +++ b/lib/axlsx/drawing/ser_axis.rb @@ -22,7 +22,7 @@ module Axlsx def initialize(axId, crossAx, options={}) @tickLblSkip, @tickMarkSkip = nil, nil super(axId, crossAx, options) - end + end # @see tickLblSkip def tickLblSkip=(v) Axlsx::validate_unsigned_int(v); @tickLblSkip = v; end @@ -30,6 +30,13 @@ module Axlsx # @see tickMarkSkip def tickMarkSkip=(v) Axlsx::validate_unsigned_int(v); @tickMarkSkip = v; end + def to_xml_string(str = '') + str << '<c:serAx>' + super(str) + str << '<c:tickLblSkip val="' << @tickLblSkip.to_s << '"/>' unless @tickLblSkip.nil? + str << '<c:tickMarkSkip val="' << @tickMarkSkip.to_s << '"/>' unless @tickMarkSkip.nil? + str << '</c:serAx>' + end # Serializes the series axis # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] @@ -41,6 +48,6 @@ module Axlsx } end end - + end diff --git a/lib/axlsx/drawing/series.rb b/lib/axlsx/drawing/series.rb index 1544bc48..5960f508 100644 --- a/lib/axlsx/drawing/series.rb +++ b/lib/axlsx/drawing/series.rb @@ -25,7 +25,7 @@ module Axlsx options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end - end + end # The index of this series in the chart's series. # @return [Integer] @@ -44,16 +44,25 @@ module Axlsx def order=(v) Axlsx::validate_unsigned_int(v); @order = v; end # @see title - def title=(v) + def title=(v) v = SeriesTitle.new(v) if v.is_a?(String) || v.is_a?(Cell) DataTypeValidator.validate "#{self.class}.title", SeriesTitle, v @title = v end - - private - + + private + # assigns the chart for this series - def chart=(v) DataTypeValidator.validate "Series.chart", Chart, v; @chart = v; end + def chart=(v) DataTypeValidator.validate "Series.chart", Chart, v; @chart = v; end + + def to_xml_string(str = '') + str << '<ser>' + str << '<idx val="' << index.to_s << '"/>' + str << '<order val="' << (order || index).to_s << '"/>' + title.to_xml_string(str) unless title.nil? + yeild str if block_given? + str << '</ser>' + end # Serializes the series # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. diff --git a/lib/axlsx/drawing/title.rb b/lib/axlsx/drawing/title.rb index cc382205..a7b8e715 100644 --- a/lib/axlsx/drawing/title.rb +++ b/lib/axlsx/drawing/title.rb @@ -2,7 +2,7 @@ module Axlsx # A Title stores information about the title of a chart class Title - + # The text to be shown. Setting this property directly with a string will remove the cell reference. # @return [String] attr_reader :text @@ -17,9 +17,9 @@ module Axlsx self.cell = title if title.is_a?(Cell) self.text = title.to_s unless title.is_a?(Cell) end - + # @see text - def text=(v) + def text=(v) DataTypeValidator.validate 'Title.text', String, v @text = v @cell = nil @@ -30,7 +30,7 @@ module Axlsx def cell=(v) DataTypeValidator.validate 'Title.text', Cell, v @cell = v - @text = v.value.to_s + @text = v.value.to_s v end @@ -38,7 +38,25 @@ module Axlsx #def layout=(v) DataTypeValidator.validate 'Title.layout', Layout, v; @layout = v; end #def overlay=(v) Axlsx::validate_boolean v; @overlay=v; end #def spPr=(v) DataTypeValidator.validate 'Title.spPr', SpPr, v; @spPr = v; end - + + def to_xml_string(str = '') + str << '<c:title>' + unless @text.empty? + str << '<c:tx>' + str << '<c:strRef>' + str << '<c:f>' << Axlsx::cell_range([@cell]) << '</c:f>' + str << '<c:strCache>' + str << '<c:ptCount val="1"/>' + str << '<c:pt idx="0">' + str << '<c:v>' << @text << '</c:v>' + str << '</c:pt>' + str << '</c:strCache>' + str << '</c:strRef>' + str << '</c:tx>' + end + str << '</c:title>' + end + # Serializes the chart title # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] @@ -59,8 +77,8 @@ module Axlsx end xml[:c].layout xml[:c].overlay :val=>0 - } + } end - + end end diff --git a/lib/axlsx/drawing/val_axis.rb b/lib/axlsx/drawing/val_axis.rb index c7091d49..51adc31a 100644 --- a/lib/axlsx/drawing/val_axis.rb +++ b/lib/axlsx/drawing/val_axis.rb @@ -22,6 +22,13 @@ module Axlsx # @see crossBetween def crossBetween=(v) RestrictionValidator.validate "ValAxis.crossBetween", [:between, :midCat], v; @crossBetween = v; end + def to_xml_string(str = '') + str << '<valAx>' + super(str) + str << '<crossBetween val="' << @crossBetween.to_s << '"/>' + str << '</valAx>' + end + # Serializes the value axis # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] diff --git a/lib/axlsx/drawing/val_axis_data.rb b/lib/axlsx/drawing/val_axis_data.rb index 6b950443..b974b5d1 100644 --- a/lib/axlsx/drawing/val_axis_data.rb +++ b/lib/axlsx/drawing/val_axis_data.rb @@ -3,6 +3,22 @@ module Axlsx # The ValAxisData class manages the values for a chart value series. class ValAxisData < CatAxisData + def to_xml_string(str = '') + str << '<val>' + str << '<numRef>' + str << '<f>' << Axlsx::cell_range(@list) << '</f>' + str << '<numCache>' + str << '<formatCode>General</formatCode>' + str << '<ptCount val="' << size.to_s << '"/>' + each_with_index do |item, index| + v = item.is_a?(Cell) ? item.value.to_s : item + str << '<pt idx="' << index.to_s << '"><v>' << v << '</v></pt>' + end + str << '</numCache>' + str << '</numRef>' + str << '</val>' + end + # Serializes the value axis data # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] @@ -17,11 +33,11 @@ module Axlsx v = item.is_a?(Cell) ? item.value : item xml.pt(:idx=>index) { xml.v v } end - } + } } } end end - + end diff --git a/lib/axlsx/drawing/view_3D.rb b/lib/axlsx/drawing/view_3D.rb index d8a569b1..ee2739b4 100644 --- a/lib/axlsx/drawing/view_3D.rb +++ b/lib/axlsx/drawing/view_3D.rb @@ -5,34 +5,34 @@ module Axlsx # Validation for hPercent H_PERCENT_REGEX = /0*(([5-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/ - + # validation for depthPercent DEPTH_PERCENT_REGEX = /0*(([2-9][0-9])|([1-9][0-9][0-9])|(1[0-9][0-9][0-9])|2000)%/ - # x rotation for the chart + # x rotation for the chart # must be between -90 and 90 # @return [Integer] attr_reader :rotX - + # height of chart as % of chart # must be between 5% and 500% # @return [String] attr_reader :hPercent - + # y rotation for the chart # must be between 0 and 360 # @return [Integer] attr_reader :rotY - + # depth or chart as % of chart width # must be between 20% and 2000% # @return [String] attr_reader :depthPercent - + # Chart axis are at right angles # @return [Boolean] attr_reader :rAngAx - + # field of view angle # @return [Integer] attr_reader :perspective @@ -45,10 +45,10 @@ module Axlsx # @option options [Boolean] rAngAx # @option options [Integer] perspective def initialize(options={}) - @rotX, @hPercent, @rotY, @depthPercent, @rAngAx, @perspective = nil, nil, nil, nil, nil, nil + @rotX, @hPercent, @rotY, @depthPercent, @rAngAx, @perspective = nil, nil, nil, nil, nil, nil options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + end end # @see rotX @@ -69,6 +69,18 @@ module Axlsx # @see perspective def perspective=(v) DataTypeValidator.validate "#{self.class}.perspective", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 240 }; @perspective = v; end + + def to_xml_string(str = '') + str << '<c:view3D>' + str << '<c:rotX val="' << @rotX << '"/>' unless @rotX.nil? + str << '<c:hPercent val="' << @hPercent << '"/>' unless @hPercent.nil? + str << '<c:rotY val="' << @rotY << '"/>' unless @rotY.nil? + str << '<c:depthPercent val="' << @depthPercent << '"/>' unless @depthPercent.nil? + str << '<c:rAngAx val="' << @rAngAx << '"/>' unless @rAngAx.nil? + str << '<c:perspective val="' << @perspective << '"/>' unless @perspective.nil? + str << '</c:view3D>' + end + # Serializes the view3D properties # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 35ab181a..9bab4269 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -170,21 +170,21 @@ module Axlsx # @private def parts @parts = [ - {:entry => RELS_PN, :doc => relationships.to_xml, :schema => RELS_XSD}, + {:entry => RELS_PN, :doc => relationships.to_xml_string, :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 => 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.rels_pn}", :doc => drawing.relationships.to_xml_string, :schema => RELS_XSD} @parts << {:entry => "xl/#{drawing.pn}", :doc => drawing.to_xml, :schema => DRAWING_XSD} end workbook.tables.each do |table| - @parts << {:entry => "xl/#{table.pn}", :doc => table.to_xml, :schema => SML_XSD} + @parts << {:entry => "xl/#{table.pn}", :doc => table.to_xml_string, :schema => SML_XSD} end workbook.charts.each do |chart| @@ -200,7 +200,7 @@ module Axlsx end workbook.worksheets.each do |sheet| - @parts << {:entry => "xl/#{sheet.rels_pn}", :doc => sheet.relationships.to_xml, :schema => RELS_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 diff --git a/lib/axlsx/rels/relationship.rb b/lib/axlsx/rels/relationship.rb index 3596d808..4321c7e1 100644 --- a/lib/axlsx/rels/relationship.rb +++ b/lib/axlsx/rels/relationship.rb @@ -47,14 +47,17 @@ module Axlsx # @see TargetMode def TargetMode=(v) RestrictionValidator.validate 'Relationship.TargetMode', [:External, :Internal], v; @TargetMode = v; end - # Serializes the relationship - # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. - # @param [String] rId the reference id of the object. + # serialize relationship + # @param [String] str + # @param [Integer] rId the id for this relationship # @return [String] - def to_xml(xml, rId) + def to_xml_string(str = '', rId) h = self.instance_values - h[:Id] = rId - xml.Relationship(h) + h[:Id] = 'rId' << rId.to_s + str << '<Relationship ' + str << h.map { |key, value| '' << key.to_s << '="' << value.to_s << '"'}.join(' ') + str << '/>' end + end end diff --git a/lib/axlsx/rels/relationships.rb b/lib/axlsx/rels/relationships.rb index 8516bc69..06bd4934 100644 --- a/lib/axlsx/rels/relationships.rb +++ b/lib/axlsx/rels/relationships.rb @@ -11,6 +11,12 @@ require 'axlsx/rels/relationship.rb' super Relationship end + def to_xml_string(str = '') + str << '<?xml version="1.0" encoding="UTF-8"?>' + str << '<Relationships xmlns="' << RELS_R << '">' + each_with_index { |rel, index| rel.to_xml_string(str, index+1) } + str << '</Relationships>' + end # Serializes the relationships document. # @return [String] def to_xml() @@ -21,6 +27,6 @@ require 'axlsx/rels/relationship.rb' end builder.to_xml(:save_with => 0) end - + end end diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index 0bdd7936..dac8221f 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -37,7 +37,7 @@ module Axlsx end def to_xml_string - '<sst xmlns="' << XML_NS << '" count="' << @count.to_s << '" uniqueCount="' << unique_count.to_s << '">' << @shared_xml_string << '</sst>' + '<?xml version="1.0" encoding="UTF-8"?><sst xmlns="' << XML_NS << '" count="' << @count.to_s << '" uniqueCount="' << unique_count.to_s << '">' << @shared_xml_string << '</sst>' end # Generate the xml document for the Shared Strings Table diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 43296611..ce4e161b 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -189,34 +189,29 @@ require 'axlsx/workbook/worksheet/table.rb' 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 - } - xml.definedNames { - @worksheets.each_with_index do |sheet, index| - if sheet.auto_filter - xml.definedName(sheet.abs_auto_filter, :name => '_xlnm._FilterDatabase', :localSheetId => index, :hidden => 1) - end - 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 diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index bfb7f35f..015c4b91 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -1,4 +1,5 @@ # encoding: UTF-8 +require 'cgi' module Axlsx # A cell in a worksheet. # Cell stores inforamation requried to serialize a single worksheet cell to xml. You must provde the Row that the cell belongs to and the cells value. The data type will automatically be determed if you do not specify the :type option. The default style will be applied if you do not supply the :style option. Changing the cell's type will recast the value to the type specified. Altering the cell's value via the property accessor will also automatically cast the provided value to the cell's type. @@ -276,6 +277,9 @@ module Axlsx self.row.worksheet.merge_cells "#{self.r}:#{range_end}" unless range_end.nil? end + # builds an xml text run based on this cells attributes. + # @param [String] str The string instance this run will be concated to. + # @return [String] def run_xml_string(str = '') if is_text_run? data = self.instance_values.reject{|key, value| value == nil } @@ -298,40 +302,11 @@ module Axlsx end str end - # builds an xml text run based on this cells attributes. This is extracted from to_xml so that shared strings can use it. - # @param [Nokogiri::XML::Builder] xml The document builder instance this output will be added to. - # @return [String] the xml for this cell's text run - def run_xml(xml) - if (self.instance_values.keys & INLINE_STYLES).size > 0 - xml.r { - xml.rPr { - xml.rFont(:val=>@font_name) if @font_name - xml.charset(:val=>@charset) if @charset - xml.family(:val=>@family) if @family - xml.b(:val=>@b) if @b - xml.i(:val=>@i) if @i - xml.strike(:val=>@strike) if @strike - xml.outline(:val=>@outline) if @outline - xml.shadow(:val=>@shadow) if @shadow - xml.condense(:val=>@condense) if @condense - xml.extend(:val=>@extend) if @extend - @color.to_xml(xml) if @color - xml.sz(:val=>@sz) if @sz - xml.u(:val=>@u) if @u - # :baseline, :subscript, :superscript - xml.vertAlign(:val=>@vertAlign) if @vertAlign - # :none, major, :minor - xml.scheme(:val=>@scheme) if @scheme - } - xml.t @value.to_s - } - else - xml.t @value.to_s - end - end # Serializes the cell - # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. + # @param [Integer] r_index The row index for the cell + # @param [Integer] c_index The cell index in the row. + # @param [String] str The string index the cell content will be appended to. Defaults to empty string. # @return [String] xml text for the cell def to_xml_string(r_index, c_index, str = '') str << '<c r="' << Axlsx::cell_r(c_index, r_index) << '" s="' << @style.to_s << '" ' @@ -343,7 +318,7 @@ module Axlsx else #parse shared if @ssti - str << 't="s"><v>' << ssti << '</v>' + str << 't="s"><v>' << @ssti.to_s << '</v>' else str << 't="inlineStr"><is>' << run_xml_string << '</is>' end @@ -361,40 +336,6 @@ module Axlsx str << '</c>' end - def to_xml(xml) - if @type == :string - #parse formula - if @value.start_with?('=') - xml.c(:r => r, :s=>style, :t=>:str) { - xml.f @value.to_s.gsub('=', '') - } - else - #parse shared - if @ssti - xml.c(:r => r, :s=>style, :t => :s) { xml.v ssti } - else - #parse inline string - xml.c(:r => r, :s=>style, :t => :inlineStr) { - xml.is { - run_xml(xml) - } - } - end - end - elsif @type == :date - # TODO: See if this is subject to the same restriction as Time below - v = DateTimeConverter::date_to_serial @value - xml.c(:r => r, :s => style) { xml.v v } - elsif @type == :time - v = DateTimeConverter::time_to_serial @value - xml.c(:r => r, :s => style) { xml.v v } - elsif @type == :boolean - xml.c(:r => r, :s => style, :t => :b) { xml.v value } - else - xml.c(:r => r, :s => style) { xml.v value } - end - end - private # Utility method for setting inline style attributes @@ -457,7 +398,7 @@ module Axlsx v ? 1 : 0 else @type = :string - v.to_s + ::CGI.escapeHTML(v.to_s) end end end diff --git a/lib/axlsx/workbook/worksheet/col.rb b/lib/axlsx/workbook/worksheet/col.rb index ccf4dfaf..7a34ad40 100644 --- a/lib/axlsx/workbook/worksheet/col.rb +++ b/lib/axlsx/workbook/worksheet/col.rb @@ -64,13 +64,13 @@ module Axlsx @outlineLevel = v end - # @see Col#phonetic - def phonetic=(v) + # @see Col#phonetic + def phonetic=(v) Axlsx.validate_boolean(v) @phonetic = v end - # @see Col#style + # @see Col#style def style=(v) Axlsx.validate_unsigned_int(v) @style = v @@ -103,10 +103,11 @@ module Axlsx end # Serialize this columns data to an xml string + # @param [String] str # @return [String] def to_xml_string(str = '') attrs = self.instance_values.reject{ |key, value| value == nil } - str << '<col ' << attrs.map { |key, value| "#{key}='#{value}' " }.join << '/>' + str << '<col ' << attrs.map { |key, value| '' << key << '="' << value.to_s << '"' }.join(' ') << '/>' end end diff --git a/lib/axlsx/workbook/worksheet/page_margins.rb b/lib/axlsx/workbook/worksheet/page_margins.rb index 1c456f2a..19402a6d 100644 --- a/lib/axlsx/workbook/worksheet/page_margins.rb +++ b/lib/axlsx/workbook/worksheet/page_margins.rb @@ -83,15 +83,15 @@ module Axlsx # @see footer def footer=(v); Axlsx::validate_unsigned_numeric(v); @footer = v end - def to_xml_string - "<pageMargins left='%s' right='%s' top='%s' bottom='%s' header='%s' footer='%s'/>" % [left, right, top, bottom, header, footer] - end # Serializes the page margins element # @note For compatibility, this is a noop unless custom margins have been specified. - # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @see #custom_margins_specified? - def to_xml(xml) - xml.pageMargins :left => left, :right => right, :top => top, :bottom => bottom, :header => header, :footer => footer + # @param [String] str + # @retrun [String] + def to_xml_string(str = '') + str << '<pageMargins ' + str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' }.join(' ') + str << '/>' end end end diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb index 58171fea..4e5150da 100644 --- a/lib/axlsx/workbook/worksheet/row.rb +++ b/lib/axlsx/workbook/worksheet/row.rb @@ -59,6 +59,10 @@ module Axlsx worksheet.rows.index(self) end + # Serializes the row + # @param [Integer] r_index The row index, 0 based. + # @param [String] str The string this rows xml will be appended to. + # @return [String] def to_xml_string(r_index, str = '') str << '<row r="' << (r_index + 1 ).to_s << '" ' if custom_height? @@ -70,14 +74,6 @@ module Axlsx str << '</row>' str end - # Serializes the row - # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. - # @return [String] - def to_xml(xml) - attrs = {:r => index+1} - attrs.merge!(:customHeight => 1, :ht => height) if custom_height? - xml.row(attrs) { |ixml| @cells.each { |cell| cell.to_xml(ixml) } } - end # Adds a singel sell to the row based on the data provided and updates the worksheet's autofit data. # @return [Cell] diff --git a/lib/axlsx/workbook/worksheet/table.rb b/lib/axlsx/workbook/worksheet/table.rb index 831040f3..0b1dc385 100644 --- a/lib/axlsx/workbook/worksheet/table.rb +++ b/lib/axlsx/workbook/worksheet/table.rb @@ -63,29 +63,25 @@ module Axlsx end end + def to_xml_string(str = '') + str << '<?xml version="1.0" encoding="UTF-8"?>' + str << '<table xmlns="' << XML_NS << '" id="' << (index+1).to_s << '" name="' << @name << '" displayName="' << @name.gsub(/\s/,'_') << '" ' + str << 'ref="' << @ref << '" totalsRowShown="0">' + str << '<autoFilter ref="' << @ref << '"/>' + str << '<tableColumns count="' << header_cells.length.to_s << '">' + header_cells.each_with_index do |cell,index| + str << '<tableColumn id ="' << (index+1).to_s << '" name="' << cell.value << '"/>' + end + str << '</tableColumns>' + #TODO implement tableStyleInfo + str << '<tableStyleInfo showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0" name="TableStyleMedium9" />' + str << '</table>' + 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) diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 84f82791..200c8b80 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -385,7 +385,8 @@ module Axlsx end def to_xml_string - str = "<worksheet xmlns=\"%s\" xmlns:r=\"%s\">" % [XML_NS, XML_NS_R] + str = '<?xml version="1.0" encoding="UTF-8"?>' + str.concat "<worksheet xmlns=\"%s\" xmlns:r=\"%s\">" % [XML_NS, XML_NS_R] str.concat "<sheetPr><pageSetUpPr fitToPage=\"%s\"></pageSetUpPr></sheetPr>" % fit_to_page if fit_to_page str.concat "<dimension ref=\"%s\"></dimension>" % dimension unless rows.size == 0 str.concat "<sheetViews><sheetView tabSelected='%s' workbookViewId='0' showGridLines='%s'><selection activeCell=\"A1\" sqref=\"A1\"/></sheetView></sheetViews>" % [@selected, show_gridlines] @@ -393,17 +394,12 @@ module Axlsx if @column_info.size > 0 str << "<cols>" @column_info.each { |col| col.to_xml_string(str) } - - # @auto_fit_data.each_with_index do |col, index| - # min_max = index+1 - # str.concat "<col min='%s' max='%s' width='%s' customWidth='1'></col>" % [min_max, min_max, auto_width(col)] - # end str.concat '</cols>' end str.concat '<sheetData>' @rows.each_with_index { |row, index| row.to_xml_string(index, str) } str.concat '</sheetData>' - str.concat page_margins.to_xml_string if @page_margins + page_margins.to_xml_string(str) if @page_margins str.concat "<autoFilter ref='%s'></autoFilter>" % @auto_filter if @auto_filter str.concat "<mergeCells count='%s'>%s</mergeCells>" % [@merged_cells.size, @merged_cells.reduce('') { |memo, obj| "<mergeCell ref='%s'></mergeCell>" % obj } ] unless @merged_cells.empty? str.concat "<drawing r:id='rId1'></drawing>" if @drawing |
