diff options
| author | Randy Morgan <[email protected]> | 2011-11-26 13:20:57 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2011-11-26 13:20:57 +0900 |
| commit | 11303a4d6664d9a7ff7bc408035d2ab70378a375 (patch) | |
| tree | 51c675ae3ab8cd6c1afd7087145261b1eb2ec54c /lib/axlsx | |
| parent | 044afe5a35db42cefce4f18a0fecc3da90cab1b5 (diff) | |
| download | caxlsx-11303a4d6664d9a7ff7bc408035d2ab70378a375.tar.gz caxlsx-11303a4d6664d9a7ff7bc408035d2ab70378a375.zip | |
adding in support for ruby 1.9.3
release version 1.0.9
Diffstat (limited to 'lib/axlsx')
49 files changed, 566 insertions, 418 deletions
diff --git a/lib/axlsx/content_type/default.rb b/lib/axlsx/content_type/default.rb index c82e5f7d..5c688568 100644 --- a/lib/axlsx/content_type/default.rb +++ b/lib/axlsx/content_type/default.rb @@ -4,10 +4,11 @@ module Axlsx # The extension of the content type. # @return [String] - attr_accessor :Extension + attr_reader :Extension - # @return [String] ContentType The type of content. TABLE_CT, WORKBOOK_CT, APP_CT, RELS_CT, STYLES_CT, XML_CT, WORKSHEET_CT, SHARED_STRINGS_CT, CORE_CT, CHART_CT, DRAWING_CT are allowed - attr_accessor :ContentType + # The type of content. + # @return [String] + attr_reader :ContentType #Creates a new Default object # @option options [String] Extension @@ -19,7 +20,11 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end + # Sets the file extension for this content type. def Extension=(v) Axlsx::validate_string v; @Extension = v end + + # Sets the content type + # @see Axlsx#validate_content_type def ContentType=(v) Axlsx::validate_content_type v; @ContentType = v end # Serializes the object to xml diff --git a/lib/axlsx/content_type/override.rb b/lib/axlsx/content_type/override.rb index 4d1b008f..362e834b 100644 --- a/lib/axlsx/content_type/override.rb +++ b/lib/axlsx/content_type/override.rb @@ -2,11 +2,13 @@ module Axlsx # An override content part. These parts are automatically created by for you based on the content of your package. class Override - # @return [String] ContentType The type of content. TABLE_CT, WORKBOOK_CT, APP_CT, RELS_CT, STYLES_CT, XML_CT, WORKSHEET_CT, SHARED_STRINGS_CT, CORE_CT, CHART_CT, DRAWING_CT are allowed - attr_accessor :ContentType + # The type of content. + # @return [String] + attr_reader :ContentType - # @return [String] PartName The name and location of the part. - attr_accessor :PartName + # The name and location of the part. + # @return [String] + attr_reader :PartName #Creates a new Override object # @option options [String] PartName @@ -18,7 +20,12 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end + + # The name and location of the part. def PartName=(v) Axlsx::validate_string v; @PartName = v end + + # 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 diff --git a/lib/axlsx/doc_props/app.rb b/lib/axlsx/doc_props/app.rb index 77e7ea42..8e6bea27 100644 --- a/lib/axlsx/doc_props/app.rb +++ b/lib/axlsx/doc_props/app.rb @@ -11,73 +11,73 @@ module Axlsx class App # @return [String] The name of the document template. - attr_accessor :Template + attr_reader :Template # @return [String] The name of the manager for the document. - attr_accessor :Manager + attr_reader :Manager # @return [String] The name of the company generating the document. - attr_accessor :Company + attr_reader :Company # @return [Integer] The number of pages in the document. - attr_accessor :Pages + attr_reader :Pages # @return [Integer] The number of words in the document. - attr_accessor :Words + attr_reader :Words # @return [Integer] The number of characters in the document. - attr_accessor :Characters + attr_reader :Characters # @return [String] The intended format of the presentation. - attr_accessor :PresentationFormat + attr_reader :PresentationFormat # @return [Integer] The number of lines in the document. - attr_accessor :Lines + attr_reader :Lines # @return [Integer] The number of paragraphs in the document - attr_accessor :Paragraphs + attr_reader :Paragraphs # @return [Intger] The number of slides in the document. - attr_accessor :Slides + attr_reader :Slides # @return [Integer] The number of slides that have notes. - attr_accessor :Notes + attr_reader :Notes # @return [Integer] The total amount of time spent editing. - attr_accessor :TotalTime + attr_reader :TotalTime # @return [Integer] The number of hidden slides. - attr_accessor :HiddenSlides + attr_reader :HiddenSlides # @return [Integer] The total number multimedia clips - attr_accessor :MMClips + attr_reader :MMClips # @return [Boolean] The display mode for the document thumbnail. - attr_accessor :ScaleCrop + attr_reader :ScaleCrop # @return [Boolean] The links in the document are up to date. - attr_accessor :LinksUpToDate + attr_reader :LinksUpToDate # @return [Integer] The number of characters in the document including spaces. - attr_accessor :CharactersWithSpaces + attr_reader :CharactersWithSpaces # @return [Boolean] Indicates if the document is shared. - attr_accessor :ShareDoc + attr_reader :ShareDoc # @return [String] The base for hyper links in the document. - attr_accessor :HyperLinkBase + attr_reader :HyperLinkBase # @return [Boolean] Indicates that the hyper links in the document have been changed. - attr_accessor :HyperlinksChanged + attr_reader :HyperlinksChanged # @return [String] The name of the application - attr_accessor :Application + attr_reader :Application # @return [String] The version of the application. - attr_accessor :AppVersion + attr_reader :AppVersion # @return [Integer] Document security - attr_accessor :DocSecurity + attr_reader :DocSecurity # Creates an App object # @option options [String] Template @@ -108,28 +108,58 @@ module Axlsx end end + # Sets the Template property of your app.xml file 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 + + # Sets the Company property of your app.xml file 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 + + # Sets the Words property of your app.xml file 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 + + + # Sets the PresentationFormat property of your app.xml file 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 + # Sets the Paragraphs property of your app.xml file 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 + # Sets the Notes property of your app.xml file 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 + # Sets the HiddenSlides property of your app.xml file 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 + # Sets the ScaleCrop property of your app.xml file 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 + # Sets the CharactersWithSpaces property of your app.xml file 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 + # Sets the HyperLinkBase property of your app.xml file 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 + # Sets the Application property of your app.xml file 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 + # Sets the DocSecurity property of your app.xml file def DocSecurity=(v) Axlsx::validate_int v; @DocSecurity = v; end # Generate an app.xml document diff --git a/lib/axlsx/drawing/axis.rb b/lib/axlsx/drawing/axis.rb index 7e48801a..b89ef70c 100644 --- a/lib/axlsx/drawing/axis.rb +++ b/lib/axlsx/drawing/axis.rb @@ -18,22 +18,22 @@ module Axlsx # The position of the axis # must be one of [:l, :r, :t, :b] # @return [Symbol] - attr_accessor :axPos + attr_reader :axPos # the position of the tick labels # must be one of [:nextTo, :high, :low] # @return [Symbol] - attr_accessor :tickLblPos + attr_reader :tickLblPos # The number format format code for this axis # default :General # @return [String] - attr_accessor :format_code + attr_reader :format_code # specifies how the perpendicular axis is crossed # must be one of [:autoZero, :min, :max] # @return [Symbol] - attr_accessor :crosses + attr_reader :crosses # Creates an Axis object # @param [Integer] axId the id of this axis @@ -47,6 +47,7 @@ module Axlsx Axlsx::validate_unsigned_int(crossAx) @axId = axId @crossAx = crossAx + @format_code = "General" @scaling = Scaling.new(:orientation=>:minMax) self.axPos = :l self.tickLblPos = :nextTo @@ -56,10 +57,20 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end - + # The position of the axis + # must be one of [:l, :r, :t, :b] def axPos=(v) RestrictionValidator.validate "#{self.class}.axPos", [:l, :r, :b, :t], v; @axPos = v; end + + # the position of the tick labels + # must be one of [:nextTo, :high, :low1] def tickLblPos=(v) RestrictionValidator.validate "#{self.class}.tickLblPos", [:nextTo, :high, :low], v; @tickLblPos = v; end + + # The number format format code for this axis + # default :General def format_code=(v) Axlsx::validate_string(v); @formatCode = v; end + + # specifies how the perpendicular axis is crossed + # must be one of [:autoZero, :min, :max] def crosses=(v) RestrictionValidator.validate "#{self.class}.crosses", [:autoZero, :min, :max], v; @crosses = v; end # Serializes the common axis diff --git a/lib/axlsx/drawing/bar_3D_chart.rb b/lib/axlsx/drawing/bar_3D_chart.rb index 3d7f6296..6d74022f 100644 --- a/lib/axlsx/drawing/bar_3D_chart.rb +++ b/lib/axlsx/drawing/bar_3D_chart.rb @@ -18,25 +18,25 @@ module Axlsx # The direction of the bars in the chart # must be one of [:bar, :col] # @return [Symbol] - attr_accessor :barDir + attr_reader :barDir # space between bar or column clusters, as a percentage of the bar or column width. # @return [String] - attr_accessor :gapDepth + attr_reader :gapDepth # space between bar or column clusters, as a percentage of the bar or column width. # @return [String] - attr_accessor :gapWidth + attr_reader :gapWidth #grouping for a column, line, or area chart. # must be one of [:percentStacked, :clustered, :standard, :stacked] # @return [Symbol] - attr_accessor :grouping + attr_reader :grouping # The shabe of the bars or columns # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax] # @return [Symbol] - attr_accessor :shape + attr_reader :shape # validation regex for gap amount percent GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/ @@ -61,6 +61,7 @@ module Axlsx def initialize(frame, options={}) @barDir = :bar @grouping = :clustered + @gapWidth, @gapDepth, @shape = nil, nil, nil @catAxId = rand(8 ** 8) @valAxId = rand(8 ** 8) @catAxis = CatAxis.new(@catAxId, @valAxId) @@ -69,28 +70,35 @@ module Axlsx @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) RestrictionValidator.validate "Bar3DChart.barDir", [:bar, :col], v @barDir = v end - + #grouping for a column, line, or area chart. + # must be one of [:percentStacked, :clustered, :standard, :stacked] def grouping=(v) RestrictionValidator.validate "Bar3DChart.grouping", [:percentStacked, :clustered, :standard, :stacked], v @grouping = v end + # space between bar or column clusters, as a percentage of the bar or column width. def gapWidth=(v) RegexValidator.validate "Bar3DChart.gapWidth", GAP_AMOUNT_PERCENT, v @gapWidth=(v) end + # space between bar or column clusters, as a percentage of the bar or column width. def gapDepth=(v) RegexValidator.validate "Bar3DChart.gapWidth", GAP_AMOUNT_PERCENT, v @gapDepth=(v) end + # The shabe of the bars or columns + # must be one of [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax] def shape=(v) RestrictionValidator.validate "Bar3DChart.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v @shape = v diff --git a/lib/axlsx/drawing/bar_series.rb b/lib/axlsx/drawing/bar_series.rb index c4bffa8c..129a13e8 100644 --- a/lib/axlsx/drawing/bar_series.rb +++ b/lib/axlsx/drawing/bar_series.rb @@ -17,7 +17,7 @@ module Axlsx # The shabe of the bars or columns # must be one of [:percentStacked, :clustered, :standard, :stacked] # @return [Symbol] - attr_accessor :shape + attr_reader :shape # Creates a new series # @option options [Array, SimpleTypedList] data @@ -32,6 +32,8 @@ module Axlsx self.data = ValAxisData.new(options[:data]) unless options[:data].nil? end + # The shabe of the bars or columns + # must be one of [:percentStacked, :clustered, :standard, :stacked] def shape=(v) RestrictionValidator.validate "BarSeries.shape", [:cone, :coneToMax, :box, :cylinder, :pyramid, :pyramidToMax], v @shape = v @@ -41,10 +43,10 @@ module Axlsx # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] def to_xml(xml) - super(xml) do |xml| - @labels.to_xml(xml) unless @labels.nil? - @data.to_xml(xml) unless @data.nil? - xml.send('c:shape', :val=>@shape) + super(xml) do |xml_inner| + @labels.to_xml(xml_inner) unless @labels.nil? + @data.to_xml(xml_inner) unless @data.nil? + xml_inner.send('c:shape', :val=>@shape) end end diff --git a/lib/axlsx/drawing/cat_axis.rb b/lib/axlsx/drawing/cat_axis.rb index 083eac68..54066aa0 100644 --- a/lib/axlsx/drawing/cat_axis.rb +++ b/lib/axlsx/drawing/cat_axis.rb @@ -4,17 +4,17 @@ module Axlsx # 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. # @return [Boolean] - attr_accessor :auto + attr_reader :auto # specifies how the perpendicular axis is crossed # must be one of [:ctr, :l, :r] # @return [Symbol] - attr_accessor :lblAlgn + attr_reader :lblAlgn # The offset of the labels # must be between a string between 0 and 1000 # @return [Integer] - attr_accessor :lblOffset + attr_reader :lblOffset # regex for validating label offset LBL_OFFSET_REGEX = /0*(([0-9])|([1-9][0-9])|([1-9][0-9][0-9])|1000)%/ @@ -35,8 +35,15 @@ module Axlsx super(axId, crossAx, options) 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 + + # specifies how the perpendicular axis is crossed + # must be one of [:ctr, :l, :r] def lblAlgn=(v) RestrictionValidator.validate "#{self.class}.lblAlgn", [:ctr, :l, :r], v; @lblAlgn = v; end + + # The offset of the labels + # must be between a string between 0 and 1000 def lblOffset=(v) RegexValidator.validate "#{self.class}.lblOffset", LBL_OFFSET_REGEX, v; @lblOffset = v; end # Serializes the category axis diff --git a/lib/axlsx/drawing/chart.rb b/lib/axlsx/drawing/chart.rb index 6be9cbfe..45c75bda 100644 --- a/lib/axlsx/drawing/chart.rb +++ b/lib/axlsx/drawing/chart.rb @@ -21,29 +21,21 @@ module Axlsx # @return [Series] attr_reader :series_type - # The index of this chart in the workbooks charts collection - # @return [Integer] - attr_reader :index - - # The part name for this chart - # @return [String] - attr_reader :pn - #TODO data labels! - #attr_accessor :dLabls + #attr_reader :dLabls # The title object for the chart. # @return [Title] - attr_accessor :title + attr_reader :title # The style for the chart. # see ECMA Part 1 §21.2.2.196 # @return [Integer] - attr_accessor :style + attr_reader :style # Show the legend in the chart # @return [Boolean] - attr_accessor :show_legend + attr_reader :show_legend # Creates a new chart object # @param [GraphicalFrame] frame The frame that holds this chart. @@ -59,28 +51,42 @@ module Axlsx options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end - start_at *options[:start_at] if options[:start_at] - end_at *options[:end_at] if options[:start_at] + start_at(*options[:start_at]) if options[:start_at] + end_at(*options[:end_at]) if options[:start_at] yield self if block_given? end + # The index of this chart in the workbooks charts collection + # @return [Integer] def index @graphic_frame.anchor.drawing.worksheet.workbook.charts.index(self) end + # The part name for this chart + # @return [String] def pn "#{CHART_PN % (index+1)}" end + # The title object for the chart. + # @param [String, Cell] v + # @return [Title] def title=(v) v = Title.new(v) if v.is_a?(String) || v.is_a?(Cell) DataTypeValidator.validate "#{self.class}.title", Title, v @title = v end + # Show the legend in the chart + # @param [Boolean] v + # @return [Boolean] def show_legend=(v) Axlsx::validate_boolean(v); @show_legend = v; end - def style=(v) DataTypeValidator.validate "Chart.style", Integer, v, lambda { |v| v >= 1 && v <= 48 }; @style = v; end + + # The style for the chart. + # see ECMA Part 1 §21.2.2.196 + # @param [Integer] v must be between 1 and 48 + def style=(v) DataTypeValidator.validate "Chart.style", Integer, v, lambda { |arg| arg >= 1 && arg <= 48 }; @style = v; end # backwards compatibility to allow chart.to and chart.from access to anchor markers # @note This will be disconinued in version 2.0.0. Please use the end_at method diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index 17347cd1..4fa7dc11 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -47,34 +47,6 @@ module Axlsx # @return [SimpleTypedList] attr_reader :anchors - # An array of charts that are associated with this drawing's anchors - # @return [Array] - attr_reader :charts - - # An array of image objects that are associated with this drawing's anchors - # @return [Array] - attr_reader :images - - # The index of this drawing in the owning workbooks's drawings collection. - # @return [Integer] - attr_reader :index - - # The relation reference id for this drawing - # @return [String] - attr_reader :rId - - # The part name for this drawing - # @return [String] - attr_reader :pn - - # The relational part name for this drawing - # @return [String] - attr_reader :rels_pn - - # The drawing's relationships. - # @return [Relationships] - attr_reader :relationships - # Creates a new Drawing object # @param [Worksheet] worksheet The worksheet that owns this drawing def initialize(worksheet) @@ -99,33 +71,47 @@ module Axlsx TwoCellAnchor.new(self, options) @anchors.last.add_chart(chart_type, options) end - + + # An array of charts that are associated with this drawing's anchors + # @return [Array] def charts charts = @anchors.select { |a| a.object.is_a?(GraphicFrame) } charts.map { |a| a.object.chart } end + # An array of image objects that are associated with this drawing's anchors + # @return [Array] def images images = @anchors.select { |a| a.object.is_a?(Pic) } images.map { |a| a.object } end + # The index of this drawing in the owning workbooks's drawings collection. + # @return [Integer] def index @worksheet.workbook.drawings.index(self) end + # The relation reference id for this drawing + # @return [String] def rId "rId#{index+1}" end + # The part name for this drawing + # @return [String] def pn "#{DRAWING_PN % (index+1)}" end - + + # The relational part name for this drawing + # @return [String] def rels_pn "#{DRAWING_RELS_PN % (index+1)}" end + # The drawing's relationships. + # @return [Relationships] def relationships r = Relationships.new charts.each do |chart| diff --git a/lib/axlsx/drawing/graphic_frame.rb b/lib/axlsx/drawing/graphic_frame.rb index e8e5ba21..0b1f6d14 100644 --- a/lib/axlsx/drawing/graphic_frame.rb +++ b/lib/axlsx/drawing/graphic_frame.rb @@ -12,10 +12,6 @@ module Axlsx # @return [TwoCellAnchor] attr_reader :anchor - # The relationship id for this graphic - # @return [String] - attr_reader :rId - # Creates a new GraphicFrame object # @param [TwoCellAnchor] anchor # @param [Class] chart_type @@ -25,6 +21,8 @@ module Axlsx @chart = chart_type.new(self, options) end + # The relationship id for this graphic + # @return [String] def rId "rId#{@anchor.index+1}" end diff --git a/lib/axlsx/drawing/line_3D_chart.rb b/lib/axlsx/drawing/line_3D_chart.rb index 6fb34afb..af0cac86 100644 --- a/lib/axlsx/drawing/line_3D_chart.rb +++ b/lib/axlsx/drawing/line_3D_chart.rb @@ -34,12 +34,12 @@ module Axlsx # space between bar or column clusters, as a percentage of the bar or column width. # @return [String] - attr_accessor :gapDepth + attr_reader :gapDepth #grouping for a column, line, or area chart. # must be one of [:percentStacked, :clustered, :standard, :stacked] # @return [Symbol] - attr_accessor :grouping + attr_reader :grouping # validation regex for gap amount percent GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/ @@ -59,6 +59,7 @@ module Axlsx # @see Chart # @see View3D def initialize(frame, options={}) + @gapDepth = nil @grouping = :standard @catAxId = rand(8 ** 8) @valAxId = rand(8 ** 8) @@ -71,11 +72,13 @@ module Axlsx @view3D = View3D.new({:perspective=>30}.merge(options)) end + # @see grouping def grouping=(v) RestrictionValidator.validate "Bar3DChart.grouping", [:percentStacked, :standard, :stacked], v @grouping = v end + # @see gapDepth def gapDepth=(v) RegexValidator.validate "Bar3DChart.gapWidth", GAP_AMOUNT_PERCENT, v @gapDepth=(v) diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb index 40beeb84..c56980fc 100644 --- a/lib/axlsx/drawing/line_series.rb +++ b/lib/axlsx/drawing/line_series.rb @@ -18,18 +18,19 @@ module Axlsx # @option options [Array, SimpleTypedList] labels # @param [Chart] chart def initialize(chart, options={}) + @labels, @data = nil, nil super(chart, options) - self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? - self.data = ValAxisData.new(options[:data]) unless options[:data].nil? + @labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? + @data = ValAxisData.new(options[:data]) unless options[:data].nil? end # Serializes the series # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] def to_xml(xml) - super(xml) do |xml| - @labels.to_xml(xml) unless @labels.nil? - @data.to_xml(xml) unless @data.nil? + super(xml) do |xml_inner| + @labels.to_xml(xml_inner) unless @labels.nil? + @data.to_xml(xml_inner) unless @data.nil? end end diff --git a/lib/axlsx/drawing/marker.rb b/lib/axlsx/drawing/marker.rb index 019c14d9..6cd7acd6 100644 --- a/lib/axlsx/drawing/marker.rb +++ b/lib/axlsx/drawing/marker.rb @@ -6,19 +6,19 @@ module Axlsx # The column this marker anchors to # @return [Integer] - attr_accessor :col + attr_reader :col # The offset distance from this marker's column # @return [Integer] - attr_accessor :colOff + attr_reader :colOff # The row this marker anchors to # @return [Integer] - attr_accessor :row + attr_reader :row # The offset distance from this marker's row # @return [Integer] - attr_accessor :rowOff + attr_reader :rowOff # Creates a new Marker object # @option options [Integer] col @@ -32,11 +32,15 @@ module Axlsx end end + # @see col def col=(v) Axlsx::validate_unsigned_int v; @col = v end + # @see colOff def colOff=(v) Axlsx::validate_int v; @colOff = v end + # @see row 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 diff --git a/lib/axlsx/drawing/one_cell_anchor.rb b/lib/axlsx/drawing/one_cell_anchor.rb index caa0edae..0626502e 100644 --- a/lib/axlsx/drawing/one_cell_anchor.rb +++ b/lib/axlsx/drawing/one_cell_anchor.rb @@ -17,20 +17,15 @@ module Axlsx # @return [Drawing] attr_reader :drawing - # The index of this anchor in the drawing - # @return [Integer] - attr_reader :index - - # the width of the graphic object in pixels. # this is converted to EMU at a 92 ppi resolution # @return [Integer] - attr_accessor :width + attr_reader :width # the height of the graphic object in pixels # this is converted to EMU at a 92 ppi resolution # @return [Integer] - attr_accessor :height + attr_reader :height # Creates a new OneCellAnchor object and an Pic associated with it. @@ -53,9 +48,14 @@ module Axlsx @object = Pic.new(self, options) end + # @see height def height=(v) Axlsx::validate_unsigned_int(v); @height = v; end + + # @see width def width=(v) Axlsx::validate_unsigned_int(v); @width = v; end + # The index of this anchor in the drawing + # @return [Integer] def index @drawing.anchors.index(self) end diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index 7043df35..84e750ce 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -10,50 +10,22 @@ module Axlsx # The name to use for this picture # @return [String] - attr_accessor :name + attr_reader :name - # providing access to the anchor's width attribute - # @param [Integer] v - # @see OneCellAnchor.width - attr_accessor :width - - # providing access to update the anchor's height attribute - # @param [Integer] v - # @see OneCellAnchor.width - attr_accessor :height # A description of the picture # @return [String] - attr_accessor :descr + attr_reader :descr # The path to the image you want to include # Only local images are supported at this time and only jpg support # @return [String] - attr_accessor :image_src + attr_reader :image_src # The anchor for this image # @return [OneCellAnchor] attr_reader :anchor - # returns the extension of image_src without the preceeding '.' - # @return [String] - attr_reader :extname - - # The file name of image_src without any path information - # @return [String] - attr_reader :file_name - - # The part name for this image used in serialization and relationship building - # @return [String] - attr_reader :pn - - # The index of this image in the workbooks images collections - # @return [Index] - attr_reader :index - - # The name of the image file, sans directory info - # @return [String] - attr_reader :file_name # Creates a new Pic(ture) object # @param [Anchor] anchor the anchor that holds this image @@ -69,7 +41,7 @@ module Axlsx options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end - start_at *options[:start_at] if options[:start_at] + start_at(*options[:start_at]) if options[:start_at] yield self if block_given? end @@ -80,41 +52,61 @@ module Axlsx @image_src = v end + # @see name def name=(v) Axlsx::validate_string(v); @name = v; end + + # @see descr def descr=(v) Axlsx::validate_string(v); @descr = v; end + + # The file name of image_src without any path information + # @return [String] def file_name File.basename(image_src) unless image_src.nil? end + # returns the extension of image_src without the preceeding '.' + # @return [String] def extname File.extname(image_src).delete('.') unless image_src.nil? end + # The index of this image in the workbooks images collections + # @return [Index] def index @anchor.drawing.worksheet.workbook.images.index(self) end - + + # The part name for this image used in serialization and relationship building + # @return [String] def pn "#{IMAGE_PN % [(index+1), extname]}" end + # providing access to the anchor's width attribute + # @param [Integer] v + # @see OneCellAnchor.width def width @anchor.width end + # @see width def width=(v) @anchor.width = v end - def height=(v) - @anchor.height = v - end - + # providing access to update the anchor's height attribute + # @param [Integer] v + # @see OneCellAnchor.width def height @anchor.height end + # @see height + def height=(v) + @anchor.height = v + end + # This is a short cut method to set the start anchor position # If you need finer granularity in positioning use # graphic_frame.anchor.from.colOff / rowOff diff --git a/lib/axlsx/drawing/pie_series.rb b/lib/axlsx/drawing/pie_series.rb index 1f20058d..f72802f6 100644 --- a/lib/axlsx/drawing/pie_series.rb +++ b/lib/axlsx/drawing/pie_series.rb @@ -15,7 +15,7 @@ module Axlsx # The explosion for this series # @return [Integert] - attr_accessor :explosion + attr_reader :explosion # Creates a new series # @option options [Array, SimpleTypedList] data @@ -24,21 +24,23 @@ module Axlsx # @option options [Integer] explosion # @param [Chart] chart def initialize(chart, options={}) + @explosion = nil super(chart, options) self.labels = CatAxisData.new(options[:labels]) unless options[:labels].nil? self.data = ValAxisData.new(options[:data]) unless options[:data].nil? end + # @see explosion def explosion=(v) Axlsx::validate_unsigned_int(v); @explosion = v; end # Serializes the series # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] def to_xml(xml) - super(xml) do |xml| - xml.send('c:explosion', :val=>@explosion) unless @explosion.nil? - @labels.to_xml(xml) unless @labels.nil? - @data.to_xml(xml) unless @data.nil? + super(xml) do |xml_inner| + xml_inner.send('c:explosion', :val=>@explosion) unless @explosion.nil? + @labels.to_xml(xml_inner) unless @labels.nil? + @data.to_xml(xml_inner) unless @data.nil? end end private diff --git a/lib/axlsx/drawing/scaling.rb b/lib/axlsx/drawing/scaling.rb index c0665463..e89d00f1 100644 --- a/lib/axlsx/drawing/scaling.rb +++ b/lib/axlsx/drawing/scaling.rb @@ -5,20 +5,20 @@ module Axlsx # logarithmic base for a logarithmic axis. # must be between 2 and 1000 # @return [Integer] - attr_accessor :logBase + attr_reader :logBase # the orientation of the axis # must be one of [:minMax, :maxMin] # @return [Symbol] - attr_accessor :orientation + attr_reader :orientation # the maximum scaling # @return [Float] - attr_accessor :max + attr_reader :max # the minimu scaling # @return [Float] - attr_accessor :min + attr_reader :min # creates a new Scaling object # @option options [Integer, Fixnum] logBase @@ -27,14 +27,20 @@ module Axlsx # @option options [Float] min def initialize(options={}) @orientation = :minMax + @logBase, @min, @max = nil, nil, nil options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end - def logBase=(v) DataTypeValidator.validate "Scaling.logBase", [Integer, Fixnum], v, lambda { |v| v >= 2 && v <= 1000}; @logBase = v; end + # @see logBase + def logBase=(v) DataTypeValidator.validate "Scaling.logBase", [Integer, Fixnum], v, lambda { |arg| arg >= 2 && arg <= 1000}; @logBase = v; end + # @see orientation def orientation=(v) RestrictionValidator.validate "Scaling.orientation", [:minMax, :maxMin], v; @orientation = v; end + # @see max def max=(v) DataTypeValidator.validate "Scaling.max", Float, v; @max = v; end + + # @see min def min=(v) DataTypeValidator.validate "Scaling.min", Float, v; @min = v; end # Serializes the axId diff --git a/lib/axlsx/drawing/ser_axis.rb b/lib/axlsx/drawing/ser_axis.rb index a086b23c..077b92d7 100644 --- a/lib/axlsx/drawing/ser_axis.rb +++ b/lib/axlsx/drawing/ser_axis.rb @@ -4,11 +4,11 @@ module Axlsx # The number of tick lables to skip between labels # @return [Integer] - attr_accessor :tickLblSkip + attr_reader :tickLblSkip # The number of tickmarks to be skipped before the next one is rendered. # @return [Boolean] - attr_accessor :tickMarkSkip + attr_reader :tickMarkSkip # Creates a new SerAxis object # @param [Integer] axId the id of this axis. Inherited @@ -19,10 +19,14 @@ module Axlsx # @option options [Integer] tickLblSkip # @option options [Integer] tickMarkSkip def initialize(axId, crossAx, options={}) + @tickLblSkip, @tickMarkSkip = nil, nil super(axId, crossAx, options) end + # @see tickLblSkip def tickLblSkip=(v) Axlsx::validate_unsigned_int(v); @tickLblSkip = v; end + + # @see tickMarkSkip def tickMarkSkip=(v) Axlsx::validate_unsigned_int(v); @tickMarkSkip = v; end # Serializes the series axis diff --git a/lib/axlsx/drawing/series.rb b/lib/axlsx/drawing/series.rb index cb5d6a8d..3647f22e 100644 --- a/lib/axlsx/drawing/series.rb +++ b/lib/axlsx/drawing/series.rb @@ -9,40 +9,40 @@ module Axlsx # @return [Chart] attr_reader :chart - # The index of this series in the chart's series. - # @return [Integer] - attr_reader :index - - # The order of this series in the chart's series. By default the order is the index of the series. - # @return [Integer] - attr_accessor :order - # The title of the series # @return [SeriesTitle] - attr_accessor :title + attr_reader :title # Creates a new series # @param [Chart] chart # @option options [Integer] order # @option options [String] title def initialize(chart, options={}) + @order = nil self.chart = chart @chart.series << self options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end - + + # The index of this series in the chart's series. + # @return [Integer] def index @chart.series.index(self) end - def order=(v) Axlsx::validate_unsigned_int(v); @order = v; end + # The order of this series in the chart's series. By default the order is the index of the series. + # @return [Integer] def order @order || index end + # @see order + def order=(v) Axlsx::validate_unsigned_int(v); @order = v; end + + # @see title def title=(v) v = SeriesTitle.new(v) if v.is_a?(String) || v.is_a?(Cell) DataTypeValidator.validate "#{self.class}.title", SeriesTitle, v diff --git a/lib/axlsx/drawing/series_title.rb b/lib/axlsx/drawing/series_title.rb index bfb1b56f..de977633 100644 --- a/lib/axlsx/drawing/series_title.rb +++ b/lib/axlsx/drawing/series_title.rb @@ -8,7 +8,7 @@ module Axlsx def to_xml(xml) xml.send('c:tx') { xml.send('c:strRef') { - xml.send('c:f', range) + xml.send('c:f', Axlsx::cell_range([@cell])) xml.send('c:strCache') { xml.send('c:ptCount', :val=>1) xml.send('c:pt', :idx=>0) { diff --git a/lib/axlsx/drawing/title.rb b/lib/axlsx/drawing/title.rb index bdbf0e5c..f8c87921 100644 --- a/lib/axlsx/drawing/title.rb +++ b/lib/axlsx/drawing/title.rb @@ -4,11 +4,11 @@ module Axlsx # The text to be shown. Setting this property directly with a string will remove the cell reference. # @return [String] - attr_accessor :text + attr_reader :text # The cell that holds the text for the title. Setting this property will automatically update the text attribute. # @return [Cell] - attr_accessor :cell + attr_reader :cell # Creates a new Title object # @param [String, Cell] title The cell or string to be used for the chart's title @@ -17,6 +17,7 @@ module Axlsx self.text = title.to_s unless title.is_a?(Cell) end + # @see text def text=(v) DataTypeValidator.validate 'Title.text', String, v @text = v @@ -24,6 +25,7 @@ module Axlsx v end + # @see cell def cell=(v) DataTypeValidator.validate 'Title.text', Cell, v @cell = v @@ -43,7 +45,7 @@ module Axlsx xml.send('c:title') { xml.send('c:tx') { xml.send('c:strRef') { - xml.send('c:f', range) + xml.send('c:f', Axlsx::cell_range([@cell])) xml.send('c:strCache') { xml.send('c:ptCount', :val=>1) xml.send('c:pt', :idx=>0) { @@ -55,14 +57,5 @@ module Axlsx } end - private - - # returns the excel style abslute reference for the title when title is a Cell object - # @return [String] - def range - return "" unless @data.is_a?(Cell) - "#{@data.row.worksheet.name}!#{data.row.r_abs}" - end - end end diff --git a/lib/axlsx/drawing/two_cell_anchor.rb b/lib/axlsx/drawing/two_cell_anchor.rb index 73e2fe20..06c0554a 100644 --- a/lib/axlsx/drawing/two_cell_anchor.rb +++ b/lib/axlsx/drawing/two_cell_anchor.rb @@ -24,9 +24,6 @@ module Axlsx # @return [Drawing] attr_reader :drawing - # The index of this anchor in the drawing - # @return [Integer] - attr_reader :index # Creates a new TwoCellAnchor object and sets up a reference to the from and to markers in the # graphic_frame's chart. That means that you can do stuff like @@ -51,6 +48,8 @@ module Axlsx @object.chart end + # The index of this anchor in the drawing + # @return [Integer] def index @drawing.anchors.index(self) end diff --git a/lib/axlsx/drawing/val_axis.rb b/lib/axlsx/drawing/val_axis.rb index 10a070f6..3233bcfd 100644 --- a/lib/axlsx/drawing/val_axis.rb +++ b/lib/axlsx/drawing/val_axis.rb @@ -5,7 +5,7 @@ module Axlsx # This element specifies how the value axis crosses the category axis. # must be one of [:between, :midCat] # @return [Symbol] - attr_accessor :crossBetween + attr_reader :crossBetween # Creates a new ValAxis object # @param [Integer] axId the id of this axis @@ -18,7 +18,7 @@ module Axlsx self.crossBetween = :between super(axId, crossAx, options) end - + # @see crossBetween def crossBetween=(v) RestrictionValidator.validate "ValAxis.crossBetween", [:between, :midCat], v; @crossBetween = v; end # Serializes the value axis diff --git a/lib/axlsx/drawing/view_3D.rb b/lib/axlsx/drawing/view_3D.rb index 1065d778..7a8c115f 100644 --- a/lib/axlsx/drawing/view_3D.rb +++ b/lib/axlsx/drawing/view_3D.rb @@ -11,30 +11,30 @@ module Axlsx # x rotation for the chart # must be between -90 and 90 # @return [Integer] - attr_accessor :rotX + attr_reader :rotX # height of chart as % of chart # must be between 5% and 500% # @return [String] - attr_accessor :hPercent + attr_reader :hPercent # y rotation for the chart # must be between 0 and 360 # @return [Integer] - attr_accessor :rotY + attr_reader :rotY # depth or chart as % of chart width # must be between 20% and 2000% # @return [String] - attr_accessor :depthPercent + attr_reader :depthPercent # Chart axis are at right angles # @return [Boolean] - attr_accessor :rAngAx + attr_reader :rAngAx # field of view angle # @return [Integer] - attr_accessor :perspective + attr_reader :perspective # Creates a new View3D for charts # @option options [Integer] rotX @@ -44,22 +44,29 @@ 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 options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end - def rotX=(v) DataTypeValidator.validate "#{self.class}.rotX", [Integer, Fixnum], v, lambda {|v| v >= -90 && v <= 90 }; @rotX = v; end + # @see rotX + def rotX=(v) DataTypeValidator.validate "#{self.class}.rotX", [Integer, Fixnum], v, lambda {|arg| arg >= -90 && arg <= 90 }; @rotX = v; end + # @see hPercent def hPercent=(v) RegexValidator.validate "#{self.class}.rotX", H_PERCENT_REGEX, v; @hPercent = v; end - def rotY=(v) DataTypeValidator.validate "#{self.class}.rotY", [Integer, Fixnum], v, lambda {|v| v >= 0 && v <= 360 }; @rotY = v; end + # @see rotY + def rotY=(v) DataTypeValidator.validate "#{self.class}.rotY", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 360 }; @rotY = v; end + # @see depthPercent def depthPercent=(v) RegexValidator.validate "#{self.class}.depthPercent", DEPTH_PERCENT_REGEX, v; @depthPercent = v; end + # @see rAngAx def rAngAx=(v) Axlsx::validate_boolean(v); @rAngAx = v; end - def perspective=(v) DataTypeValidator.validate "#{self.class}.perspective", [Integer, Fixnum], v, lambda {|v| v >= 0 && v <= 240 }; @perspective = v; end + # @see perspective + def perspective=(v) DataTypeValidator.validate "#{self.class}.perspective", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 240 }; @perspective = v; end # Serializes the view3D properties # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 91df81df..d9455f14 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -1,12 +1,23 @@ # -*- coding: utf-8 -*- -# Create Office Open XML Spreadsheets (xlsx) with safe and full control over cell styles, automatically resized column widths and 3D pie charts. module Axlsx # Package is responsible for managing all the bits and peices that Open Office XML requires to make a valid # xlsx document including valdation and serialization. class Package + # Initializes your package + # + # @param [Hash] options A hash that you can use to specify the author and workbook for this package. + # @option options [String] :author The author of the document + # @example Package.new :author => 'you!', :workbook => Workbook.new + def initialize(options={}) + @workbook = nil + @core, @app = Core.new, App.new + @core.creator = options[:author] || @core.creator + yield self if block_given? + end + + # The workbook this package will serialize or validate. - # @attribute # @return [Workbook] If no workbook instance has been assigned with this package a new Workbook instance is returned. # @raise ArgumentError if workbook parameter is not a Workbook instance. # @note As there are multiple ways to instantiate a workbook for the package, @@ -18,25 +29,13 @@ module Axlsx # wb = Pacakge.new().workbook # # # set the workbook after creating the package # wb = Package.new().workbook = Workbook.new - attr_accessor :workbook - - # Initializes your package - # - # @param [Hash] options A hash that you can use to specify the author and workbook for this package. - # @option options [String] :author The author of the document - # @example Package.new :author => 'you!', :workbook => Workbook.new - def initialize(options={}) - @core, @app = Core.new, App.new - @core.creator = options[:author] || @core.creator - yield self if block_given? - end - - def workbook=(workbook) DataTypeValidator.validate "Package.workbook", Workbook, workbook; @workbook = workbook; end - def workbook @workbook || @workbook = Workbook.new end + # @see workbook + def workbook=(workbook) DataTypeValidator.validate "Package.workbook", Workbook, workbook; @workbook = workbook; end + # Serialize your workbook to disk as an xlsx document. # # @param [File] output The file you want to serialize your package to @@ -57,18 +56,17 @@ module Axlsx p = parts Zip::ZipOutputStream.open(output) do |zip| p.each do |part| - zip.put_next_entry(part[:entry]); zip.puts(part[:doc]) unless part[:doc].nil? - end - end - Zip::ZipFile.open(output) do |zip| - p.each do |part| - if part[:path] - zip.add(part[:entry], part[:path], &proc{ true }) + unless part[:doc].nil? + zip.put_next_entry(part[:entry]); + zip.puts(part[:doc]) end - end + unless part[:path].nil? + zip.put_next_entry(part[:entry]); + # binread for 1.9.3 + zip.write IO.respond_to?(:binread) ? IO.binread(part[:path]) : IO.read(part[:path]) + end + end end - - true end diff --git a/lib/axlsx/rels/relationship.rb b/lib/axlsx/rels/relationship.rb index a76c0c25..829a724a 100644 --- a/lib/axlsx/rels/relationship.rb +++ b/lib/axlsx/rels/relationship.rb @@ -5,7 +5,7 @@ module Axlsx # The location of the relationship target # @return [String] - attr_accessor :Target + attr_reader :Target # The type of relationship # @note Supported types are defined as constants in Axlsx: @@ -20,13 +20,15 @@ module Axlsx # @see CHART_R # @see DRAWING_R # @return [String] - attr_accessor :Type + attr_reader :Type def initialize(type, target) self.Target=target self.Type=type end + # @see Target def Target=(v) Axlsx::validate_string v; @Target = v end + # @see Type def Type=(v) Axlsx::validate_relationship_type v; @Type = v end # Serializes the relationship diff --git a/lib/axlsx/stylesheet/border.rb b/lib/axlsx/stylesheet/border.rb index 167b42c9..73a873e5 100644 --- a/lib/axlsx/stylesheet/border.rb +++ b/lib/axlsx/stylesheet/border.rb @@ -3,13 +3,13 @@ module Axlsx class Border # @return [Boolean] The diagonal up property for the border that indicates if the border should include a diagonal line from the bottom left to the top right of the cell. - attr_accessor :diagonalUp + attr_reader :diagonalUp # @return [Boolean] The diagonal down property for the border that indicates if the border should include a diagonal line from the top left to the top right of the cell. - attr_accessor :diagonalDown + attr_reader :diagonalDown # @return [Boolean] The outline property for the border indicating that top, left, right and bottom borders should only be applied to the outside border of a range of cells. - attr_accessor :outline + attr_reader :outline # @return [SimpleTypedList] A list of BorderPr objects for this border. attr_reader :prs @@ -35,8 +35,11 @@ module Axlsx end end + # @see diagonalUp def diagonalUp=(v) Axlsx::validate_boolean v; @diagonalUp = v end + # @see diagonalDown def diagonalDown=(v) Axlsx::validate_boolean v; @diagonalDown = v end + # @see outline def outline=(v) Axlsx::validate_boolean v; @outline = v end # Serializes the border element @@ -44,7 +47,9 @@ module Axlsx def to_xml(xml) xml.border(self.instance_values.select{ |k,v| [:diagonalUp, :diagonalDown, :outline].include? k }) { [:start, :end, :left, :right, :top, :bottom, :diagonal, :vertical, :horizontal].each do |k| - @prs.select { |pr| pr.name == k }.each { |pr| pr.to_xml(xml) } + @prs.select { |pr| pr.name == k }.each do |part| + part.to_xml(xml) + end end } end diff --git a/lib/axlsx/stylesheet/border_pr.rb b/lib/axlsx/stylesheet/border_pr.rb index 63a1f9de..cdab5b13 100644 --- a/lib/axlsx/stylesheet/border_pr.rb +++ b/lib/axlsx/stylesheet/border_pr.rb @@ -3,7 +3,7 @@ module Axlsx class BorderPr # @return [Color] The color of this border part. - attr_accessor :color + attr_reader :color # @return [Symbol] The syle of this border part. # @note @@ -22,7 +22,7 @@ module Axlsx # :dashDotDot # :mediumDashDotDot # :slantDashDot - attr_accessor :style + attr_reader :style # @return [Symbol] The name of this border part # @note @@ -36,7 +36,7 @@ module Axlsx # :diagonal # :vertical # :horizontal - attr_accessor :name + attr_reader :name # Creates a new Border Part Object # @option options [Color] color @@ -49,15 +49,18 @@ module Axlsx end end + # @see name def name=(v) RestrictionValidator.validate "BorderPr.name", [:start, :end, :left, :right, :top, :bottom, :diagonal, :vertical, :horizontal], v; @name = v end + # @see color def color=(v) DataTypeValidator.validate(:color, Color, v); @color = v end + # @see style def style=(v) RestrictionValidator.validate "BorderPr.style", [:none, :thin, :medium, :dashed, :dotted, :thick, :double, :hair, :mediumDashed, :dashDot, :mediumDashDot, :dashDotDot, :mediumDashDotDot, :slantDashDot], v; @style = v end # Serializes the border part # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] def to_xml(xml) - xml.send(@name, :style=>@style) { + xml.send(@name, :style => @style) { @color.to_xml(xml) if @color.is_a? Color } end diff --git a/lib/axlsx/stylesheet/cell_alignment.rb b/lib/axlsx/stylesheet/cell_alignment.rb index 071ce68f..75d86e2d 100644 --- a/lib/axlsx/stylesheet/cell_alignment.rb +++ b/lib/axlsx/stylesheet/cell_alignment.rb @@ -15,7 +15,7 @@ module Axlsx # :centerContinuous # :distributed # @return [Symbol] - attr_accessor :horizontal + attr_reader :horizontal # The vertical alignment of the cell. # @note @@ -26,38 +26,38 @@ module Axlsx # :justify # :distributed # @return [Symbol] - attr_accessor :vertical + attr_reader :vertical # The textRotation of the cell. # @return [Integer] - attr_accessor :textRotation + attr_reader :textRotation # Indicate if the text of the cell should wrap # @return [Boolean] - attr_accessor :wrapText + attr_reader :wrapText # The amount of indent # @return [Integer] - attr_accessor :indent + attr_reader :indent # The amount of relativeIndent # @return [Integer] - attr_accessor :relativeIndent + attr_reader :relativeIndent # Indicate if the last line should be justified. # @return [Boolean] - attr_accessor :justifyLastLine + attr_reader :justifyLastLine # Indicate if the text should be shrunk to the fit in the cell. # @return [Boolean] - attr_accessor :shrinkToFit + attr_reader :shrinkToFit # The reading order of the text # 0 Context Dependent # 1 Left-to-Right # 2 Right-to-Left # @return [Integer] - attr_accessor :readingOrder + attr_reader :readingOrder # Create a new cell_alignment object # @option options [Symbol] horizontal @@ -75,14 +75,23 @@ module Axlsx end end + # @see horizontal def horizontal=(v) Axlsx::validate_horizontal_alignment v; @horizontal = v end + # @see vertical def vertical=(v) Axlsx::validate_vertical_alignment v; @vertical = v end + # @see textRotation def textRotation=(v) Axlsx::validate_unsigned_int v; @textRotation = v end + # @see wrapText def wrapText=(v) Axlsx::validate_boolean v; @wrapText = v end + # @see indent def indent=(v) Axlsx::validate_unsigned_int v; @indent = v end + # @see relativeIndent def relativeIndent=(v) Axlsx::validate_int v; @relativeIndent = v end + # @see justifyLastLine def justifyLastLine=(v) Axlsx::validate_boolean v; @justifyLastLine = v end + # @see shrinkToFit def shrinkToFit=(v) Axlsx::validate_boolean v; @shrinkToFit = v end + # @see readingOrder def readingOrder=(v) Axlsx::validate_unsigned_int v; @readingOrder = v end # Serializes the cell alignment diff --git a/lib/axlsx/stylesheet/cell_protection.rb b/lib/axlsx/stylesheet/cell_protection.rb index 7f0f4db8..e1c6183f 100644 --- a/lib/axlsx/stylesheet/cell_protection.rb +++ b/lib/axlsx/stylesheet/cell_protection.rb @@ -6,11 +6,11 @@ module Axlsx # specifies locking for cells that have the style containing this protection # @return [Boolean] - attr_accessor :hidden + attr_reader :hidden # specifies if the cells that have the style containing this protection # @return [Boolean] - attr_accessor :locked + attr_reader :locked # Creates a new CellProtection # @option options [Boolean] hidden value for hidden protection @@ -20,7 +20,10 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end + + # @see hidden def hidden=(v) Axlsx::validate_boolean v; @hidden = v end + # @see locked def locked=(v) Axlsx::validate_boolean v; @locked = v end # Serializes the cell protection diff --git a/lib/axlsx/stylesheet/cell_style.rb b/lib/axlsx/stylesheet/cell_style.rb index 11272075..f11f5193 100644 --- a/lib/axlsx/stylesheet/cell_style.rb +++ b/lib/axlsx/stylesheet/cell_style.rb @@ -5,29 +5,29 @@ module Axlsx class CellStyle # The name of this cell style # @return [String] - attr_accessor :name + attr_reader :name # The formatting record id this named style utilizes # @return [Integer] # @see Axlsx::Xf - attr_accessor :xfId + attr_reader :xfId # The buildinId to use when this named style is applied # @return [Integer] # @see Axlsx::NumFmt - attr_accessor :builtinId + attr_reader :builtinId # Determines if this formatting is for an outline style, and what level of the outline it is to be applied to. # @return [Integer] - attr_accessor :iLevel + attr_reader :iLevel # Determines if this named style should show in the list of styles when using excel # @return [Boolean] - attr_accessor :hidden + attr_reader :hidden # Indicates that the build in style reference has been customized. # @return [Boolean] - attr_accessor :customBuiltin + attr_reader :customBuiltin # Creats a new CellStyle object # @option options [String] name @@ -41,12 +41,17 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] end end - + # @see name def name=(v) Axlsx::validate_string v; @name = v end + # @see xfId def xfId=(v) Axlsx::validate_unsigned_int v; @xfId = v end + # @see builtinId def builtinId=(v) Axlsx::validate_unsigned_int v; @builtinId = v end + # @see iLivel def iLevel=(v) Axlsx::validate_unsigned_int v; @iLevel = v end + # @see hidden def hidden=(v) Axlsx::validate_boolean v; @hidden = v end + # @see customBuiltin def customBuiltin=(v) Axlsx::validate_boolean v; @customBuiltin = v end # Serializes the cell style diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb index 34567cd1..2df70ee8 100644 --- a/lib/axlsx/stylesheet/color.rb +++ b/lib/axlsx/stylesheet/color.rb @@ -3,11 +3,11 @@ module Axlsx class Color # Determines if the color is system color dependant # @return [Boolean] - attr_accessor :auto + attr_reader :auto # Backwards compatability color index # return [Integer] - #attr_accessor :indexed + #attr_reader :indexed # The color as defined in rgb terms. # @note @@ -16,16 +16,16 @@ module Axlsx # "FF000000" is black # "FFFFFFFF" is white # @return [String] - attr_accessor :rgb + attr_reader :rgb # no support for theme just yet # @return [Integer] - #attr_accessor :theme + #attr_reader :theme # The tint value. # @note valid values are between -1.0 and 1.0 # @return [Float] - attr_accessor :tint + attr_reader :tint # Creates a new Color object # @option options [Boolean] auto @@ -37,9 +37,11 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] end end - + # @see auto def auto=(v) Axlsx::validate_boolean v; @auto = v end + # @see rgb def rgb=(v) Axlsx::validate_string v; @rgb = v end + # @see tint def tint=(v) Axlsx::validate_float v; @tint = v end # This version does not support themes diff --git a/lib/axlsx/stylesheet/fill.rb b/lib/axlsx/stylesheet/fill.rb index b7477a88..ad313679 100644 --- a/lib/axlsx/stylesheet/fill.rb +++ b/lib/axlsx/stylesheet/fill.rb @@ -8,7 +8,7 @@ module Axlsx # The type of fill # @return [PatternFill, GradientFill] - attr_accessor :fill_type + attr_reader :fill_type # Creates a new Fill object # @param [PatternFill, GradientFill] fill_type @@ -24,6 +24,7 @@ module Axlsx xml.fill { @fill_type.to_xml(xml) } end + # @see fill_type def fill_type=(v) DataTypeValidator.validate "Fill.fill_type", [PatternFill, GradientFill], v; @fill_type = v; end diff --git a/lib/axlsx/stylesheet/font.rb b/lib/axlsx/stylesheet/font.rb index fbd369a3..b0c9eaae 100644 --- a/lib/axlsx/stylesheet/font.rb +++ b/lib/axlsx/stylesheet/font.rb @@ -5,7 +5,7 @@ module Axlsx class Font # The name of the font # @return [String] - attr_accessor :name + attr_reader :name # The charset of the font # @return [Integer] @@ -30,7 +30,7 @@ module Axlsx # 222 THAI_CHARSET # 238 EASTEUROPE_CHARSET # 255 OEM_CHARSET - attr_accessor :charset + attr_reader :charset # The font's family # @note @@ -43,43 +43,43 @@ module Axlsx # 5 Decorative # 6..14 Reserved for future use # @return [Integer] - attr_accessor :family + attr_reader :family # Indicates if the font should be rendered in *bold* # @return [Boolean] - attr_accessor :b + attr_reader :b # Indicates if the font should be rendered italicized # @return [Boolean] - attr_accessor :i + attr_reader :i # Indicates if the font should be rendered with a strikthrough # @return [Boolean] - attr_accessor :strike + attr_reader :strike # Indicates if the font should be rendered with an outline # @return [Boolean] - attr_accessor :outline + attr_reader :outline # Indicates if the font should be rendered with a shadow # @return [Boolean] - attr_accessor :shadow + attr_reader :shadow # Indicates if the font should be condensed # @return [Boolean] - attr_accessor :condense + attr_reader :condense # The font's extend property # @return [Boolean] - attr_accessor :extend + attr_reader :extend # The color of the font # @return [Color] - attr_accessor :color + attr_reader :color # The size of the font. # @return [Integer] - attr_accessor :sz + attr_reader :sz # Creates a new Font # @option options [String] name @@ -100,18 +100,29 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] end end - + # @see name def name=(v) Axlsx::validate_string v; @name = v end + # @see charset def charset=(v) Axlsx::validate_unsigned_int v; @charset = v end + # @see family def family=(v) Axlsx::validate_unsigned_int v; @family = v end + # @see b def b=(v) Axlsx::validate_boolean v; @b = v end + # @see i def i=(v) Axlsx::validate_boolean v; @i = v end + # @see strike def strike=(v) Axlsx::validate_boolean v; @strike = v end + # @see outline def outline=(v) Axlsx::validate_boolean v; @outline = v end + # @see shadow def shadow=(v) Axlsx::validate_boolean v; @shadow = v end + # @see condense def condense=(v) Axlsx::validate_boolean v; @condense = v end + # @see extend def extend=(v) Axlsx::validate_boolean v; @extend = v end + # @see color def color=(v) DataTypeValidator.validate "Font.color", Color, v; @color=v end + # @see sz def sz=(v) Axlsx::validate_unsigned_int v; @sz=v end # Serializes the fill diff --git a/lib/axlsx/stylesheet/gradient_fill.rb b/lib/axlsx/stylesheet/gradient_fill.rb index 8fc41a5d..2b02ac76 100644 --- a/lib/axlsx/stylesheet/gradient_fill.rb +++ b/lib/axlsx/stylesheet/gradient_fill.rb @@ -10,27 +10,27 @@ module Axlsx # :linear # :path # @return [Symbol] - attr_accessor :type + attr_reader :type # Angle of the linear gradient # @return [Float] - attr_accessor :degree + attr_reader :degree # Percentage format left # @return [Float] - attr_accessor :left + attr_reader :left # Percentage format right # @return [Float] - attr_accessor :right + attr_reader :right # Percentage format top # @return [Float] - attr_accessor :top + attr_reader :top # Percentage format bottom # @return [Float] - attr_accessor :bottom + attr_reader :bottom # Collection of stop objects # @return [SimpleTypedList] @@ -51,12 +51,18 @@ module Axlsx @stop = SimpleTypedList.new GradientStop end + # @see type def type=(v) Axlsx::validate_gradient_type v; @type = v end + # @see degree def degree=(v) Axlsx::validate_float v; @degree = v end - def left=(v) DataTypeValidator.validate "GradientFill.left", Float, v, lambda { |v| v >= 0.0 && v <= 1.0}; @left = v end - def right=(v) DataTypeValidator.validate "GradientFill.right", Float, v, lambda { |v| v >= 0.0 && v <= 1.0}; @right = v end - def top=(v) DataTypeValidator.validate "GradientFill.top", Float, v, lambda { |v| v >= 0.0 && v <= 1.0}; @top = v end - def bottom=(v) DataTypeValidator.validate "GradientFill.bottom", Float, v, lambda { |v| v >= 0.0 && v <= 1.0}; @bottom= v end + # @see left + def left=(v) DataTypeValidator.validate "GradientFill.left", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @left = v end + # @see right + def right=(v) DataTypeValidator.validate "GradientFill.right", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @right = v end + # @see top + def top=(v) DataTypeValidator.validate "GradientFill.top", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @top = v end + # @see bottom + def bottom=(v) DataTypeValidator.validate "GradientFill.bottom", Float, v, lambda { |arg| arg >= 0.0 && arg <= 1.0}; @bottom= v end # Serializes the gradientFill # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. diff --git a/lib/axlsx/stylesheet/gradient_stop.rb b/lib/axlsx/stylesheet/gradient_stop.rb index 22786fcb..d493c037 100644 --- a/lib/axlsx/stylesheet/gradient_stop.rb +++ b/lib/axlsx/stylesheet/gradient_stop.rb @@ -6,11 +6,11 @@ module Axlsx # The color for this gradient stop # @return [Color] # @see Color - attr_accessor :color + attr_reader :color # The position of the color # @return [Float] - attr_accessor :position + attr_reader :position # Creates a new GradientStop object # @param [Color] color @@ -20,8 +20,10 @@ module Axlsx self.position = position end + # @see color def color=(v) DataTypeValidator.validate "GradientStop.color", Color, v; @color=v end - def position=(v) DataTypeValidator.validate "GradientStop.position", Float, v, lambda { |v| v >= 0 && v <= 1}; @position = v end + # @see position + def position=(v) DataTypeValidator.validate "GradientStop.position", Float, v, lambda { |arg| arg >= 0 && arg <= 1}; @position = v end # Serializes the gradientStop # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. diff --git a/lib/axlsx/stylesheet/num_fmt.rb b/lib/axlsx/stylesheet/num_fmt.rb index 826599b6..0ed45e5c 100644 --- a/lib/axlsx/stylesheet/num_fmt.rb +++ b/lib/axlsx/stylesheet/num_fmt.rb @@ -37,11 +37,11 @@ module Axlsx # 48 ##0.0E+0 # 49 @ # @see Axlsx - attr_accessor :numFmtId + attr_reader :numFmtId # @return [String] The formatting to use for this number format. # @see http://support.microsoft.com/kb/264372 - attr_accessor :formatCode + attr_reader :formatCode def initialize(options={}) @numFmtId = 0 @formatCode = "" @@ -50,7 +50,9 @@ module Axlsx end end + # @see numFmtId def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end + # @see formatCode def formatCode=(v) Axlsx::validate_string v; @formatCode = v end # Creates a numFmt element applying the instance values of this object as attributes. diff --git a/lib/axlsx/stylesheet/pattern_fill.rb b/lib/axlsx/stylesheet/pattern_fill.rb index 7d674e27..a6a6f6a0 100644 --- a/lib/axlsx/stylesheet/pattern_fill.rb +++ b/lib/axlsx/stylesheet/pattern_fill.rb @@ -6,11 +6,11 @@ module Axlsx # The color to use for the the background in solid fills. # @return [Color] - attr_accessor :fgColor + attr_reader :fgColor # The color to use for the background of the fill when the type is not solid. # @return [Color] - attr_accessor :bgColor + attr_reader :bgColor # The pattern type to use # @note @@ -35,7 +35,7 @@ module Axlsx # :gray125 # :gray0625 # @see Office Open XML Part 1 18.18.55 - attr_accessor :patternType + attr_reader :patternType # Creates a new PatternFill Object # @option options [Symbol] patternType @@ -47,9 +47,11 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end - + # @see fgColor def fgColor=(v) DataTypeValidator.validate "PatternFill.fgColor", Color, v; @fgColor=v end + # @see bgColor def bgColor=(v) DataTypeValidator.validate "PatternFill.bgColor", Color, v; @bgColor=v end + # @see patternType def patternType=(v) Axlsx::validate_pattern_type v; @patternType = v end # Serializes the pattern fill diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb index 6f428ecd..e06a4026 100644 --- a/lib/axlsx/stylesheet/styles.rb +++ b/lib/axlsx/stylesheet/styles.rb @@ -197,10 +197,10 @@ module Axlsx else options[:num_fmt] || 0 end - + borderId = options[:border] || 0 raise ArgumentError, "Invalid borderId" unless borderId < borders.size - + fill = if options[:bg_color] color = Color.new(:rgb=>options[:bg_color]) pattern = PatternFill.new(:patternType =>:solid, :fgColor=>color) @@ -208,7 +208,7 @@ module Axlsx else 0 end - + fontId = if (options.values_at(:fg_color, :sz, :b, :i, :strike, :outline, :shadow, :charset, :family, :font_name).length) font = Font.new() [:b, :i, :strike, :outline, :shadow, :charset, :family, :sz].each { |k| font.send("#{k}=", options[k]) unless options[k].nil? } @@ -218,11 +218,11 @@ module Axlsx else 0 end - - applyProtection = (options[:hidden] || options[:locked]) ? 1 : 0 - + + applyProtection = (options[:hidden] || options[:locked]) ? 1 : 0 + xf = Xf.new(:fillId => fill, :fontId=>fontId, :applyFill=>1, :applyFont=>1, :numFmtId=>numFmtId, :borderId=>borderId, :applyProtection=>applyProtection) - + if options[:alignment] xf.alignment = CellAlignment.new(options[:alignment]) end @@ -230,16 +230,16 @@ module Axlsx if applyProtection xf.protection = CellProtection.new(options) end - + cellXfs << xf - end - + end + # Serializes the styles document # @return [String] def to_xml() builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| xml.styleSheet(:xmlns => XML_NS) { - [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :dxfs, :tableStyles].each do |key| + [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key| self.instance_values[key.to_s].to_xml(xml) end } @@ -280,7 +280,7 @@ module Axlsx @cellStyleXfs.lock @cellStyles = SimpleTypedList.new CellStyle - @cellStyles << CellStyle.new(:name =>"標準", :builtinId =>0, :xfId=>0) + @cellStyles << CellStyle.new(:name =>"Normal", :builtinId =>0, :xfId=>0) @cellStyles.lock @cellXfs = SimpleTypedList.new Xf, "cellXfs" diff --git a/lib/axlsx/stylesheet/table_style.rb b/lib/axlsx/stylesheet/table_style.rb index ca138b1f..77fbd448 100644 --- a/lib/axlsx/stylesheet/table_style.rb +++ b/lib/axlsx/stylesheet/table_style.rb @@ -5,15 +5,15 @@ module Axlsx # The name of this table style # @return [string] - attr_accessor :name + attr_reader :name # indicates if this style should be applied to pivot tables # @return [Boolean] - attr_accessor :pivot + attr_reader :pivot # indicates if this style should be applied to tables # @return [Boolean] - attr_accessor :table + attr_reader :table # creates a new TableStyle object # @raise [ArgumentError] if name option is not provided. @@ -28,8 +28,11 @@ module Axlsx super TableStyleElement end + # @see name def name=(v) Axlsx::validate_string v; @name=v end + # @see pivot def pivot=(v) Axlsx::validate_boolean v; @pivot=v end + # @see table def table=(v) Axlsx::validate_boolean v; @table=v end # Serializes the table style diff --git a/lib/axlsx/stylesheet/table_style_element.rb b/lib/axlsx/stylesheet/table_style_element.rb index 146a6bad..20159788 100644 --- a/lib/axlsx/stylesheet/table_style_element.rb +++ b/lib/axlsx/stylesheet/table_style_element.rb @@ -32,15 +32,15 @@ module Axlsx # :pageFieldLabels # :pageFieldValues # @return [Symbol] - attr_accessor :type + attr_reader :type # Number of rows or columns used in striping when the type is firstRowStripe, secondRowStripe, firstColumnStripe, or secondColumnStripe. # @return [Integer] - attr_accessor :size + attr_reader :size # The dxfId this style element points to # @return [Integer] - attr_accessor :dxfId + attr_reader :dxfId # creates a new TableStyleElement object # @option options [Symbol] type @@ -51,9 +51,14 @@ module Axlsx self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] end end - + + # @see type def type=(v) Axlsx::validate_table_element_type v; @type = v end + + # @see size def size=(v) Axlsx::validate_unsigned_int v; @size = v end + + # @see dxfId def dxfId=(v) Axlsx::validate_unsigned_int v; @dxfId = v end # Serializes the table style element diff --git a/lib/axlsx/stylesheet/table_styles.rb b/lib/axlsx/stylesheet/table_styles.rb index cc2005f6..bfd15261 100644 --- a/lib/axlsx/stylesheet/table_styles.rb +++ b/lib/axlsx/stylesheet/table_styles.rb @@ -5,12 +5,11 @@ module Axlsx # The default table style. The default value is 'TableStyleMedium9' # @return [String] - # - attr_accessor :defaultTableStyle + attr_reader :defaultTableStyle # The default pivot table style. The default value is 'PivotStyleLight6' # @return [String] - attr_accessor :defaultPivotStyle + attr_reader :defaultPivotStyle # Creates a new TableStyles object that is a container for TableStyle objects # @option options [String] defaultTableStyle @@ -20,8 +19,9 @@ module Axlsx @defaultPivotStyle = options[:defaultPivotStyle] || "PivotStyleLight16" super TableStyle end - + # @see defaultTableStyle def defaultTableStyle=(v) Axlsx::validate_string(v); @defaultTableStyle = v; end + # @see defaultPivotStyle def defaultPivotStyle=(v) Axlsx::validate_string(v); @defaultPivotStyle = v; end # Serializes the table styles element diff --git a/lib/axlsx/stylesheet/xf.rb b/lib/axlsx/stylesheet/xf.rb index 905eda2c..3ca1a8a2 100644 --- a/lib/axlsx/stylesheet/xf.rb +++ b/lib/axlsx/stylesheet/xf.rb @@ -1,107 +1,128 @@ module Axlsx - # The Xf class defines a formatting record for use in Styles + # The Xf class defines a formatting record for use in Styles. The recommended way to manage styles for your workbook is with Styles#add_style + # @see Styles#add_style class Xf #does not support extList (ExtensionList) # The cell alignment for this style # @return [CellAlignment] # @see CellAlignment - attr_accessor :alignment + attr_reader :alignment # The cell protection for this style # @return [CellProtection] # @see CellProtection - attr_accessor :protection + attr_reader :protection # id of the numFmt to apply to this style # @return [Integer] - attr_accessor :numFmtId + attr_reader :numFmtId # index (0 based) of the font to be used in this style # @return [Integer] - attr_accessor :fontId + attr_reader :fontId # index (0 based) of the fill to be used in this style # @return [Integer] - attr_accessor :fillId + attr_reader :fillId # index (0 based) of the border to be used in this style # @return [Integer] - attr_accessor :borderId + attr_reader :borderId # index (0 based) of cellStylesXfs item to be used in this style. Only applies to cellXfs items # @return [Integer] - attr_accessor :xfId + attr_reader :xfId # indecates if text should be prefixed by a single quote in the cell # @return [Boolean] - attr_accessor :quotePrefix + attr_reader :quotePrefix # indicates if the cell has a pivot table drop down button # @return [Boolean] - attr_accessor :pivotButton + attr_reader :pivotButton # indicates if the numFmtId should be applied # @return [Boolean] - attr_accessor :applyNumberFormat + attr_reader :applyNumberFormat # indicates if the fontId should be applied # @return [Boolean] - attr_accessor :applyFont + attr_reader :applyFont # indicates if the fillId should be applied # @return [Boolean] - attr_accessor :applyFill + attr_reader :applyFill # indicates if the borderId should be applied # @return [Boolean] - attr_accessor :applyBorder + attr_reader :applyBorder # Indicates if the alignment options should be applied # @return [Boolean] - attr_accessor :applyAlignment + attr_reader :applyAlignment # Indicates if the protection options should be applied # @return [Boolean] - attr_accessor :applyProtection + attr_reader :applyProtection # Creates a new Xf object - # @option [Integer] numFmtId - # @option [Integer] fontId - # @option [Integer] fillId - # @option [Integer] borderId - # @option [Integer] xfId - # @option [Boolean] quotePrefix - # @option [Boolean] pivotButton - # @option [Boolean] applyNumberFormat - # @option [Boolean] applyFont - # @option [Boolean] applyFill - # @option [Boolean] applyBorder - # @option [Boolean] applyAlignment - # @option [Boolean] applyProtection - # @option [CellAlignment] alignment - # @option [CellProtection] protection + # @option options [Integer] numFmtId + # @option options [Integer] fontId + # @option options [Integer] fillId + # @option options [Integer] borderId + # @option options [Integer] xfId + # @option options [Boolean] quotePrefix + # @option options [Boolean] pivotButton + # @option options [Boolean] applyNumberFormat + # @option options [Boolean] applyFont + # @option options [Boolean] applyFill + # @option options [Boolean] applyBorder + # @option options [Boolean] applyAlignment + # @option options [Boolean] applyProtection + # @option options [CellAlignment] alignment + # @option options [CellProtection] protection def initialize(options={}) options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end + # @see Xf#alignment def alignment=(v) DataTypeValidator.validate "Xf.alignment", CellAlignment, v; @alignment = v end + + # @see protection def protection=(v) DataTypeValidator.validate "Xf.protection", CellProtection, v; @protection = v end + # @see numFmtId def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end + + # @see fontId def fontId=(v) Axlsx::validate_unsigned_int v; @fontId = v end + # @see fillId def fillId=(v) Axlsx::validate_unsigned_int v; @fillId = v end + # @see borderId def borderId=(v) Axlsx::validate_unsigned_int v; @borderId = v end + # @see xfId def xfId=(v) Axlsx::validate_unsigned_int v; @xfId = v end + # @see quotePrefix def quotePrefix=(v) Axlsx::validate_boolean v; @quotePrefix = v end + # @see pivotButton def pivotButton=(v) Axlsx::validate_boolean v; @pivotButton = v end + # @see applyNumberFormat def applyNumberFormat=(v) Axlsx::validate_boolean v; @applyNumberFormat = v end + # @see applyFont def applyFont=(v) Axlsx::validate_boolean v; @applyFont = v end + # @see applyFill def applyFill=(v) Axlsx::validate_boolean v; @applyFill = v end + + # @see applyBorder def applyBorder=(v) Axlsx::validate_boolean v; @applyBorder = v end + + # @see applyAlignment def applyAlignment=(v) Axlsx::validate_boolean v; @applyAlignment = v end + + # @see applyProtection def applyProtection=(v) Axlsx::validate_boolean v; @applyProtection = v end # Serializes the xf elemen diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb index 827030c7..5e590bbe 100644 --- a/lib/axlsx/util/validators.rb +++ b/lib/axlsx/util/validators.rb @@ -31,7 +31,7 @@ module Axlsx # @raise [ArugumentError] Raised if the class of the value provided is not in the specified array of types or the block passed returns false # @return [Boolean] true if validation succeeds. # @see validate_boolean - def self.validate(name, types, v, other= lambda{|v| true }) + def self.validate(name, types, v, other= lambda{|arg| true }) types = [types] unless types.is_a? Array valid_type = false if v.class == Class @@ -49,7 +49,7 @@ module Axlsx # @raise [ArgumentError] raised if the value is not a Fixnum or Integer value greater or equal to 0 # @return [Boolean] true if the data is valid def self.validate_unsigned_int(v) - DataTypeValidator.validate(:unsigned_int, [Fixnum, Integer], v, lambda { |v| v.respond_to?(:>=) && v >= 0 }) + DataTypeValidator.validate(:unsigned_int, [Fixnum, Integer], v, lambda { |arg| arg.respond_to?(:>=) && arg >= 0 }) end # Requires that the value is a Fixnum or Integer @@ -63,7 +63,7 @@ module Axlsx # it must be one of 0, 1, "true", "false", :true, :false, true, false, "0", or "1" # @param [Any] v The value validated def self.validate_boolean(v) - DataTypeValidator.validate(:boolean, [Fixnum, String, Integer, Symbol, TrueClass, FalseClass], v, lambda { |v| [0, 1, "true", "false", :true, :false, true, false, "0", "1"].include?(v) }) + DataTypeValidator.validate(:boolean, [Fixnum, String, Integer, Symbol, TrueClass, FalseClass], v, lambda { |arg| [0, 1, "true", "false", :true, :false, true, false, "0", "1"].include?(arg) }) end # Requires that the value is a String diff --git a/lib/axlsx/version.rb b/lib/axlsx/version.rb index ed7a1ff1..2ecaf2a9 100644 --- a/lib/axlsx/version.rb +++ b/lib/axlsx/version.rb @@ -1,4 +1,4 @@ module Axlsx # version - VERSION="1.0.8" + VERSION="1.0.9" end diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 76a3803c..68f9f8b8 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -65,13 +65,6 @@ require 'axlsx/workbook/worksheet/worksheet.rb' # @return [Styles] attr_reader :styles - # The workbook relationships. This is managed automatically by the workbook - # @return [Relationships] - attr_reader :relationships - - # Instance level access to the class variable 1904 - # @return [Boolean] - attr_accessor :date1904 # Indicates if the epoc date for serialization should be 1904. If false, 1900 is used. @@ -89,9 +82,13 @@ require 'axlsx/workbook/worksheet/worksheet.rb' yield self if block_given? end - def date1904=(v) Axlsx::validate_boolean v; @@date1904 = v; end + # Instance level access to the class variable 1904 + # @return [Boolean] def date1904() @@date1904; end + # see @date1904 + def date1904=(v) Axlsx::validate_boolean v; @@date1904 = v; end + # Sets the date1904 attribute to the provided boolean # @return [Boolean] def self.date1904=(v) Axlsx::validate_boolean v; @@date1904 = v; end @@ -109,7 +106,9 @@ require 'axlsx/workbook/worksheet/worksheet.rb' yield worksheet if block_given? worksheet end - + + # The workbook relationships. This is managed automatically by the workbook + # @return [Relationships] def relationships r = Relationships.new @worksheets.each do |sheet| diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 5e535721..556a5a89 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- 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. @@ -25,7 +26,7 @@ module Axlsx # The index of the cellXfs item to be applied to this cell. # @return [Integer] # @see Axlsx::Styles - attr_accessor :style + attr_reader :style # The row this cell belongs to. # @return [Row] @@ -42,12 +43,24 @@ module Axlsx # :string to :integer or :float, type coversions always return 0 or 0.0 # :string, :integer, or :float to :time conversions always return the original value as a string and set the cells type to :string. # No support is currently implemented for parsing time strings. - # - attr_accessor :type + attr_reader :type + # @see type + def type=(v) + RestrictionValidator.validate "Cell.type", [:time, :float, :integer, :string], v + @type=v + self.value = @value + end + # The value of this cell. # @return casted value based on cell's type attribute. - attr_accessor :value + attr_reader :value + # @see value + def value=(v) + #TODO: consider doing value based type determination first? + @value = cast_value(v) + end + # @param [Row] row The row this cell belongs to. # @param [Any] value The value associated with this cell. @@ -91,16 +104,7 @@ module Axlsx @style = v end - def type=(v) - RestrictionValidator.validate "Cell.type", [:time, :float, :integer, :string], v - @type=v - self.value = @value - end - def value=(v) - #TODO: consider doing value based type determination first? - @value = cast_value(v) - end # Serializes the cell # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. @@ -108,14 +112,19 @@ module Axlsx # @note # Shared Strings are not used in this library. All values are set directly in the each sheet. def to_xml(xml) - if @type == :string - #NOTE not sure why, but xml.t @v renders the text as html entities of unicode data - xml.c(:r => r, :t=>:inlineStr, :s=>style) { xml.is { xml.t value.to_s } } + # Both 1.8 and 1.9 return the same 'fast_xf' + # ニホンゴ + # ニホンゴ + + # however nokogiri does a nice 'force_encoding' which we shall remove! + if @type == :string + xml.c(:r => r, :t=>:inlineStr, :s=>style) { xml.is { xml.t @value.to_s } } else xml.c(:r => r, :s => style) { xml.v value } end end + private # assigns the owning row for this cell. diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb index 27e60f38..96a1c3d1 100644 --- a/lib/axlsx/workbook/worksheet/row.rb +++ b/lib/axlsx/workbook/worksheet/row.rb @@ -12,10 +12,6 @@ module Axlsx # @return [SimpleTypedList] attr_reader :cells - # The index of this row in the worksheet - # @return [Integer] - attr_reader :index - # Creates a new row. New Cell objects are created based on the values, types and style options. # A new cell is created for each item in the values array. style and types options are applied as follows: # If the types option is defined and is a symbol it is applied to all the cells created. @@ -37,6 +33,8 @@ module Axlsx array_to_cells(values, options) end + # The index of this row in the worksheet + # @return [Integer] def index worksheet.rows.index(self) end diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 861bb8ef..6dfbc060 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -6,49 +6,24 @@ module Axlsx # The name of the worksheet # @return [String] - attr_accessor :name + attr_reader :name # The workbook that owns this worksheet # @return [Workbook] attr_reader :workbook - # The worksheet relationships. This is managed automatically by the worksheet - # @return [Relationships] - attr_reader :relationships # The rows in this worksheet # @note The recommended way to manage rows is Worksheet#add_row # @return [SimpleTypedList] # @see Worksheet#add_row attr_reader :rows - - # The drawing associated with this worksheet. - # @note the recommended way to work with drawings and charts is Worksheet#add_chart - # @return [Drawing] - # @see Worksheet#add_chart - attr_reader :drawing # An array of content based calculated column widths. # @note a single auto fit data item is a hash with :longest => [String] and :sz=> [Integer] members. # @return [Array] of Hash attr_reader :auto_fit_data - # The part name of this worksheet - # @return [String] - attr_reader :pn - - # The relationship part name of this worksheet - # @return [String] - attr_reader :rels_pn - - # The relationship Id of thiw worksheet - # @return [String] - attr_reader :rId - - # The index of this worksheet in the owning Workbook's worksheets list. - # @return [Integer] - attr_reader :index - # TODO Merge Cells # attr_reader :merge_cells @@ -57,6 +32,7 @@ module Axlsx # @see Workbook#add_worksheet # @option options [String] name The name of this sheet. def initialize(wb, options={}) + @drawing = nil @rows = SimpleTypedList.new Row self.workbook = wb @workbook.worksheets << self @@ -65,25 +41,38 @@ module Axlsx @magick_draw = Magick::Draw.new end - + # The name of the worksheet + # @param [String] v def name=(v) DataTypeValidator.validate "Worksheet.name", String, v; @name=v end - + + # The part name of this worksheet + # @return [String] def pn "#{WORKSHEET_PN % (index+1)}" end + # The relationship part name of this worksheet + # @return [String] def rels_pn "#{WORKSHEET_RELS_PN % (index+1)}" end + # The relationship Id of thiw worksheet + # @return [String] def rId "rId#{index+1}" end - + + # The index of this worksheet in the owning Workbook's worksheets list. + # @return [Integer] def index @workbook.worksheets.index(self) end + # The drawing associated with this worksheet. + # @note the recommended way to work with drawings and charts is Worksheet#add_chart + # @return [Drawing] + # @see Worksheet#add_chart def drawing @drawing || @drawing = Axlsx::Drawing.new(self) end @@ -149,10 +138,11 @@ module Axlsx xml.drawing :"r:id"=>"rId1" if @drawing } end - builder.to_xml(:indent=>0, :save_with=>0) + builder.to_xml end - # The worksheet's relationships. + # The worksheet relationships. This is managed automatically by the worksheet + # @return [Relationships] def relationships r = Relationships.new r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing @@ -193,7 +183,7 @@ module Axlsx # From ECMA docs # Column width measured as the number of characters of the maximum digit width of the numbers 0 .. 9 as # rendered in the normal style's font. There are 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines. - # width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256 + # width = Truncate([!{Number of Characters} * !{Maximum Digit Width} + !{5 pixel padding}]/!{Maximum Digit Width}*256)/256 # @return [Float] # @param [Hash] A hash of auto_fit_data def auto_width(col) @@ -201,6 +191,7 @@ module Axlsx mdw_count = 0 best_guess = 1.5 #direct testing shows the results of the documented formula to be a bit too small. This is a best guess scaling font_scale = col[:sz].to_f / (self.workbook.styles.fonts[0].sz.to_f || 11.0) + col[:longest].scan(/./mu).each do |i| mdw_count +=1 if @magick_draw.get_type_metrics(i).width >= mdw end |
