diff options
78 files changed, 860 insertions, 335 deletions
@@ -3,11 +3,19 @@ Axlsx: Office Open XML Spreadsheet Generation [](http://travis-ci.org/randym/axlsx/) **IRC**: [irc.freenode.net / #axlsx](irc://irc.freenode.net/axlsx) + **Git**: [http://github.com/randym/axlsx](http://github.com/randym/axlsx) + +**Twitter**: [https://twitter.com/#!/morgan_randy](https://twitter.com/#!/morgan_randy) release announcements and news will be published here + **Author**: Randy Morgan + **Copyright**: 2011 + **License**: MIT License + **Latest Version**: 1.0.18 + **Ruby Version**: 1.8.7, 1.9.2, 1.9.3 **Release Date**: March 5th 2012 @@ -16,7 +24,7 @@ Synopsis -------- Axlsx is an Office Open XML Spreadsheet generator for the Ruby programming language. -With Axlsx you can create excel worksheets with charts, images (with links), automated and fixed column widths, customizable styles, functions, merged cells, auto filters, file and stream serialization as well as full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. +With Axlsx you can create excel worksheets with charts, images (with links), automated and fixed column widths, customized styles, functions, merged cells, auto filters, file and stream serialization as well as full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. If you are working in rails, or with active record see: http://github.com/randym/acts_as_xlsx @@ -27,12 +35,12 @@ There are guides for using axlsx and acts_as_xlsx here: Help Wanted ----------- -I'd really like to get rid of the depenency on RMagick in this gem. RMagic is being used to calculate the column widths in a worksheet based on the content the user specified. If there happens to be anyone out there with the background and c skills to write an extenstion that can determine the width of a single character rendered with a specific font at a specific font size please give me a shout. +I'd really like to get rid of the dependency on RMagick in this gem. RMagic is being used to calculate the column widths in a worksheet based on the content the user specified. If there happens to be anyone out there with the background and c skills to write an extension that can determine the width of a single character rendered with a specific font at a specific font size please give me a shout. Feature List ------------ -**1. Author xlsx documents: Axlsx is made to let you easily and quickly generate profesional xlsx based reports that can be validated before serialiation. +**1. Author xlsx documents: Axlsx is made to let you easily and quickly generate professional xlsx based reports that can be validated before serialization. **2. Generate 3D Pie, Line and Bar Charts: With Axlsx chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your sheet at all. @@ -46,7 +54,7 @@ Feature List **7. Add jpg, gif and png images to worksheets with hyperlinks -**8. Reference cells in your worksheet with "A1" and "A1:D4" style references or from the workbook using "Sheett1!A3:B4" style references +**8. Reference cells in your worksheet with "A1" and "A1:D4" style references or from the workbook using "Sheet1!A3:B4" style references **9. Cell level style overrides for default and customized style objects @@ -275,17 +283,33 @@ To install Axlsx, use the following command: end ##Specify Page Margins for printing + margins = {:left => 3, :right => 3, :top => 1.2, :bottom => 1.2, :header => 0.7, :footer => 0.7} wb.add_worksheet(:name => "print margins", :page_margins => margins) do |sheet| sheet.add_row["this sheet uses customized page margins for printing"] end +##Fit to page printing + + wb.add_worksheet(:name => "fit to page") do |sheet| + sheet.add_row ['this all goes on one page'] + sheet.fit_to_page = true + end + + +##Hide Gridlines in worksheet + + wb.add_worksheet(:name => "No Gridlines") do |sheet| + sheet.add_row ["This", "Sheet", "Hides", "Gridlines"] + sheet.show_gridlines = false + end + ##Validate and Serialize p.validate.each { |e| puts e.message } p.serialize("example.xlsx") - # alternatively, serilaize to StringIO + # alternatively, serialize to StringIO s = p.to_stream() File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) } @@ -294,6 +318,16 @@ To install Axlsx, use the following command: p.use_shared_strings = true p.serialize("shared_strings_example.xlsx") +##Disabling Autowidth + + p = Axlsx::Package.new + p.use_autowidth = false + wb = p.workbook + wb.add_worksheet(:name => "No Magick") do | sheet | + sheet.add_row ['oh look! no autowidth - and no magick loaded in your process'] + end + p.validate.each { |e| puts e.message } + p.serialize("no-use_autowidth.xlsx") #Documentation @@ -307,8 +341,20 @@ This gem is 100% documented with YARD, an exceptional documentation library. To ------ This gem has 100% test coverage using test/unit. To execute tests for this gem, simply run rake in the gem directory. -#Changelog +#Change log --------- +- ** March.??.12**: 1.0.19 release + - bugfix patch name_to_indecies to properly handle extended ranges. + - bugfix properly serialize chart title. + - lower rake minimum requirement for 1.8.7 apps that don't want to move on to 0.9 NOTE this will be reverted for 2.0.0 with workbook parsing! + - Added Fit to Page printing + - added support for turning off gridlines in charts. + - added support for turning off gridlines in worksheet. + - bugfix some apps like libraoffice require apply[x] attributes to be true. applyAlignment is now properly set. + - added option to *not* use RMagick - and default all assigned columns to the excel default of 8.43 + - added border style specification to styles#add_style - now you can pass in :border => {:style => :thin, :color =>"0000FF"} instead of creating a border object and border parts manually each time. + - Support for tables added in - Note: Pre 2011 versions of Mac office do not support this feature. + - ** March.5.12**: 1.0.18 release https://github.com/randym/axlsx/compare/1.0.17...1.0.18 - bugfix custom borders are not properly applied when using styles.add_style @@ -324,13 +370,13 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem, - Added in support for serializing to StringIO - Added in support for using shared strings table. This makes most of the features in axlsx interoperable with iWorks Numbers - Added in support for fixed column_widths - - Removed unneded depenencies on activesupport and i18n + - Removed unneeded dependencies on active-support and i18n - ** February.2.12**: 1.0.16 release https://github.com/randym/axlsx/compare/1.0.15...1.0.16 - Bug fix for schema file locations when validating in rails - Added hyperlink to images - - date1904 now automatically set in bsd and mac environments + - date1904 now automatically set in BSD and mac environments - removed whitespace/indentation from xml outputs - col_style now skips rows that do not contain cells at the column index @@ -353,7 +399,7 @@ Please see the {file:CHANGELOG.md} document for past release information. [jurriaan](https://github.com/jurriaan) - for showing there is more than one way to skin a cat, and work with rows while you are at it. -[joekain](https://github.com/joekain) - for keeping our refereneces working even in the double digits! +[joekain](https://github.com/joekain) - for keeping our references working even in the double digits! #Copyright and License ---------- diff --git a/examples/example.rb b/examples/example.rb index df5a401f..2c0f5fc1 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby -w -s # -*- coding: utf-8 -*- -require 'axlsx.rb' +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" +require 'axlsx' p = Axlsx::Package.new wb = p.workbook @@ -23,6 +24,19 @@ wb.styles do |s| end end +#Using Custom Border Styles + +wb.styles do |s| + red_border = s.add_style :border => {:style=>:thin, :color =>"FFFF0000"} + blue_border = s.add_style :border => {:style=>:thin, :color =>"FF0000FF"} + + wb.add_worksheet(:name => "Custom Borders") do |sheet| + sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border + sheet.add_row [1, 2, 3], :style => blue_border + end +end + + ##Using Custom Formatting and date1904 require 'date' wb.styles do |s| @@ -36,10 +50,11 @@ wb.styles do |s| end end + ##Add an Image wb.add_worksheet(:name => "Images") do |sheet| - img = File.expand_path('examples/image1.jpeg') + img = File.expand_path('../image1.jpeg', __FILE__) sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image| image.width=720 image.height=666 @@ -50,7 +65,7 @@ end ##Add an Image with a hyperlink wb.add_worksheet(:name => "Image with Hyperlink") do |sheet| - img = File.expand_path('examples/image1.jpeg') + img = File.expand_path('../image1.jpeg', __FILE__) sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image| image.width=720 image.height=666 @@ -144,6 +159,18 @@ wb.add_worksheet(:name => "Bar Chart") do |sheet| end end +##Hide Gridlines in chart +wb.add_worksheet(:name => "Chart With No Gridlines") do |sheet| + sheet.add_row ["A Simple Bar Chart"] + sheet.add_row ["First", "Second", "Third"] + sheet.add_row [1, 2, 3] + sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17") do |chart| + chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"], :title => sheet["A1"] + chart.valAxis.gridlines = false + chart.catAxis.gridlines = false + end +end + ##Generating A Pie Chart wb.add_worksheet(:name => "Pie Chart") do |sheet| @@ -194,6 +221,18 @@ wb.add_worksheet(:name => "Auto Filter") do |sheet| sheet.auto_filter = "A2:D5" end +##Tables + + wb.add_worksheet(:name => "Table") do |sheet| + sheet.add_row ["Build Matrix"] + sheet.add_row ["Build", "Duration", "Finished", "Rvm"] + sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"] + sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"] + sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"] + sheet.add_table "A2:D5", :name => 'Build Matrix' + end + + ##Specifying Column Widths wb.add_worksheet(:name => "custom column widths") do |sheet| @@ -201,6 +240,13 @@ wb.add_worksheet(:name => "custom column widths") do |sheet| sheet.column_widths nil, 3 end +##Fit to page printing + +wb.add_worksheet(:name => "fit to page") do |sheet| + sheet.add_row ['this all goes on one page'] + sheet.fit_to_page = true +end + ##Hide Gridlines in worksheet wb.add_worksheet(:name => "No Gridlines") do |sheet| @@ -216,7 +262,7 @@ end ##Validate and Serialize -p.validate.each { |e| puts e.message } +#p.validate.each { |e| puts e.message } p.serialize("example.xlsx") s = p.to_stream() @@ -228,6 +274,15 @@ p.use_shared_strings = true p.serialize("shared_strings_example.xlsx") +##Disabling Autowidth +p = Axlsx::Package.new +p.use_autowidth = false +wb = p.workbook +wb.add_worksheet(:name => "No Magick") do | sheet | + sheet.add_row ['oh look! no autowidth - and no magick loaded in your process'] +end +p.validate.each { |e| puts e.message } +p.serialize("no-use_autowidth.xlsx") diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 8b8fb7c5..eb5068b5 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -22,10 +22,8 @@ require 'axlsx/rels/relationships.rb' require 'axlsx/drawing/drawing.rb' require 'axlsx/workbook/workbook.rb' require 'axlsx/package.rb' - #required gems require 'nokogiri' -require 'RMagick' require 'zip/zip' #core dependencies @@ -33,8 +31,8 @@ require 'bigdecimal' require 'time' #if object does not have this already, I am borrowing it from active_support. -# I am a very big fan of activesupports instance_values method, but do not want to require nor include the entire -# library just for this one method. +# I am a very big fan of activesupports instance_values method, but do not want to require nor include the entire +# library just for this one method. if !Object.respond_to?(:instance_values) Object.send :public # patch for 1.8.7 as it uses private scope Object.send :define_method, :instance_values do @@ -42,12 +40,11 @@ if !Object.respond_to?(:instance_values) end end - # xlsx generation with charts, images, automated column width, customizable styles and full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. Check out the README for some examples of how easy it is. Best of all, you can validate your xlsx file before serialization so you know for sure that anything generated is going to load on your client's machine. module Axlsx # determines the cell range for the items provided def self.cell_range(items) - return "" unless items.first.is_a? Cell + return "" unless items.first.is_a? Cell ref = "'#{items.first.row.worksheet.name}'!" + "#{items.first.r_abs}" ref += ":#{items.last.r_abs}" if items.size > 1 @@ -56,8 +53,8 @@ module Axlsx def self.name_to_indices(name) raise ArgumentError, 'invalid cell name' unless name.size > 1 - v = name[/[A-Z]+/].reverse.chars.reduce({:base=>1, :i=>0}) do |val, c| - val[:i] += ((c.bytes.first - 65) + val[:base]); val[:base] *= 26; val + v = name[/[A-Z]+/].reverse.chars.reduce({:base=>1, :i=>0}) do |val, c| + val[:i] += ((c.bytes.first - 65) + val[:base]); val[:base] *= 26; val end [v[:i]-1, ((name[/[1-9][0-9]*/]).to_i)-1] diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index b9a31150..93068a23 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -6,6 +6,7 @@ module Axlsx require 'axlsx/drawing/pie_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' require 'axlsx/drawing/axis.rb' @@ -15,6 +16,7 @@ module Axlsx require 'axlsx/drawing/cat_axis_data.rb' require 'axlsx/drawing/val_axis_data.rb' + require 'axlsx/drawing/named_axis_data.rb' require 'axlsx/drawing/marker.rb' @@ -27,6 +29,7 @@ module Axlsx require 'axlsx/drawing/pie_3D_chart.rb' require 'axlsx/drawing/bar_3D_chart.rb' require 'axlsx/drawing/line_3D_chart.rb' + require 'axlsx/drawing/scatter_chart.rb' require 'axlsx/drawing/picture_locking.rb' require 'axlsx/drawing/pic.rb' diff --git a/lib/axlsx/drawing/named_axis_data.rb b/lib/axlsx/drawing/named_axis_data.rb new file mode 100644 index 00000000..b249196f --- /dev/null +++ b/lib/axlsx/drawing/named_axis_data.rb @@ -0,0 +1,31 @@ +# encoding: UTF-8 +module Axlsx + # The ValAxisData class manages the values for a chart value series. + class NamedAxisData < CatAxisData + + def initialize(name, data=[]) + super(data) + @name = 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] + def to_xml(xml) + xml.send(@name) { + xml.numRef { + xml.f Axlsx::cell_range(@list) + xml.numCache { + xml.formatCode 'General' + xml.ptCount :val=>size + each_with_index do |item, index| + 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/scatter_chart.rb b/lib/axlsx/drawing/scatter_chart.rb new file mode 100644 index 00000000..f257303a --- /dev/null +++ b/lib/axlsx/drawing/scatter_chart.rb @@ -0,0 +1,50 @@ +# encoding: UTF-8 +module Axlsx + class ScatterChart < Chart + attr_reader :scatterStyle + + # the x value axis + # @return [ValAxis] + attr_reader :xValAxis + + # the y value axis + # @return [ValAxis] + attr_reader :yValAxis + + def initialize(frame, options={}) + @scatterStyle = :lineMarker + @xValAxId = rand(8 ** 8) + @yValAxId = rand(8 ** 8) + @xValAxis = ValAxis.new(@xValAxId, @yValAxId) + @yValAxis = ValAxis.new(@yValAxId, @xValAxId) + super(frame, options) + @series_type = ScatterSeries + end + + # Serializes the bar chart + # @return [String] + def to_xml + super() do |xml| + xml.scatterChart { + xml.scatterStyle :val=>scatterStyle + + # This is all repeated from line_3D_chart.rb! + xml.varyColors :val=>1 + @series.each { |ser| ser.to_xml(xml) } + xml.dLbls { + xml.showLegendKey :val=>0 + xml.showVal :val=>0 + xml.showCatName :val=>0 + xml.showSerName :val=>0 + xml.showPercent :val=>0 + xml.showBubbleSize :val=>0 + } + xml.axId :val=>@xValAxId + xml.axId :val=>@yValAxId + } + @xValAxis.to_xml(xml) + @yValAxis.to_xml(xml) + end + end + end +end diff --git a/lib/axlsx/drawing/scatter_series.rb b/lib/axlsx/drawing/scatter_series.rb new file mode 100644 index 00000000..52145cd1 --- /dev/null +++ b/lib/axlsx/drawing/scatter_series.rb @@ -0,0 +1,31 @@ +# encoding: UTF-8 +module Axlsx + class ScatterSeries < Series + # The x data for this series. + # @return [NamedAxisData] + attr_reader :xData + + # The y data for this series. + # @return [NamedAxisData] + attr_reader :yData + + def initialize(chart, options={}) + @xData, @yData = nil + super(chart, options) + + @xData = NamedAxisData.new("xVal", options[:xData]) unless options[:xData].nil? + @yData = NamedAxisData.new("yVal", options[:yData]) unless options[:yData].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_inner| + @xData.to_xml(xml_inner) unless @xData.nil? + @yData.to_xml(xml_inner) unless @yData.nil? + end + end + + end +end diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 6a8864f4..73e72493 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -29,6 +29,14 @@ module Axlsx yield self if block_given? end + # Shortcut to specify that the workbook should use autowidth + # @see Workbook#use_autowidth + def use_autowidth=(v) + Axlsx::validate_boolean(v); + workbook.use_autowidth = v + end + + # Shortcut to specify that the workbook should use shared strings # @see Workbook#use_shared_strings def use_shared_strings=(v) @@ -175,6 +183,10 @@ module Axlsx @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} + end + workbook.charts.each do |chart| @parts << {:entry => "xl/#{chart.pn}", :doc => chart.to_xml, :schema => DRAWING_XSD} end @@ -224,6 +236,10 @@ module Axlsx c_types << Axlsx::Override.new(:PartName => "/xl/#{chart.pn}", :ContentType => CHART_CT) end + workbook.tables.each do |table| + c_types << Axlsx::Override.new(:PartName => "/xl/#{table.pn}", + :ContentType => TABLE_CT) + end workbook.worksheets.each do |sheet| c_types << Axlsx::Override.new(:PartName => "/xl/#{sheet.pn}", :ContentType => WORKSHEET_CT) diff --git a/lib/axlsx/stylesheet/border.rb b/lib/axlsx/stylesheet/border.rb index 334ed8d3..f3d329fa 100644 --- a/lib/axlsx/stylesheet/border.rb +++ b/lib/axlsx/stylesheet/border.rb @@ -3,16 +3,16 @@ module Axlsx # This class details a border used in Office Open XML spreadsheet styles. 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. + # @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_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_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_reader :outline - # @return [SimpleTypedList] A list of BorderPr objects for this border. + # @return [SimpleTypedList] A list of BorderPr objects for this border. attr_reader :prs # Creates a new Border object @@ -20,21 +20,20 @@ module Axlsx # @option options [Boolean] diagonalDown # @option options [Boolean] outline # @example Making a border - # p = Package.new - # red_border = Border.new - # [:left, :right, :top, :bottom].each do |item| - # red_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FFFF0000")) # - # end - # # this sets red_border to be the index for the created border. - # red_border = p.workbook.styles.@borders << red_border - # #used in row creation as follows. This will add a red border to each of the cells in the row. - # p.workbook.add_worksheet.rows << :values=>[1,2,3] :style=>red_border + # p = Axlsx::Package.new + # red_border = p.workbook.styles.add_style :border => {:style =>: thin, :color => "FFFF0000"} + # ws = p.workbook.add_worksheet + # ws.add_row [1,2,3], :style => red_border + # p.serialize('red_border.xlsx') + # + # @note The recommended way to manage borders is with Style#add_style + # @see Style#add_style def initialize(options={}) @prs = SimpleTypedList.new BorderPr options.each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end - end + end # @see diagonalUp def diagonalUp=(v) Axlsx::validate_boolean v; @diagonalUp = v end @@ -48,8 +47,8 @@ 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 do |part| - part.to_xml(xml) + @prs.select { |pr| pr.name == k }.each do |part| + part.to_xml(xml) end end } diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb index cb40ee74..a05067cd 100644 --- a/lib/axlsx/stylesheet/styles.rb +++ b/lib/axlsx/stylesheet/styles.rb @@ -133,7 +133,7 @@ module Axlsx # @option options [String] font_name The name of the font to use # @option options [Integer] num_fmt The number format to apply # @option options [String] format_code The formatting to apply. If this is specified, num_fmt is ignored. - # @option options [Integer] border The border style to use. + # @option options [Integer] border The border style to use. This can be the index of an existing border or a hash like {:style => :thin, :color => "FFFF0000"} to create a new border style # @option options [String] bg_color The background color to apply to the cell # @option options [Boolean] hidden Indicates if the cell should be hidden # @option options [Boolean] locked Indicates if the cell should be locked @@ -201,6 +201,16 @@ module Axlsx borderId = options[:border] || 0 + if borderId.is_a?(Hash) + raise ArgumentError, "border hash definitions must include both style and color" unless borderId.keys.include?(:style) && borderId.keys.include?(:color) + + s = borderId.delete :style + c = borderId.delete :color + border = Border.new + [:left, :right, :top, :bottom].each {|pr| border.prs << BorderPr.new(:name => pr, :style=>s, :color => Color.new(:rgb => c))} + borderId = self.borders << border + end + raise ArgumentError, "Invalid borderId" unless borderId < borders.size fill = if options[:bg_color] diff --git a/lib/axlsx/util/constants.rb b/lib/axlsx/util/constants.rb index bdda9216..588dada5 100644 --- a/lib/axlsx/util/constants.rb +++ b/lib/axlsx/util/constants.rb @@ -171,6 +171,9 @@ module Axlsx # drawing rels part DRAWING_RELS_PN = "drawings/_rels/drawing%d.xml.rels" + + # drawing part + TABLE_PN = "tables/table%d.xml" # chart part CHART_PN = "charts/chart%d.xml" @@ -228,4 +231,6 @@ module Axlsx # error message for duplicate sheet names ERR_DUPLICATE_SHEET_NAME = "There is already a worksheet in this workbook named '%s'. Please use a unique name" + + FIXED_COL_WIDTH = 8.43 end diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 3350b3ff..a8e0a1af 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -7,6 +7,7 @@ require 'axlsx/workbook/worksheet/page_margins.rb' require 'axlsx/workbook/worksheet/row.rb' require 'axlsx/workbook/worksheet/worksheet.rb' require 'axlsx/workbook/shared_strings_table.rb' +require 'axlsx/workbook/worksheet/table.rb' # The Workbook class is an xlsx workbook that manages worksheets, charts, drawings and styles. # The following parts of the Office Open XML spreadsheet specification are not implimented in this version. @@ -46,7 +47,7 @@ require 'axlsx/workbook/shared_strings_table.rb' end - # A collection of worksheets associated with this workbook. + # A collection of worksheets associated with this workbook. # @note The recommended way to manage worksheets is add_worksheet # @see Workbook#add_worksheet # @see Worksheet @@ -74,6 +75,14 @@ require 'axlsx/workbook/shared_strings_table.rb' # @return [SimpleTypedList] attr_reader :drawings + # A colllection of tables associated with this workbook + # @note The recommended way to manage drawings is Worksheet#add_table + # @see Worksheet#add_table + # @see Table + # @return [SimpleTypedList] + attr_reader :tables + + # The styles associated with this workbook # @note The recommended way to manage styles is Styles#add_style # @see Style#add_style @@ -88,6 +97,7 @@ require 'axlsx/workbook/shared_strings_table.rb' # Indicates if the epoc date for serialization should be 1904. If false, 1900 is used. @@date1904 = false + # lets come back to this later when we are ready for parsing. #def self.parse entry # io = entry.get_input_stream @@ -106,6 +116,9 @@ require 'axlsx/workbook/shared_strings_table.rb' @drawings = SimpleTypedList.new Drawing @charts = SimpleTypedList.new Chart @images = SimpleTypedList.new Pic + @tables = SimpleTypedList.new Table + @use_autowidth = true + self.date1904= !options[:date1904].nil? && options[:date1904] yield self if block_given? end @@ -125,6 +138,14 @@ require 'axlsx/workbook/shared_strings_table.rb' # @return [Boolean] def self.date1904() @@date1904; end + # Indicates if the workbook should use autowidths or not. + # this must be set before instantiating a worksheet to avoid Rmagix inclusion + # @return [Boolean] + def use_autowidth() @use_autowidth; end + + # see @use_autowidth + def use_autowidth=(v) Axlsx::validate_boolean v; @use_autowidth = v; end + # Adds a worksheet to this workbook # @return [Worksheet] # @option options [String] name The name of the worksheet. diff --git a/lib/axlsx/workbook/worksheet/table.rb b/lib/axlsx/workbook/worksheet/table.rb new file mode 100644 index 00000000..831040f3 --- /dev/null +++ b/lib/axlsx/workbook/worksheet/table.rb @@ -0,0 +1,97 @@ +# encoding: UTF-8 +module Axlsx + # Table + # @note Worksheet#add_table is the recommended way to create tables for your worksheets. + # @see README for examples + class Table + + + # The reference to the table data + # @return [String] + attr_reader :ref + + # The name of the table. + # @return [String] + attr_reader :name + + # The style for the table. + # @return [TableStyle] + attr_reader :style + + # Creates a new Table object + # @param [String] ref The reference to the table data. + # @param [Sheet] ref The sheet containing the table data. + # @option options [Cell, String] name + # @option options [TableStyle] style + def initialize(ref, sheet, options={}) + @ref = ref + @sheet = sheet + @style = nil + @sheet.workbook.tables << self + @name = "Table#{index+1}" + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + yield self if block_given? + end + + # The index of this chart in the workbooks charts collection + # @return [Integer] + def index + @sheet.workbook.tables.index(self) + end + + # The part name for this table + # @return [String] + def pn + "#{TABLE_PN % (index+1)}" + end + + # The relation reference id for this table + # @return [String] + def rId + "rId#{index+1}" + end + + # The name of the Table. + # @param [String, Cell] v + # @return [Title] + def name=(v) + DataTypeValidator.validate "#{self.class}.name", [String], v + if v.is_a?(String) + @name = v + end + 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) + def header_cells + header = @ref.gsub(/^(\w+)(\d+)\:(\w+)\d+$/, '\1\2:\3\2') + @sheet[header] + end + end +end diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index a514c31c..5325294f 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -12,6 +12,9 @@ module Axlsx # @return [Workbook] attr_reader :workbook + # The tables in this worksheet + # @return [Array] of Table + attr_reader :tables # The rows in this worksheet # @note The recommended way to manage rows is Worksheet#add_row @@ -39,6 +42,17 @@ module Axlsx # @return Boolean attr_reader :show_gridlines + + # Indicates if the worksheet is selected in the workbook + # It is possible to have more than one worksheet selected, however it might cause issues + # in some older versions of excel when using copy and paste. + # @return Boolean + attr_reader :selected + + # Indicates if the worksheet should print in a single page + # @return Boolean + attr_reader :fit_to_page + # Page margins for printing the worksheet. # @example # wb = Axlsx::Package.new.workbook @@ -69,19 +83,33 @@ module Axlsx # @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins # @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet. def initialize(wb, options={}) - @drawing = @page_margins = @auto_filter = nil - @show_gridlines = true - @rows = SimpleTypedList.new Row self.workbook = wb @workbook.worksheets << self - @auto_fit_data = [] - self.name = options[:name] || "Sheet" + (index+1).to_s - @magick_draw = Magick::Draw.new - @cols = SimpleTypedList.new Cell + @drawing = @page_margins = @auto_filter = nil @merged_cells = [] + @auto_fit_data = [] + @selected = false + @show_gridlines = true + self.name = "Sheet" + (index+1).to_s @page_margins = PageMargins.new options[:page_margins] if options[:page_margins] + + @rows = SimpleTypedList.new Row + @cols = SimpleTypedList.new Cell + @tables = SimpleTypedList.new Table + + if self.workbook.use_autowidth + require 'RMagick' unless defined?(Magick) + @magick_draw = Magick::Draw.new + else + @magick_draw = nil + end + + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + end # convinience method to access all cells in this worksheet @@ -124,6 +152,21 @@ module Axlsx @show_gridlines = v end + # @see selected + # @return [Boolean] + def selected=(v) + Axlsx::validate_boolean v + @selected = v + end + + + # Indicates if gridlines should be shown in the sheet. + # This is true by default. + # @return [Boolean] + def fit_to_page=(v) + Axlsx::validate_boolean v + @fit_to_page = v + end # Returns the cell or cells defined using excel style A1:B3 references. # @param [String|Integer] cell_def the string defining the cell or range of cells, or the rownumber @@ -332,6 +375,13 @@ module Axlsx chart end + def add_table(ref, options={}) + table = Table.new(ref, self, options) + @tables << table + yield table if block_given? + table + end + # Adds a media item to the worksheets drawing # @param [Class] media_type # @option options [] unknown @@ -347,13 +397,15 @@ module Axlsx builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| xml.worksheet(:xmlns => XML_NS, :'xmlns:r' => XML_NS_R) { + xml.sheetPr { + xml.pageSetUpPr :fitToPage => fit_to_page if fit_to_page + } # another patch for the folks at rubyXL as thier parser depends on this optional element. xml.dimension :ref=>dimension unless rows.size == 0 # this is required by rubyXL, spec says who cares - but it seems they didnt notice - # however, it also seems to be causing some odd [Grouped] stuff in excel 2011 - so - # removing until I understand it better. + # grouping issue resolved by keeping tabSelected set to 0 xml.sheetViews { - xml.sheetView(:tabSelected => 1, :workbookViewId => 0, :showGridLines => show_gridlines) { + xml.sheetView(:tabSelected => @selected, :workbookViewId => 0, :showGridLines => show_gridlines) { xml.selection :activeCell=>"A1", :sqref => "A1" } } @@ -375,6 +427,13 @@ module Axlsx xml.mergeCells(:count=>@merged_cells.size) { @merged_cells.each { | mc | xml.mergeCell(:ref=>mc) } } unless @merged_cells.empty? page_margins.to_xml(xml) if @page_margins xml.drawing :"r:id"=>"rId1" if @drawing + unless @tables.empty? + xml.tableParts(:count => @tables.length) { + @tables.each do |table| + xml.tablePart :'r:id' => table.rId + end + } + end } end builder.to_xml(:save_with => 0) @@ -384,6 +443,9 @@ module Axlsx # @return [Relationships] def relationships r = Relationships.new + @tables.each do |table| + r << Relationship.new(TABLE_R, "../#{table.pn}") + end r << Relationship.new(DRAWING_R, "../#{@drawing.pn}") if @drawing r end @@ -435,7 +497,7 @@ module Axlsx # @param [Hash] A hash of auto_fit_data def auto_width(col) return col[:fixed] unless col[:fixed] == nil - + return Axlsx::FIXED_COL_WIDTH unless self.workbook.use_autowidth mdw_count, font_scale, mdw = 0, col[:sz]/11.0, 6.0 mdw_count = col[:longest].scan(/./mu).reduce(0) do | count, char | count +=1 if @magick_draw.get_type_metrics(char).max_advance >= mdw diff --git a/test/content_type/tc_content_type.rb b/test/content_type/tc_content_type.rb index 5757b980..79141748 100644 --- a/test/content_type/tc_content_type.rb +++ b/test/content_type/tc_content_type.rb @@ -1,7 +1,5 @@ # encoding: UTF-8 - -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestContentType < Test::Unit::TestCase def setup diff --git a/test/content_type/tc_default.rb b/test/content_type/tc_default.rb index 90c0934e..e5245b38 100644 --- a/test/content_type/tc_default.rb +++ b/test/content_type/tc_default.rb @@ -1,10 +1,8 @@ # encoding: UTF-8 - -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestDefault < Test::Unit::TestCase - def setup + def setup end def teardown end @@ -19,7 +17,7 @@ class TestDefault < Test::Unit::TestCase def test_content_type_restriction assert_raise(ArgumentError, "raises argument error if invlalid ContentType is") { Axlsx::Default.new :ContentType=>"asdf" } end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::CONTENT_TYPES_XSD)) type = Axlsx::Default.new :Extension=>"xml", :ContentType=>Axlsx::XML_CT @@ -35,7 +33,7 @@ class TestDefault < Test::Unit::TestCase errors << error end assert_equal(errors.size, 0, "[Content Types].xml Invalid" + errors.map{ |e| e.message }.to_s) - + end diff --git a/test/content_type/tc_override.rb b/test/content_type/tc_override.rb index 911ec649..5005d12d 100644 --- a/test/content_type/tc_override.rb +++ b/test/content_type/tc_override.rb @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestOverride < Test::Unit::TestCase - def setup + def setup end def teardown end @@ -18,7 +17,7 @@ class TestOverride < Test::Unit::TestCase def test_content_type_restriction assert_raise(ArgumentError, "requires known content type") { Axlsx::Override.new :ContentType=>"asdf" } end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::CONTENT_TYPES_XSD)) type = Axlsx::Override.new :PartName=>"somechart.xml", :ContentType=>Axlsx::CHART_CT @@ -34,7 +33,7 @@ class TestOverride < Test::Unit::TestCase errors << error end assert_equal(errors.size, 0, "Override content type caused invalid content_type doc" + errors.map{ |e| e.message }.to_s) - + end diff --git a/test/doc_props/tc_app.rb b/test/doc_props/tc_app.rb index 546896a0..31c87c41 100644 --- a/test/doc_props/tc_app.rb +++ b/test/doc_props/tc_app.rb @@ -1,12 +1,11 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestApp < Test::Unit::TestCase - def setup + def setup end def teardown end - + def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::APP_XSD)) doc = Nokogiri::XML(Axlsx::App.new.to_xml) diff --git a/test/doc_props/tc_core.rb b/test/doc_props/tc_core.rb index 5dd490a8..5e75d812 100644 --- a/test/doc_props/tc_core.rb +++ b/test/doc_props/tc_core.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCore < Test::Unit::TestCase - - def setup + + def setup @core = Axlsx::Core.new @doc = Nokogiri::XML(@core.to_xml) end @@ -21,7 +20,7 @@ class TestCore < Test::Unit::TestCase def test_populates_created assert_equal(@doc.xpath('//dcterms:created').text, Time.now.strftime('%Y-%m-%dT%H:%M:%S'), "dcterms:created incorrect") end - + def test_populates_default_name assert_equal(@doc.xpath('//dc:creator').text, "axlsx", "Default name not populated") end diff --git a/test/drawing/tc_axis.rb b/test/drawing/tc_axis.rb index cad6b07a..234eba9c 100644 --- a/test/drawing/tc_axis.rb +++ b/test/drawing/tc_axis.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestAxis < Test::Unit::TestCase - def setup + def setup @axis = Axlsx::Axis.new 12345, 54321 end def teardown diff --git a/test/drawing/tc_bar_3D_chart.rb b/test/drawing/tc_bar_3D_chart.rb index ff01464c..93997730 100644 --- a/test/drawing/tc_bar_3D_chart.rb +++ b/test/drawing/tc_bar_3D_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestBar3DChart < Test::Unit::TestCase @@ -13,7 +12,7 @@ class TestBar3DChart < Test::Unit::TestCase def teardown end - def test_initialization + def test_initialization assert_equal(@chart.grouping, :clustered, "grouping defualt incorrect") assert_equal(@chart.series_type, Axlsx::BarSeries, "series type incorrect") assert_equal(@chart.barDir, :bar, " bar direction incorrect") @@ -61,6 +60,6 @@ class TestBar3DChart < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_bar_series.rb b/test/drawing/tc_bar_series.rb index afd6004a..b07b42a4 100644 --- a/test/drawing/tc_bar_series.rb +++ b/test/drawing/tc_bar_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestBarSeries < Test::Unit::TestCase @@ -9,12 +8,12 @@ class TestBarSeries < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Bar3DChart, :title => "fishery" @series = chart.add_series :data=>[0,1,2], :labels=>["zero", "one", "two"], :title=>"bob" end - + def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") - assert_equal(@series.shape, :box, "series shape has been applied") + assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") + assert_equal(@series.shape, :box, "series shape has been applied") end def test_data diff --git a/test/drawing/tc_cat_axis.rb b/test/drawing/tc_cat_axis.rb index 23ed2eca..a4f25a1c 100644 --- a/test/drawing/tc_cat_axis.rb +++ b/test/drawing/tc_cat_axis.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCatAxis < Test::Unit::TestCase - def setup + def setup @axis = Axlsx::CatAxis.new 12345, 54321 end def teardown diff --git a/test/drawing/tc_cat_axis_data.rb b/test/drawing/tc_cat_axis_data.rb index e8a71fb1..b176c0f0 100644 --- a/test/drawing/tc_cat_axis_data.rb +++ b/test/drawing/tc_cat_axis_data.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCatAxisData < Test::Unit::TestCase @@ -9,7 +8,7 @@ class TestCatAxisData < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Bar3DChart @series = chart.add_series :labels=>["zero", "one", "two"] end - + def test_initialize assert(@series.labels.is_a?Axlsx::SimpleTypedList) assert_equal(@series.labels, ["zero", "one", "two"]) diff --git a/test/drawing/tc_chart.rb b/test/drawing/tc_chart.rb index 474266d5..c0e938cd 100644 --- a/test/drawing/tc_chart.rb +++ b/test/drawing/tc_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestChart < Test::Unit::TestCase @@ -16,7 +15,7 @@ class TestChart < Test::Unit::TestCase def test_initialization assert_equal(@p.workbook.charts.last,@chart, "the chart is in the workbook") assert_equal(@chart.title.text, "fishery", "the title option has been applied") - assert((@chart.series.is_a?(Axlsx::SimpleTypedList) && @chart.series.empty?), "The series is initialized and empty") + assert((@chart.series.is_a?(Axlsx::SimpleTypedList) && @chart.series.empty?), "The series is initialized and empty") end def test_title @@ -47,18 +46,18 @@ class TestChart < Test::Unit::TestCase assert_equal(@chart.graphic_frame.anchor.to.row, 90) end - def test_add_series + def test_add_series s = @chart.add_series :data=>[0,1,2,3], :labels => ["one", 1, "anything"], :title=>"bob" assert_equal(@chart.series.last, s, "series has been added to chart series collection") assert_equal(s.title.text, "bob", "series title has been applied") assert_equal(s.data, [0,1,2,3], "data option applied") - assert_equal(s.labels, ["one",1,"anything"], "labels option applied") - end - + assert_equal(s.labels, ["one",1,"anything"], "labels option applied") + end + def test_pn assert_equal(@chart.pn, "charts/chart1.xml") end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) doc = Nokogiri::XML(@chart.to_xml) @@ -68,6 +67,6 @@ class TestChart < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_drawing.rb b/test/drawing/tc_drawing.rb index 7dd21c32..ac2dddbe 100644 --- a/test/drawing/tc_drawing.rb +++ b/test/drawing/tc_drawing.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestDrawing < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet @@ -24,14 +23,14 @@ class TestDrawing < Test::Unit::TestCase assert_equal([anchor.to.row, anchor.to.col], [1,1], "options for start at are applied") assert_equal(chart.title.text, "bob", "option for title is applied") end - + def test_add_image src = File.dirname(__FILE__) + "/../../examples/image1.jpeg" image = @ws.add_image(:image_src => src, :start_at=>[0,0], :width=>600, :height=>400) assert(@ws.drawing.anchors.last.is_a?(Axlsx::OneCellAnchor)) assert(image.is_a?(Axlsx::Pic)) assert_equal(600, image.width) - assert_equal(400, image.height) + assert_equal(400, image.height) end def test_charts @@ -39,7 +38,7 @@ class TestDrawing < Test::Unit::TestCase chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1]) assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"nancy", :start_at=>[1,5], :end_at=>[5,10]) - assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") + assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") end def test_pn @@ -53,7 +52,7 @@ class TestDrawing < Test::Unit::TestCase def test_rId assert_equal(@ws.drawing.rId, "rId1") end - + def test_index assert_equal(@ws.drawing.index, @ws.workbook.drawings.index(@ws.drawing)) end diff --git a/test/drawing/tc_graphic_frame.rb b/test/drawing/tc_graphic_frame.rb index 30017d2b..fe0b901d 100644 --- a/test/drawing/tc_graphic_frame.rb +++ b/test/drawing/tc_graphic_frame.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestGraphicFrame < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet @chart = @ws.add_chart Axlsx::Chart @@ -20,7 +19,7 @@ class TestGraphicFrame < Test::Unit::TestCase def test_rId assert_equal(@frame.rId, "rId1") chart = @ws.add_chart Axlsx::Chart - assert_equal(chart.graphic_frame.rId, "rId2") + assert_equal(chart.graphic_frame.rId, "rId2") end end diff --git a/test/drawing/tc_hyperlink.rb b/test/drawing/tc_hyperlink.rb index a5564c96..251c7967 100644 --- a/test/drawing/tc_hyperlink.rb +++ b/test/drawing/tc_hyperlink.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestHyperlink < Test::Unit::TestCase @@ -66,5 +65,5 @@ class TestHyperlink < Test::Unit::TestCase assert_raise(ArgumentError) {@hyperlink.history = "bob"} assert_equal(@hyperlink.history, false ) end - + end diff --git a/test/drawing/tc_line_3d_chart.rb b/test/drawing/tc_line_3d_chart.rb index 5c4a3f26..22dd6158 100644 --- a/test/drawing/tc_line_3d_chart.rb +++ b/test/drawing/tc_line_3d_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestLine3DChart < Test::Unit::TestCase @@ -13,7 +12,7 @@ class TestLine3DChart < Test::Unit::TestCase def teardown end - def test_initialization + def test_initialization assert_equal(@chart.grouping, :standard, "grouping defualt incorrect") assert_equal(@chart.series_type, Axlsx::LineSeries, "series type incorrect") assert(@chart.catAxis.is_a?(Axlsx::CatAxis), "category axis not created") @@ -43,6 +42,6 @@ class TestLine3DChart < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_line_series.rb b/test/drawing/tc_line_series.rb index 602e7770..a2417549 100644 --- a/test/drawing/tc_line_series.rb +++ b/test/drawing/tc_line_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestLineSeries < Test::Unit::TestCase @@ -9,11 +8,11 @@ class TestLineSeries < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Line3DChart, :title => "fishery" @series = chart.add_series :data=>[0,1,2], :labels=>["zero", "one", "two"], :title=>"bob" end - + def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") + assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") end def test_data diff --git a/test/drawing/tc_marker.rb b/test/drawing/tc_marker.rb index 8bd9983f..05137ae7 100644 --- a/test/drawing/tc_marker.rb +++ b/test/drawing/tc_marker.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestMarker < Test::Unit::TestCase - def setup + def setup @marker = Axlsx::Marker.new end @@ -41,5 +40,5 @@ class TestMarker < Test::Unit::TestCase assert_equal(@marker.col, 5) assert_equal(@marker.row, 10) end - + end diff --git a/test/drawing/tc_one_cell_anchor.rb b/test/drawing/tc_one_cell_anchor.rb index cab22840..cba66112 100644 --- a/test/drawing/tc_one_cell_anchor.rb +++ b/test/drawing/tc_one_cell_anchor.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestOneCellAnchor < Test::Unit::TestCase - def setup + def setup @p = Axlsx::Package.new @ws = @p.workbook.add_worksheet @test_img = File.dirname(__FILE__) + "/../../examples/image1.jpeg" @@ -22,7 +21,7 @@ class TestOneCellAnchor < Test::Unit::TestCase end def test_from - assert(@anchor.from.is_a?(Axlsx::Marker)) + assert(@anchor.from.is_a?(Axlsx::Marker)) end def test_object @@ -36,13 +35,13 @@ class TestOneCellAnchor < Test::Unit::TestCase def test_width assert_raise(ArgumentError) { @anchor.width = "a" } assert_nothing_raised { @anchor.width = 600 } - assert_equal(@anchor.width, 600) + assert_equal(@anchor.width, 600) end def test_height assert_raise(ArgumentError) { @anchor.height = "a" } assert_nothing_raised { @anchor.height = 400 } - assert_equal(400, @anchor.height) + assert_equal(400, @anchor.height) end def test_ext @@ -63,5 +62,5 @@ class TestOneCellAnchor < Test::Unit::TestCase assert_equal(2, i.anchor.from.row) assert_equal(@test_img, i.image_src) end - + end diff --git a/test/drawing/tc_pic.rb b/test/drawing/tc_pic.rb index 9e4d62e6..83ed4e1d 100644 --- a/test/drawing/tc_pic.rb +++ b/test/drawing/tc_pic.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPic < Test::Unit::TestCase @@ -17,7 +16,7 @@ class TestPic < Test::Unit::TestCase assert_equal(@p.workbook.images.first, @image) assert_equal(@image.image_src, @test_img) end - + def test_hyperlink assert_equal(@image.hyperlink, nil) @image.hyperlink = "http://axlsx.blogspot.com" @@ -40,13 +39,13 @@ class TestPic < Test::Unit::TestCase def test_width assert_raise(ArgumentError) { @image.width = "a" } assert_nothing_raised { @image.width = 600 } - assert_equal(@image.width, 600) + assert_equal(@image.width, 600) end def test_height assert_raise(ArgumentError) { @image.height = "a" } assert_nothing_raised { @image.height = 600 } - assert_equal(600, @image.height) + assert_equal(600, @image.height) end def test_image_src @@ -62,7 +61,7 @@ class TestPic < Test::Unit::TestCase assert_nothing_raised { @image.descr = "test" } assert_equal(@image.descr, "test") end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) doc = Nokogiri::XML(@image.anchor.drawing.to_xml) @@ -72,6 +71,6 @@ class TestPic < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_picture_locking.rb b/test/drawing/tc_picture_locking.rb index 15b89e22..4c531d1a 100644 --- a/test/drawing/tc_picture_locking.rb +++ b/test/drawing/tc_picture_locking.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPictureLocking < Test::Unit::TestCase - def setup + def setup @item = Axlsx::PictureLocking.new end def teardown diff --git a/test/drawing/tc_pie_3D_chart.rb b/test/drawing/tc_pie_3D_chart.rb index e8a42962..76bf49c5 100644 --- a/test/drawing/tc_pie_3D_chart.rb +++ b/test/drawing/tc_pie_3D_chart.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPie3DChart < Test::Unit::TestCase @@ -18,7 +17,7 @@ class TestPie3DChart < Test::Unit::TestCase assert_equal(@chart.view3D.perspective, 30, "view 3d default perspective incorrect") assert_equal(@chart.series_type, Axlsx::PieSeries, "series type incorrect") end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) doc = Nokogiri::XML(@chart.to_xml) @@ -28,6 +27,6 @@ class TestPie3DChart < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - end - + end + end diff --git a/test/drawing/tc_pie_series.rb b/test/drawing/tc_pie_series.rb index 53799c72..66c8da81 100644 --- a/test/drawing/tc_pie_series.rb +++ b/test/drawing/tc_pie_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPieSeries < Test::Unit::TestCase @@ -9,12 +8,12 @@ class TestPieSeries < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Pie3DChart, :title => "fishery" @series = chart.add_series :data=>[0,1,2], :labels=>["zero", "one", "two"], :title=>"bob" end - + def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") assert_equal(@series.data, [0,1,2], "data option applied") - assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") - assert_equal(@series.explosion, nil, "series shape has been applied") + assert_equal(@series.labels, ["zero", "one","two"], "labels option applied") + assert_equal(@series.explosion, nil, "series shape has been applied") end def test_data diff --git a/test/drawing/tc_scaling.rb b/test/drawing/tc_scaling.rb index 3bf39c53..01e21a5d 100644 --- a/test/drawing/tc_scaling.rb +++ b/test/drawing/tc_scaling.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestScaling < Test::Unit::TestCase - def setup + def setup @scaling = Axlsx::Scaling.new end @@ -33,5 +32,5 @@ class TestScaling < Test::Unit::TestCase assert_raise(ArgumentError) { @scaling.min = 1} assert_nothing_raised {@scaling.min = 10.5} end - + end diff --git a/test/drawing/tc_scatter_chart.rb b/test/drawing/tc_scatter_chart.rb new file mode 100644 index 00000000..367cf56d --- /dev/null +++ b/test/drawing/tc_scatter_chart.rb @@ -0,0 +1,32 @@ +require 'tc_helper.rb' + +class TestScatterChart < Test::Unit::TestCase + def setup + @p = Axlsx::Package.new + ws = @p.workbook.add_worksheet + @row = ws.add_row ["one", 1, Time.now] + @chart = ws.add_chart Axlsx::ScatterChart, :title => "A Title" + end + + def teardown + end + + def test_initialization + assert_equal(@chart.scatterStyle, :lineMarker, "scatterStyle defualt incorrect") + assert_equal(@chart.series_type, Axlsx::ScatterSeries, "series type incorrect") + assert(@chart.xValAxis.is_a?(Axlsx::ValAxis), "independant value axis not created") + assert(@chart.yValAxis.is_a?(Axlsx::ValAxis), "dependant value axis not created") + end + + def test_to_xml + schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD)) + doc = Nokogiri::XML(@chart.to_xml) + errors = [] + schema.validate(doc).each do |error| + errors.push error + puts error.message + end + assert(errors.empty?, "error free validation") + end + +end diff --git a/test/drawing/tc_scatter_series.rb b/test/drawing/tc_scatter_series.rb new file mode 100644 index 00000000..a815c9e3 --- /dev/null +++ b/test/drawing/tc_scatter_series.rb @@ -0,0 +1,20 @@ +require 'tc_helper.rb' + +class TestScatterSeries < Test::Unit::TestCase + + def setup + p = Axlsx::Package.new + @ws = p.workbook.add_worksheet :name=>"hmmm" + chart = @ws.drawing.add_chart Axlsx::ScatterChart, :title => "Scatter Chart" + @series = chart.add_series :xData=>[1,2,4], :yData=>[1,3,9], :title=>"exponents" + end + + def test_initialize + assert_equal(@series.title.text, "exponents", "series title has been applied") + end + + def test_data + assert_equal(@series.xData, [1,2,4]) + assert_equal(@series.yData, [1,3,9]) + end +end diff --git a/test/drawing/tc_ser_axis.rb b/test/drawing/tc_ser_axis.rb index 3d744907..2f30ceb8 100644 --- a/test/drawing/tc_ser_axis.rb +++ b/test/drawing/tc_ser_axis.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSerAxis < Test::Unit::TestCase - def setup + def setup @axis = Axlsx::SerAxis.new 12345, 54321 end def teardown diff --git a/test/drawing/tc_series.rb b/test/drawing/tc_series.rb index cbbd93d4..6b83015a 100644 --- a/test/drawing/tc_series.rb +++ b/test/drawing/tc_series.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSeries < Test::Unit::TestCase @@ -9,16 +8,16 @@ class TestSeries < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Chart, :title => "fishery" @series = chart.add_series :title=>"bob" end - + def test_initialize assert_equal(@series.title.text, "bob", "series title has been applied") assert_equal(@series.order, @series.index, "order is index by default") - assert_equal(@series.index, @series.chart.series.index(@series), "index is applied") + assert_equal(@series.index, @series.chart.series.index(@series), "index is applied") end def test_order @series.order = 2 assert_equal(@series.order, 2) end - + end diff --git a/test/drawing/tc_series_title.rb b/test/drawing/tc_series_title.rb index f10370b7..90427e9c 100644 --- a/test/drawing/tc_series_title.rb +++ b/test/drawing/tc_series_title.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSeriesTitle < Test::Unit::TestCase - def setup + def setup @p = Axlsx::Package.new ws = @p.workbook.add_worksheet @row = ws.add_row ["one", 1, Time.now] @@ -21,7 +20,7 @@ class TestSeriesTitle < Test::Unit::TestCase def test_text assert_raise(ArgumentError, "text must be a string") { @title.text = 123 } @title.cell = @row.cells.first - @title.text = "bob" + @title.text = "bob" assert(@title.cell == nil, "setting title with text clears the cell") end @@ -30,5 +29,5 @@ class TestSeriesTitle < Test::Unit::TestCase @title.cell = @row.cells.first assert(@title.text == "one") end - + end diff --git a/test/drawing/tc_title.rb b/test/drawing/tc_title.rb index 7fb6044b..e53c260a 100644 --- a/test/drawing/tc_title.rb +++ b/test/drawing/tc_title.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTitle < Test::Unit::TestCase - def setup + def setup @p = Axlsx::Package.new ws = @p.workbook.add_worksheet @row = ws.add_row ["one", 1, Time.now] @@ -21,7 +20,7 @@ class TestTitle < Test::Unit::TestCase def test_text assert_raise(ArgumentError, "text must be a string") { @title.text = 123 } @title.cell = @row.cells.first - @title.text = "bob" + @title.text = "bob" assert(@title.cell == nil, "setting title with text clears the cell") end @@ -30,5 +29,5 @@ class TestTitle < Test::Unit::TestCase @title.cell = @row.cells.first assert(@title.text == "one") end - + end diff --git a/test/drawing/tc_two_cell_anchor.rb b/test/drawing/tc_two_cell_anchor.rb index 2d87b6a7..e22acce9 100644 --- a/test/drawing/tc_two_cell_anchor.rb +++ b/test/drawing/tc_two_cell_anchor.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTwoCellAnchor < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet row = @ws.add_row ["one", 1, Time.now] @@ -22,7 +21,7 @@ class TestTwoCellAnchor < Test::Unit::TestCase assert(@anchor.to.row == 10) end - + def test_options assert_raise(ArgumentError, 'invalid start_at') { @ws.add_chart Axlsx::Chart, :start_at=>[1] } assert_raise(ArgumentError, 'invalid end_at') { @ws.add_chart Axlsx::Chart, :start_at=>[1,2], :end_at => ["a", 4] } @@ -34,5 +33,5 @@ class TestTwoCellAnchor < Test::Unit::TestCase assert_equal(a.graphic_frame.anchor.to.col, 90) assert_equal(a.graphic_frame.anchor.to.row, 45) end - + end diff --git a/test/drawing/tc_val_axis.rb b/test/drawing/tc_val_axis.rb index 4bd8b883..f3f55421 100644 --- a/test/drawing/tc_val_axis.rb +++ b/test/drawing/tc_val_axis.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestValAxis < Test::Unit::TestCase - def setup + def setup @axis = Axlsx::ValAxis.new 12345, 54321 end def teardown diff --git a/test/drawing/tc_val_axis_data.rb b/test/drawing/tc_val_axis_data.rb index cb39176d..b49cc7c0 100644 --- a/test/drawing/tc_val_axis_data.rb +++ b/test/drawing/tc_val_axis_data.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestValAxisData < Test::Unit::TestCase @@ -9,7 +8,7 @@ class TestValAxisData < Test::Unit::TestCase chart = @ws.drawing.add_chart Axlsx::Line3DChart @series = chart.add_series :data=>[0,1,2] end - + def test_initialize assert(@series.data.is_a?Axlsx::SimpleTypedList) assert_equal(@series.data, [0,1,2]) diff --git a/test/drawing/tc_view_3D.rb b/test/drawing/tc_view_3D.rb index 40d8982f..e9626c75 100644 --- a/test/drawing/tc_view_3D.rb +++ b/test/drawing/tc_view_3D.rb @@ -1,14 +1,13 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestView3D < Test::Unit::TestCase - def setup + def setup @view = Axlsx::View3D.new end def teardown end - + def test_options v = Axlsx::View3D.new :rotX => 10, :rotY => 5, :hPercent => "30%", :depthPercent => "45%", :rAngAx => false, :perspective => 10 assert_equal(v.rotX, 10) @@ -18,7 +17,7 @@ class TestView3D < Test::Unit::TestCase assert_equal(v.rAngAx, false) assert_equal(v.perspective, 10) end - + def test_rotX assert_raise(ArgumentError) {@view.rotX = "bob"} assert_nothing_raised {@view.rotX = -90} @@ -44,12 +43,12 @@ class TestView3D < Test::Unit::TestCase assert_raise(ArgumentError) {@view.rAngAx = "bob"} assert_nothing_raised {@view.rAngAx = true} end - + def test_perspective assert_raise(ArgumentError) {@view.perspective = "bob"} assert_nothing_raised {@view.perspective = 30} end - + end diff --git a/test/rels/tc_relationship.rb b/test/rels/tc_relationship.rb index 60ac5021..98f21b0b 100644 --- a/test/rels/tc_relationship.rb +++ b/test/rels/tc_relationship.rb @@ -1,12 +1,11 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestRelationships < Test::Unit::TestCase - def setup + def setup + end + + def teardown end - - def teardown - end def test_type assert_raise(ArgumentError) { Axlsx::Relationship.new 'type', 'target' } diff --git a/test/rels/tc_relationships.rb b/test/rels/tc_relationships.rb index c6b4860d..9a76d1e5 100644 --- a/test/rels/tc_relationships.rb +++ b/test/rels/tc_relationships.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestRelationships < Test::Unit::TestCase @@ -21,7 +20,7 @@ class TestRelationships < Test::Unit::TestCase errors << error end - assert(errors.size == 0) + assert(errors.size == 0) end end diff --git a/test/stylesheet/tc_border.rb b/test/stylesheet/tc_border.rb index ba268e07..ae191dd0 100644 --- a/test/stylesheet/tc_border.rb +++ b/test/stylesheet/tc_border.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestBorder < Test::Unit::TestCase - def setup + def setup @b = Axlsx::Border.new end def teardown @@ -10,7 +9,7 @@ class TestBorder < Test::Unit::TestCase def test_initialiation assert_equal(@b.diagonalUp, nil) assert_equal(@b.diagonalDown, nil) - assert_equal(@b.outline, nil) + assert_equal(@b.outline, nil) assert(@b.prs.is_a?(Axlsx::SimpleTypedList)) end diff --git a/test/stylesheet/tc_border_pr.rb b/test/stylesheet/tc_border_pr.rb index 18388186..dc0fa9da 100644 --- a/test/stylesheet/tc_border_pr.rb +++ b/test/stylesheet/tc_border_pr.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestBorderPr < Test::Unit::TestCase - def setup + def setup @bpr = Axlsx::BorderPr.new end def teardown @@ -10,7 +9,7 @@ class TestBorderPr < Test::Unit::TestCase def test_initialiation assert_equal(@bpr.color, nil) assert_equal(@bpr.style, nil) - assert_equal(@bpr.name, nil) + assert_equal(@bpr.name, nil) end def test_color diff --git a/test/stylesheet/tc_cell_alignment.rb b/test/stylesheet/tc_cell_alignment.rb index 66f95518..6b8cd5cd 100644 --- a/test/stylesheet/tc_cell_alignment.rb +++ b/test/stylesheet/tc_cell_alignment.rb @@ -1,8 +1,7 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCellAlignment < Test::Unit::TestCase - def setup + def setup @item = Axlsx::CellAlignment.new end @@ -16,8 +15,8 @@ class TestCellAlignment < Test::Unit::TestCase assert_equal(@item.justifyLastLine, nil) assert_equal(@item.shrinkToFit, nil) assert_equal(@item.readingOrder, nil) - options = { :horizontal => :left, :vertical => :top, :textRotation => 3, - :wrapText => true, :indent => 2, :relativeIndent => 5, + options = { :horizontal => :left, :vertical => :top, :textRotation => 3, + :wrapText => true, :indent => 2, :relativeIndent => 5, :justifyLastLine => true, :shrinkToFit => true, :readingOrder => 2 } ca = Axlsx::CellAlignment.new options options.each do |key, value| diff --git a/test/stylesheet/tc_cell_protection.rb b/test/stylesheet/tc_cell_protection.rb index 665ec761..b2161fa6 100644 --- a/test/stylesheet/tc_cell_protection.rb +++ b/test/stylesheet/tc_cell_protection.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCellProtection < Test::Unit::TestCase - def setup + def setup @item = Axlsx::CellProtection.new end diff --git a/test/stylesheet/tc_cell_style.rb b/test/stylesheet/tc_cell_style.rb index 878c7aab..8700d8c3 100644 --- a/test/stylesheet/tc_cell_style.rb +++ b/test/stylesheet/tc_cell_style.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCellStyle < Test::Unit::TestCase - def setup + def setup @item = Axlsx::CellStyle.new end diff --git a/test/stylesheet/tc_color.rb b/test/stylesheet/tc_color.rb index 55f69448..affa2227 100644 --- a/test/stylesheet/tc_color.rb +++ b/test/stylesheet/tc_color.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestColor < Test::Unit::TestCase - def setup + def setup @item = Axlsx::Color.new end diff --git a/test/stylesheet/tc_fill.rb b/test/stylesheet/tc_fill.rb index 090a395b..9aadef29 100644 --- a/test/stylesheet/tc_fill.rb +++ b/test/stylesheet/tc_fill.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestFill < Test::Unit::TestCase diff --git a/test/stylesheet/tc_font.rb b/test/stylesheet/tc_font.rb index 141c285e..7b3da25a 100644 --- a/test/stylesheet/tc_font.rb +++ b/test/stylesheet/tc_font.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestFont < Test::Unit::TestCase @@ -29,7 +28,7 @@ class TestFont < Test::Unit::TestCase - # def name=(v) Axlsx::validate_string v; @name = v end + # def name=(v) Axlsx::validate_string v; @name = v end def test_name assert_raise(ArgumentError) { @item.name = 7 } assert_nothing_raised { @item.name = "bob" } @@ -49,7 +48,7 @@ class TestFont < Test::Unit::TestCase assert_equal(@item.family, 5) end - # def b=(v) Axlsx::validate_boolean v; @b = v end + # def b=(v) Axlsx::validate_boolean v; @b = v end def test_b assert_raise(ArgumentError) { @item.b = -7 } assert_nothing_raised { @item.b = true } @@ -62,7 +61,7 @@ class TestFont < Test::Unit::TestCase assert_nothing_raised { @item.i = true } assert_equal(@item.i, true) end - + # def u=(v) Axlsx::validate_boolean v; @u = v end def test_u assert_raise(ArgumentError) { @item.u = -7 } @@ -84,7 +83,7 @@ class TestFont < Test::Unit::TestCase assert_equal(@item.outline, true) end - # def shadow=(v) Axlsx::validate_boolean v; @shadow = v end + # def shadow=(v) Axlsx::validate_boolean v; @shadow = v end def test_shadow assert_raise(ArgumentError) { @item.shadow = -7 } assert_nothing_raised { @item.shadow = true } diff --git a/test/stylesheet/tc_gradient_fill.rb b/test/stylesheet/tc_gradient_fill.rb index f5c87771..c252a498 100644 --- a/test/stylesheet/tc_gradient_fill.rb +++ b/test/stylesheet/tc_gradient_fill.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestGradientFill < Test::Unit::TestCase diff --git a/test/stylesheet/tc_gradient_stop.rb b/test/stylesheet/tc_gradient_stop.rb index d7735231..f1cf5183 100644 --- a/test/stylesheet/tc_gradient_stop.rb +++ b/test/stylesheet/tc_gradient_stop.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestGradientStop < Test::Unit::TestCase diff --git a/test/stylesheet/tc_num_fmt.rb b/test/stylesheet/tc_num_fmt.rb index 281773ce..b5b73929 100644 --- a/test/stylesheet/tc_num_fmt.rb +++ b/test/stylesheet/tc_num_fmt.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestNumFmt < Test::Unit::TestCase diff --git a/test/stylesheet/tc_pattern_fill.rb b/test/stylesheet/tc_pattern_fill.rb index a9b6a113..65ed0dd1 100644 --- a/test/stylesheet/tc_pattern_fill.rb +++ b/test/stylesheet/tc_pattern_fill.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPatternFill < Test::Unit::TestCase @@ -17,13 +16,13 @@ class TestPatternFill < Test::Unit::TestCase assert_equal(@item.fgColor, nil) end - def test_bgColor + def test_bgColor assert_raise(ArgumentError) { @item.bgColor = -1.1 } assert_nothing_raised { @item.bgColor = Axlsx::Color.new } assert_equal(@item.bgColor.rgb, "FF000000") end - def test_fgColor + def test_fgColor assert_raise(ArgumentError) { @item.fgColor = -1.1 } assert_nothing_raised { @item.fgColor = Axlsx::Color.new } assert_equal(@item.fgColor.rgb, "FF000000") diff --git a/test/stylesheet/tc_styles.rb b/test/stylesheet/tc_styles.rb index 1b7365b2..9db37157 100644 --- a/test/stylesheet/tc_styles.rb +++ b/test/stylesheet/tc_styles.rb @@ -1,13 +1,12 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestStyles < Test::Unit::TestCase - def setup + def setup @styles = Axlsx::Styles.new end def teardown end - + def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@styles.to_xml) @@ -18,7 +17,14 @@ class TestStyles < Test::Unit::TestCase end assert(errors.size == 0) end + def test_add_style_border_hash + border_count = @styles.borders.size + s = @styles.add_style :border => {:style=>:thin, :color => "FFFF0000"} + assert_equal(@styles.borders.size, border_count + 1) + assert_equal(@styles.borders.last.prs.last.color.rgb, "FFFF0000") + assert_raise(ArgumentError) { @styles.add_style :border => {:color => "FFFF0000"} } + end def test_add_style fill_count = @styles.fills.size @@ -44,9 +50,9 @@ class TestStyles < Test::Unit::TestCase assert_equal(xf.alignment.horizontal, :left, "horizontal alignment applied") assert_equal(xf.protection.hidden, true, "hidden protection set") assert_equal(xf.protection.locked, true, "cell locking set") - assert_raise(ArgumentError, "should reject invalid borderId") { @styles.add_style :border => 2 } + assert_raise(ArgumentError, "should reject invalid borderId") { @styles.add_style :border => 2 } + - assert_equal(xf.applyProtection, 1, "protection applied") assert_equal(xf.applyBorder, true, "border applied") assert_equal(xf.applyNumberFormat, true, "number format applied") diff --git a/test/stylesheet/tc_table_style.rb b/test/stylesheet/tc_table_style.rb index 54bebb5b..8b219a93 100644 --- a/test/stylesheet/tc_table_style.rb +++ b/test/stylesheet/tc_table_style.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTableStyle < Test::Unit::TestCase @@ -16,7 +15,7 @@ class TestTableStyle < Test::Unit::TestCase assert_equal(@item.table, nil) end - def test_name + def test_name assert_raise(ArgumentError) { @item.name = -1.1 } assert_nothing_raised { @item.name = "lovely table style" } assert_equal(@item.name, "lovely table style") diff --git a/test/stylesheet/tc_table_style_element.rb b/test/stylesheet/tc_table_style_element.rb index 9cfa5204..2e6e763f 100644 --- a/test/stylesheet/tc_table_style_element.rb +++ b/test/stylesheet/tc_table_style_element.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTableStyleElement < Test::Unit::TestCase @@ -16,7 +15,7 @@ class TestTableStyleElement < Test::Unit::TestCase assert_equal(@item.dxfId, nil) end - def test_type + def test_type assert_raise(ArgumentError) { @item.type = -1.1 } assert_nothing_raised { @item.type = :blankRow } assert_equal(@item.type, :blankRow) diff --git a/test/stylesheet/tc_table_styles.rb b/test/stylesheet/tc_table_styles.rb index 825a18d8..cc8da9d5 100644 --- a/test/stylesheet/tc_table_styles.rb +++ b/test/stylesheet/tc_table_styles.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestTableStyles < Test::Unit::TestCase @@ -15,13 +14,13 @@ class TestTableStyles < Test::Unit::TestCase assert_equal(@item.defaultPivotStyle, "PivotStyleLight16") end - def test_defaultTableStyle + def test_defaultTableStyle assert_raise(ArgumentError) { @item.defaultTableStyle = -1.1 } assert_nothing_raised { @item.defaultTableStyle = "anyones guess" } assert_equal(@item.defaultTableStyle, "anyones guess") end - def test_defaultPivotStyle + def test_defaultPivotStyle assert_raise(ArgumentError) { @item.defaultPivotStyle = -1.1 } assert_nothing_raised { @item.defaultPivotStyle = "anyones guess" } assert_equal(@item.defaultPivotStyle, "anyones guess") diff --git a/test/stylesheet/tc_xf.rb b/test/stylesheet/tc_xf.rb index 9ca53562..fdaef970 100644 --- a/test/stylesheet/tc_xf.rb +++ b/test/stylesheet/tc_xf.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestXf < Test::Unit::TestCase @@ -28,91 +27,91 @@ class TestXf < Test::Unit::TestCase assert_equal(@item.applyProtection, nil) end - def test_alignment + def test_alignment assert_raise(ArgumentError) { @item.alignment = -1.1 } assert_nothing_raised { @item.alignment = Axlsx::CellAlignment.new } assert(@item.alignment.is_a?(Axlsx::CellAlignment)) end - def test_protection + def test_protection assert_raise(ArgumentError) { @item.protection = -1.1 } assert_nothing_raised { @item.protection = Axlsx::CellProtection.new } assert(@item.protection.is_a?(Axlsx::CellProtection)) end - def test_numFmtId + def test_numFmtId assert_raise(ArgumentError) { @item.numFmtId = -1.1 } assert_nothing_raised { @item.numFmtId = 0 } assert_equal(@item.numFmtId, 0) end - def test_fillId + def test_fillId assert_raise(ArgumentError) { @item.fillId = -1.1 } assert_nothing_raised { @item.fillId = 0 } assert_equal(@item.fillId, 0) end - def test_fontId + def test_fontId assert_raise(ArgumentError) { @item.fontId = -1.1 } assert_nothing_raised { @item.fontId = 0 } assert_equal(@item.fontId, 0) end - def test_borderId + def test_borderId assert_raise(ArgumentError) { @item.borderId = -1.1 } assert_nothing_raised { @item.borderId = 0 } assert_equal(@item.borderId, 0) end - def test_xfId + def test_xfId assert_raise(ArgumentError) { @item.xfId = -1.1 } assert_nothing_raised { @item.xfId = 0 } assert_equal(@item.xfId, 0) end - def test_quotePrefix + def test_quotePrefix assert_raise(ArgumentError) { @item.quotePrefix = -1.1 } assert_nothing_raised { @item.quotePrefix = false } assert_equal(@item.quotePrefix, false) end - def test_pivotButton + def test_pivotButton assert_raise(ArgumentError) { @item.pivotButton = -1.1 } assert_nothing_raised { @item.pivotButton = false } assert_equal(@item.pivotButton, false) end - def test_applyNumberFormat + def test_applyNumberFormat assert_raise(ArgumentError) { @item.applyNumberFormat = -1.1 } assert_nothing_raised { @item.applyNumberFormat = false } assert_equal(@item.applyNumberFormat, false) end - def test_applyFont + def test_applyFont assert_raise(ArgumentError) { @item.applyFont = -1.1 } assert_nothing_raised { @item.applyFont = false } assert_equal(@item.applyFont, false) end - def test_applyFill + def test_applyFill assert_raise(ArgumentError) { @item.applyFill = -1.1 } assert_nothing_raised { @item.applyFill = false } assert_equal(@item.applyFill, false) end - def test_applyBorder + def test_applyBorder assert_raise(ArgumentError) { @item.applyBorder = -1.1 } assert_nothing_raised { @item.applyBorder = false } assert_equal(@item.applyBorder, false) end - def test_applyAlignment + def test_applyAlignment assert_raise(ArgumentError) { @item.applyAlignment = -1.1 } assert_nothing_raised { @item.applyAlignment = false } assert_equal(@item.applyAlignment, false) end - def test_applyProtection + def test_applyProtection assert_raise(ArgumentError) { @item.applyProtection = -1.1 } assert_nothing_raised { @item.applyProtection = false } assert_equal(@item.applyProtection, false) diff --git a/test/tc_helper.rb b/test/tc_helper.rb new file mode 100644 index 00000000..e5606a09 --- /dev/null +++ b/test/tc_helper.rb @@ -0,0 +1,3 @@ +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" +require 'test/unit' +require "axlsx.rb" diff --git a/test/tc_package.rb b/test/tc_package.rb index a3f2bef5..86a00236 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPackage < Test::Unit::TestCase def setup diff --git a/test/util/tc_simple_typed_list.rb b/test/util/tc_simple_typed_list.rb index 6195153e..482ea8e3 100644 --- a/test/util/tc_simple_typed_list.rb +++ b/test/util/tc_simple_typed_list.rb @@ -1,11 +1,10 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSimpleTypedList < Test::Unit::TestCase - def setup + def setup @list = Axlsx::SimpleTypedList.new Fixnum end - def teardown + def teardown end def test_type_is_a_class_or_array_of_class @@ -15,18 +14,18 @@ class TestSimpleTypedList < Test::Unit::TestCase assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new "1" } assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new [Integer, "Class"] } end - + def test_indexed_based_assignment #should not allow nil assignment assert_raise(ArgumentError) { @list[0] = nil } assert_raise(ArgumentError) { @list[0] = "1" } assert_nothing_raised { @list[0] = 1 } end - + def test_concat_assignment assert_raise(ArgumentError) { @list << nil } assert_raise(ArgumentError) { @list << "1" } - assert_nothing_raised { @list << 1 } + assert_nothing_raised { @list << 1 } end def test_concat_should_return_index @@ -59,8 +58,8 @@ class TestSimpleTypedList < Test::Unit::TestCase assert_nothing_raised { @list.delete_at 3 } @list.unlock #ignore garbage - assert_nothing_raised { @list.delete 0 } - assert_nothing_raised { @list.delete 9 } + assert_nothing_raised { @list.delete 0 } + assert_nothing_raised { @list.delete 9 } end end diff --git a/test/util/tc_validators.rb b/test/util/tc_validators.rb index 09afcc27..d4f98d05 100644 --- a/test/util/tc_validators.rb +++ b/test/util/tc_validators.rb @@ -1,30 +1,29 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestValidators < Test::Unit::TestCase - def setup + def setup end def teardown end - + def test_validators #unsigned_int assert_nothing_raised { Axlsx.validate_unsigned_int 1 } assert_nothing_raised { Axlsx.validate_unsigned_int +1 } assert_raise(ArgumentError) { Axlsx.validate_unsigned_int -1 } assert_raise(ArgumentError) { Axlsx.validate_unsigned_int '1' } - + #int assert_nothing_raised { Axlsx.validate_int 1 } assert_nothing_raised { Axlsx.validate_int -1 } assert_raise(ArgumentError) { Axlsx.validate_int 'a' } assert_raise(ArgumentError) { Axlsx.validate_int Array } - + #boolean (as 0 or 1, :true, :false, true, false, or "true," "false") [0,1,:true, :false, true, false, "true", "false"].each do |v| assert_nothing_raised { Axlsx.validate_boolean 0 } end assert_raise(ArgumentError) { Axlsx.validate_boolean 2 } - + #string assert_nothing_raised { Axlsx.validate_string "1" } assert_raise(ArgumentError) { Axlsx.validate_string 2 } @@ -64,7 +63,7 @@ class TestValidators < Test::Unit::TestCase assert_raise(ArgumentError) { Axlsx.validate_content_type nil } assert_raise(ArgumentError) { Axlsx.validate_content_type "http://some.url" } assert_raise(ArgumentError) { Axlsx.validate_content_type false } - + #relationshipType assert_nothing_raised { Axlsx.validate_relationship_type Axlsx::WORKBOOK_R } assert_raise(ArgumentError) { Axlsx.validate_relationship_type nil } diff --git a/test/workbook/tc_shared_strings_table.rb b/test/workbook/tc_shared_strings_table.rb index b0e9de05..259f5970 100644 --- a/test/workbook/tc_shared_strings_table.rb +++ b/test/workbook/tc_shared_strings_table.rb @@ -1,14 +1,13 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestSharedStringsTable < Test::Unit::TestCase - def setup + def setup @p = Axlsx::Package.new :use_shared_strings=>true ws = @p.workbook.add_worksheet ws.add_row ['a', 1, 'b'] ws.add_row ['b', 1, 'c'] - ws.add_row ['c', 1, 'd'] + ws.add_row ['c', 1, 'd'] end def test_workbook_has_shared_strings @@ -25,7 +24,7 @@ class TestSharedStringsTable < Test::Unit::TestCase assert_equal(sst.unique_count, 4) end - def test_valid_document + def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml) errors = [] diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb index e068a6d0..af2a7889 100644 --- a/test/workbook/tc_workbook.rb +++ b/test/workbook/tc_workbook.rb @@ -1,15 +1,21 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestWorkbook < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new - @wb = p.workbook + @wb = p.workbook end def teardown end + def test_no_autowidth + assert_equal(@wb.use_autowidth, true) + assert_raise(ArgumentError) {@wb.use_autowidth = 0.1} + assert_nothing_raised {@wb.use_autowidth = false} + assert_equal(@wb.use_autowidth, false) + end + def test_date1904 assert_equal(Axlsx::Workbook.date1904, @wb.date1904) @wb.date1904 = :false @@ -18,6 +24,8 @@ class TestWorkbook < Test::Unit::TestCase assert_equal(Axlsx::Workbook.date1904, @wb.date1904) end + + def test_shared_strings assert_equal(@wb.use_shared_strings, nil) assert_raise(ArgumentError) {@wb.use_shared_strings = 'bpb'} diff --git a/test/workbook/worksheet/table/tc_table.rb b/test/workbook/worksheet/table/tc_table.rb new file mode 100644 index 00000000..fc29bf66 --- /dev/null +++ b/test/workbook/worksheet/table/tc_table.rb @@ -0,0 +1,71 @@ +require 'tc_helper.rb' + +class TestTable < Test::Unit::TestCase + def setup + p = Axlsx::Package.new + @ws = p.workbook.add_worksheet + 40.times do + @ws << ["aa","aa","aa","aa","aa","aa"] + end + end + + def test_initialization + assert(@ws.workbook.tables.empty?) + assert(@ws.tables.empty?) + + end + + def test_add_table + name = "test" + table = @ws.add_table("A1:D5", :name => name) + assert(table.is_a?(Axlsx::Table), "must create a table") + assert_equal(@ws.workbook.tables.last, table, "must be added to workbook table collection") + assert_equal(@ws.tables.last, table, "must be added to worksheet table collection") + assert_equal(table.name, name, "options for name are applied") + + end + + def test_charts + assert(@ws.drawing.charts.empty?) + chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1]) + assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") + chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"nancy", :start_at=>[1,5], :end_at=>[5,10]) + assert_equal(@ws.drawing.charts.last, chart, "add chart is returned") + end + + def test_pn + table = @ws.add_table("A1:D5") + assert_equal(@ws.tables.first.pn, "tables/table1.xml") + end + + def test_rId + table = @ws.add_table("A1:D5") + assert_equal(@ws.tables.first.rId, "rId1") + end + + def test_index + table = @ws.add_table("A1:D5") + assert_equal(@ws.tables.first.index, @ws.workbook.tables.index(@ws.tables.first)) + end + + def test_relationships + assert(@ws.relationships.empty?) + table = @ws.add_table("A1:D5") + assert_equal(@ws.relationships.size, 1, "adding a table adds a relationship") + table = @ws.add_table("F1:J5") + assert_equal(@ws.relationships.size, 2, "adding a table adds a relationship") + end + + def test_to_xml + table = @ws.add_table("A1:D5") + schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) + doc = Nokogiri::XML(table.to_xml) + errors = [] + schema.validate(doc).each do |error| + errors.push error + puts error.message + end + assert(errors.empty?, "error free validation") + end + +end diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index e111f7cd..7a33e9b9 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestCell < Test::Unit::TestCase diff --git a/test/workbook/worksheet/tc_date_time_converter.rb b/test/workbook/worksheet/tc_date_time_converter.rb index c78e51eb..adeae92b 100644 --- a/test/workbook/worksheet/tc_date_time_converter.rb +++ b/test/workbook/worksheet/tc_date_time_converter.rb @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestDateTimeConverter < Test::Unit::TestCase def setup diff --git a/test/workbook/worksheet/tc_page_margins.rb b/test/workbook/worksheet/tc_page_margins.rb index 3368129c..4c5e43fa 100644 --- a/test/workbook/worksheet/tc_page_margins.rb +++ b/test/workbook/worksheet/tc_page_margins.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestPageMargins < Test::Unit::TestCase @@ -8,7 +7,7 @@ class TestPageMargins < Test::Unit::TestCase ws = p.workbook.add_worksheet :name=>"hmmm" @pm = ws.page_margins end - + def test_initialize assert_equal(Axlsx::PageMargins::DEFAULT_LEFT_RIGHT, @pm.left) assert_equal(Axlsx::PageMargins::DEFAULT_LEFT_RIGHT, @pm.right) @@ -25,7 +24,7 @@ class TestPageMargins < Test::Unit::TestCase assert_equal(2, optioned.top) assert_equal(1, optioned.bottom) assert_equal(0.1, optioned.header) - assert_equal(0.1, optioned.footer) + assert_equal(0.1, optioned.footer) end @@ -48,7 +47,7 @@ class TestPageMargins < Test::Unit::TestCase assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.header) assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.footer) end - + def test_to_xml @pm.left = 1.1 @pm.right = 1.2 @@ -61,7 +60,7 @@ class TestPageMargins < Test::Unit::TestCase doc = Nokogiri::XML.parse(xml.to_xml) assert_equal(1, doc.xpath(".//pageMargins[@left=1.1][@right=1.2][@top=1.3][@bottom=1.4][@header=0.8][@footer=0.9]").size) end - + def test_left assert_raise(ArgumentError) { @pm.left = -1.2 } assert_nothing_raised { @pm.left = 1.5 } diff --git a/test/workbook/worksheet/tc_row.rb b/test/workbook/worksheet/tc_row.rb index 38c910c5..cabdcde7 100644 --- a/test/workbook/worksheet/tc_row.rb +++ b/test/workbook/worksheet/tc_row.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestRow < Test::Unit::TestCase @@ -8,7 +7,7 @@ class TestRow < Test::Unit::TestCase @ws = p.workbook.add_worksheet :name=>"hmmm" @row = @ws.add_row end - + def test_initialize assert(@row.cells.empty?, "no cells by default") assert_equal(@row.worksheet, @ws, "has a reference to the worksheet") @@ -25,7 +24,7 @@ class TestRow < Test::Unit::TestCase def test_style r = @ws.add_row([1,2,3,4,5]) r.style=1 - r.cells.each { |c| assert_equal(c.style,1) } + r.cells.each { |c| assert_equal(c.style,1) } end def test_index diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index 97845c92..6e1991e6 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -1,12 +1,12 @@ -require 'test/unit' -require 'axlsx.rb' +require 'tc_helper.rb' class TestWorksheet < Test::Unit::TestCase - def setup + def setup p = Axlsx::Package.new @ws = p.workbook.add_worksheet end + def test_pn assert_equal(@ws.pn, "worksheets/sheet1.xml") ws = @ws.workbook.add_worksheet @@ -14,7 +14,7 @@ class TestWorksheet < Test::Unit::TestCase end def test_page_margins - assert(@ws.page_margins.is_a? Axlsx::PageMargins) + assert(@ws.page_margins.is_a? Axlsx::PageMargins) end def test_page_margins_yeild @@ -24,9 +24,15 @@ class TestWorksheet < Test::Unit::TestCase end end + def test_no_autowidth + @ws.workbook.use_autowidth = false + @ws.add_row [1,2,3,4] + assert_equal(@ws.send(:auto_width, @ws.auto_fit_data[0]), Axlsx::FIXED_COL_WIDTH) + end + def test_initialization_options page_margins = {:left => 2, :right => 2, :bottom => 2, :top => 2, :header => 2, :footer => 2} - optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins) + optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :selected => true, :show_gridlines => false) assert_equal(optioned.page_margins.left, page_margins[:left]) assert_equal(optioned.page_margins.right, page_margins[:right]) assert_equal(optioned.page_margins.top, page_margins[:top]) @@ -34,6 +40,21 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(optioned.page_margins.header, page_margins[:header]) assert_equal(optioned.page_margins.footer, page_margins[:footer]) assert_equal(optioned.name, 'bob') + assert_equal(optioned.selected, true) + assert_equal(optioned.show_gridlines, false) + end + + + def test_use_gridlines + assert_raise(ArgumentError) { @ws.show_gridlines = -1.1 } + assert_nothing_raised { @ws.show_gridlines = false } + assert_equal(@ws.show_gridlines, false) + end + + def test_selected + assert_raise(ArgumentError) { @ws.selected = -1.1 } + assert_nothing_raised { @ws.selected = true } + assert_equal(@ws.selected, true) end def test_rels_pn @@ -47,7 +68,7 @@ class TestWorksheet < Test::Unit::TestCase ws = @ws.workbook.add_worksheet assert_equal(ws.rId, "rId2") end - + def test_index assert_equal(@ws.index, @ws.workbook.worksheets.index(@ws)) end @@ -68,7 +89,7 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(@ws.rows[1],last_row) assert_equal(range.size, 6) assert_equal(range.first, @ws.rows.first.cells.first) - assert_equal(range.last, @ws.rows.last.cells.last) + assert_equal(range.last, @ws.rows.last.cells.last) end def test_add_row @@ -94,7 +115,7 @@ class TestWorksheet < Test::Unit::TestCase @ws.add_row [1,2,3,4] @ws.add_row [1,2,3,4] @ws.col_style( (1..2), 1, :row_offset=>1) - @ws.rows[(1..-1)].each do | r | + @ws.rows[(1..-1)].each do | r | assert_equal(r.cells[1].style, 1) assert_equal(r.cells[2].style, 1) end @@ -115,7 +136,7 @@ class TestWorksheet < Test::Unit::TestCase @ws.add_row [1,2,3,4] @ws.add_row [1,2,3,4] c = @ws.cols[1] - assert_equal(c.size, 4) + assert_equal(c.size, 4) assert_equal(c[0].value, 2) end @@ -125,13 +146,13 @@ class TestWorksheet < Test::Unit::TestCase @ws.add_row [1,2,3,4] @ws.add_row [1,2,3,4] @ws.row_style 1, 1, :col_offset=>1 - @ws.rows[1].cells[(1..-1)].each do | c | + @ws.rows[1].cells[(1..-1)].each do | c | assert_equal(c.style, 1) end assert_equal(@ws.rows[1].cells[0].style, 0) assert_equal(@ws.rows[2].cells[1].style, 0) end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@ws.to_xml) @@ -153,7 +174,7 @@ class TestWorksheet < Test::Unit::TestCase puts error.message end assert(errors.empty?, "error free validation") - + end def test_relationships @@ -164,7 +185,7 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(@ws.relationships.size, 1, "multiple charts still only result in one relationship") end - + def test_name_unique assert_raise(ArgumentError, "worksheet name must be unique") { n = @ws.name; @ws.workbook.add_worksheet(:name=> @ws) } end @@ -205,7 +226,7 @@ class TestWorksheet < Test::Unit::TestCase @ws.add_row ["but Im Short!"], :widths=> [14.8] assert_equal(@ws.send(:auto_width, @ws.auto_fit_data[0]), 14.8) end - + def test_fixed_width_to_auto @ws.add_row ["hey, I'm like really long and stuff so I think you will merge me."] @ws.merge_cells "A1:C1" @@ -240,7 +261,7 @@ class TestWorksheet < Test::Unit::TestCase def test_merge_cells assert(@ws.merged_cells.is_a?(Array)) - assert_equal(@ws.merged_cells.size, 0) + assert_equal(@ws.merged_cells.size, 0) @ws.add_row [1,2,3] @ws.add_row [4,5,6] @ws.add_row [7,8,9] |
