diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a664ab..0ef2305 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +* **v1.0.1** + * Improvements to Date/Time handling (Issue #19) + * Ensure tests for all available cell types * **v1.0.0** Rename main module ODF to RODF, Fix cell types * **v0.3.7** Add Rails 5 support to gemspec * **v0.3.6** Resulting ODF files as bitstrings diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..35fd282 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2008 Thiago Arrais + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/LICENSE.LGPL b/LICENSE.LGPL deleted file mode 100644 index 3f7b8b1..0000000 --- a/LICENSE.LGPL +++ /dev/null @@ -1,166 +0,0 @@ - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. - - 0. Additional Definitions. - - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. - - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. - - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. - - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". - - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. - - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. - - 1. Exception to Section 3 of the GNU GPL. - - You may convey a covered work under sections 3 and 4 of this License -without being bound by section 3 of the GNU GPL. - - 2. Conveying Modified Versions. - - If you modify a copy of the Library, and, in your modifications, a -facility refers to a function or data to be supplied by an Application -that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified -version: - - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. - - 3. Object Code Incorporating Material from Library Header Files. - - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object -code under terms of your choice, provided that, if the incorporated -material is not limited to numerical parameters, data structure -layouts and accessors, or small macros, inline functions and templates -(ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license - document. - - 4. Combined Works. - - You may convey a Combined Work under terms of your choice that, -taken together, effectively do not restrict modification of the -portions of the Library contained in the Combined Work and reverse -engineering for debugging such modifications, if you also do each of -the following: - - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the -Library side by side in a single library together with other library -facilities that are not Applications and are not covered by this -License, and convey such a combined library under terms of your -choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. - - 6. Revised Versions of the GNU Lesser General Public License. - - The Free Software Foundation may publish revised and/or new versions -of the GNU Lesser General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - - Each version is given a distinguishing version number. If the -Library as you received it specifies that a certain numbered version -of the GNU Lesser General Public License "or any later version" -applies to it, you have the option of following the terms and -conditions either of that published version or of any later version -published by the Free Software Foundation. If the Library as you -received it does not specify a version number of the GNU Lesser -General Public License, you may choose any version of the GNU Lesser -General Public License ever published by the Free Software Foundation. - - If the Library as you received it specifies that a proxy can decide -whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is -permanent authorization for you to choose that version for the -Library. - diff --git a/README.md b/README.md index e8c4072..e1c1f47 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -Buy Me a Coffee # RODF +Buy Me a Coffee This is a library for writing to ODF output from Ruby. It mainly focuses creating ODS spreadsheets. @@ -7,18 +7,18 @@ As well as writing ODS spreadsheets, this library also can write ODT text docume This is NOT an ODF reading library. -### v1.0.0 Breaking Changes -The main module `ODF` has changed to `RODF` - -`require 'odf/spreadsheet'` must be changed to `require 'rodf'` - -### Install +## Install ``` gem install rodf ``` -### How do I use it? +#### v1.0.0 Breaking Changes +The main module is now `RODF`. It previously was `ODF`. + +As such the require has been simplified as `require 'rodf'`. It previously was `require 'odf/spreadsheet'`. + +## How do I use it? rODF works pretty much like Builder, but with ODF-aware constructs. Try this: @@ -92,7 +92,7 @@ RODF::Spreadsheet.file("my-spreadsheet.ods") do end ``` -### Procedural style +## Procedural style The declarative style shown above is just syntatic sugar. A more procedural style can also be used. Like so: @@ -128,7 +128,18 @@ end ss.write_to 'my-spreadsheet.ods' ``` -### Style List +## Columns Types + +Available columns types are: + +- :string +- :float +- :date +- :time +- :currency +- :percentage + +## Style List ```ruby property :text, 'font-weight' => :bold, #options are :bold, :thin @@ -162,7 +173,7 @@ property :table, ``` -### Credits +## Credits Created by [@thiagoarrais](https://github.com/thiagoarrais) Currently maintained by [@westonganger](https://github.com/westonganger) to support simplified ODS spreadsheet making in the [spreadsheet_architect](https://github.com/westonganger/spreadsheet_architect) gem diff --git a/lib/rodf/cell.rb b/lib/rodf/cell.rb index 26e134b..9f7c755 100644 --- a/lib/rodf/cell.rb +++ b/lib/rodf/cell.rb @@ -15,6 +15,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with rODF. If not, see . +require 'date' + require 'builder' require 'rodf/container' @@ -44,22 +46,26 @@ def initialize(value=nil, opts={}) @url = opts[:url] if !@type - if value.is_a?(Numeric) + if @value.is_a?(Numeric) @type = :float elsif @value.respond_to?(:strftime) - @type = :date - @value = value.to_s - - #if value.is_a?(Date) - # @value = value.strftime("%Y-%m-%d") - #else - # @value = value.strftime("%Y%m%dT%H%M%S") - #end + if @value.is_a?(Date) + @type = :date + else + @type = :time + end else @type = :string - @value = value.to_s + @value = @value.to_s end end + + case @type.to_s + when 'date' + @value = @value.strftime("%Y-%m-%d") if @value.respond_to?(:strftime) + when 'time' + @value = @value.strftime("%Y%m%dT%H%M%S") if @value.respond_to?(:strftime) + end end @elem_attrs = make_element_attributes(@type, @value, opts) @@ -93,14 +99,39 @@ def contains_string? def make_element_attributes(type, value, opts) attrs = {} - attrs['office:value-type'] = type if type == :string || !empty?(value) || !opts[:formula].nil? - attrs['office:date-value'] = value if type == :date && !empty?(value) - attrs['office:value'] = value if type == :float && !empty?(value) - attrs['table:formula'] = opts[:formula] unless opts[:formula].nil? - attrs['table:style-name'] = opts[:style] unless opts[:style].nil? - attrs['table:number-columns-spanned'] = opts[:span] unless opts[:span].nil? - attrs['table:number-matrix-columns-spanned'] = - attrs['table:number-matrix-rows-spanned'] = 1 if opts[:matrix_formula] + + if !empty?(value) || !opts[:formula].nil? || type == :string + attrs['office:value-type'] = type + end + + if type != :string && !empty?(value) + case type + when :date + attrs['office:date-value'] = value + when :time + attrs['office:time-value'] = value + else ### :float, :percentage, :currency + attrs['office:value'] = value + end + end + + unless opts[:formula].nil? + attrs['table:formula'] = opts[:formula] + end + + unless opts[:style].nil? + attrs['table:style-name'] = opts[:style] + end + + unless opts[:span].nil? + attrs['table:number-columns-spanned'] = opts[:span] + end + + if opts[:matrix_formula] + attrs['table:number-matrix-columns-spanned'] = 1 + attrs['table:number-matrix-rows-spanned'] = 1 + end + return attrs end @@ -121,5 +152,6 @@ def empty?(value) value.respond_to?(:empty?) ? value.empty? : value.nil? #respond_to?(:empty?) ? (value.empty? || value =~ /\A[[:space:]]*\z/) : value.nil? end + end end diff --git a/spec/cell_spec.rb b/spec/cell_spec.rb index 187a135..5956c48 100644 --- a/spec/cell_spec.rb +++ b/spec/cell_spec.rb @@ -18,10 +18,16 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') require 'date' +require 'time' require 'rodf/cell' +CELL_TYPES = [:float, :date, :time, :percentage, :currency].freeze +TIME_FORMAT = "%Y%m%dT%H%M%S".freeze +DATE_FORMAT = "%Y-%m-%d".freeze + describe RODF::Cell do + ### GENERAL & STRING it "should hold text content in a paragraph tag" do output = RODF::Cell.new('Test').xml output.should have_tag('//table:table-cell/*') @@ -29,50 +35,55 @@ Hpricot(output).at('text:p').innerHTML.should == 'Test' end - it "should allow value types to be specified" do - output = RODF::Cell.new(34.2, type: :float).xml - Hpricot(output).at('table:table-cell')['office:value-type'].should=='float' - end - - it "should place strings in a paragraph tag and floats in value attribute" do - output = RODF::Cell.new('Test').xml - output.should have_tag('//text:p') - Hpricot(output).at('text:p').innerHTML.should == 'Test' + it "should contain paragraph" do + c = RODF::Cell.new + c.paragraph "testing" + output = c.xml - output = RODF::Cell.new(47, type: :float).xml - output.should_not have_tag('//table:table-cell/*') - Hpricot(output).at('table:table-cell')['office:value'].should == '47' + output.should have_tag("//table:table-cell/*", count: 1) + output.should have_tag("//text:p") - output = RODF::Cell.new(34.2, type: :string).xml - output.should have_tag('//text:p') - Hpricot(output).at('text:p').innerHTML.should == '34.2' + Hpricot(output).at('text:p').innerHTML.should == 'testing' end - it "should accept formulas" do - output = RODF::Cell.new(type: :float, - formula: "oooc:=SUM([.A1:.A4])").xml + it "should be able to hold multiple paragraphs" do + output = RODF::Cell.create do |c| + c.paragraph "first" + c.paragraph "second" + end - elem = Hpricot(output).at('table:table-cell') - elem['office:value-type'].should == 'float' - elem['table:formula'].should == 'oooc:=SUM([.A1:.A4])' + output.should have_tag("//table:table-cell/*", count: 2) + output.should have_tag("//text:p") + + ps = Hpricot(output).search('text:p') + ps[0].innerHTML.should == 'first' + ps[1].innerHTML.should == 'second' end - it "should accept matrix formulas" do - output = RODF::Cell.new(type: :float, matrix_formula: true, - formula: "oooc:=SUM([.A1:.A4])").xml + it "should not render value type for non-string nil values" do + Hpricot(RODF::Cell.new(nil, type: :string).xml).at('table:table-cell').innerHTML.should == '' - elem = Hpricot(output).at('table:table-cell') - elem['table:number-matrix-columns-spanned'].should == '1' - elem['table:number-matrix-rows-spanned'].should == '1' + CELL_TYPES.each do |t| + cell = Hpricot(RODF::Cell.new(nil, type: t).xml).at('table:table-cell') + cell.innerHTML.should == '' + cell['office:value'].should be_nil + cell['office:date-value'].should be_nil + cell['office:value-type'].should be_nil + end end - it "should not make a matrix formula when asked not too" do - output = RODF::Cell.new(type: :float, matrix_formula: false, - formula: "oooc:=SUM([.A1:.A4])").xml + it "should accept parameterless blocks" do + output = RODF::Cell.create do + paragraph "first" + paragraph "second" + end - elem = Hpricot(output).at('table:table-cell') - elem['table:number-matrix-columns-spanned'].should be_nil - elem['table:number-matrix-rows-spanned'].should be_nil + output.should have_tag("//table:table-cell/*", count: 2) + output.should have_tag("//text:p") + + ps = Hpricot(output).search('text:p') + ps[0].innerHTML.should == 'first' + ps[1].innerHTML.should == 'second' end it "should not have an empty paragraph" do @@ -81,19 +92,6 @@ end end - it "should allow an style to be specified in the constructor" do - cell = RODF::Cell.new 45.8, type: :float, style: 'left-column-cell' - Hpricot(cell.xml).at('table:table-cell')['table:style-name']. - should == 'left-column-cell' - end - - it "should allow and style to be specified through a method call" do - cell = RODF::Cell.new 45.8, type: :float - cell.style = 'left-column-cell' - Hpricot(cell.xml).at('table:table-cell')['table:style-name']. - should == 'left-column-cell' - end - it "should span multiple cells when asked to" do cell = RODF::Cell.new 'Spreadsheet title', span: 4 doc = Hpricot(cell.xml) @@ -117,68 +115,97 @@ cell.xml.should_not have_tag('text:a') end - it "should have the date set correctly" do - cell = Hpricot(RODF::Cell.new(Date.parse('15 Apr 2010'), type: :date).xml). - at('table:table-cell') - cell['office:value-type'].should == 'date' - cell['office:date-value'].should == '2010-04-15' - cell['office:value'].should be_nil - end - - it "should also accept strings as date values" do - Hpricot(RODF::Cell.new(Date.parse('16 Apr 2010'), type: :date).xml). - at('table:table-cell')['office:date-value'] = '2010-04-16' + ### FLOAT + it "should allow value types to be specified" do + output = RODF::Cell.new(34.2, type: :float).xml + Hpricot(output).at('table:table-cell')['office:value-type'].should=='float' end - it "should contain paragraph" do - c = RODF::Cell.new - c.paragraph "testing" - output = c.xml + it "should place strings in a paragraph tag and floats in value attribute" do + output = RODF::Cell.new('Test').xml + output.should have_tag('//text:p') + Hpricot(output).at('text:p').innerHTML.should == 'Test' - output.should have_tag("//table:table-cell/*", count: 1) - output.should have_tag("//text:p") + output = RODF::Cell.new(47, type: :float).xml + output.should_not have_tag('//table:table-cell/*') + Hpricot(output).at('table:table-cell')['office:value'].should == '47' - Hpricot(output).at('text:p').innerHTML.should == 'testing' + output = RODF::Cell.new(34.2, type: :string).xml + output.should have_tag('//text:p') + Hpricot(output).at('text:p').innerHTML.should == '34.2' end - it "should be able to hold multiple paragraphs" do - output = RODF::Cell.create do |c| - c.paragraph "first" - c.paragraph "second" - end + it "should accept formulas" do + output = RODF::Cell.new(type: :float, formula: "oooc:=SUM([.A1:.A4])").xml - output.should have_tag("//table:table-cell/*", count: 2) - output.should have_tag("//text:p") + elem = Hpricot(output).at('table:table-cell') + elem['office:value-type'].should == 'float' + elem['table:formula'].should == 'oooc:=SUM([.A1:.A4])' + end - ps = Hpricot(output).search('text:p') - ps[0].innerHTML.should == 'first' - ps[1].innerHTML.should == 'second' + it "should accept matrix formulas" do + output = RODF::Cell.new(type: :float, matrix_formula: true, formula: "oooc:=SUM([.A1:.A4])").xml + + elem = Hpricot(output).at('table:table-cell') + elem['table:number-matrix-columns-spanned'].should == '1' + elem['table:number-matrix-rows-spanned'].should == '1' end - it "should not render value type for non-string nil values" do - Hpricot(RODF::Cell.new(nil, type: :string).xml). - at('table:table-cell').innerHTML.should == '' + it "should not make a matrix formula when asked not too" do + output = RODF::Cell.new(type: :float, matrix_formula: false, formula: "oooc:=SUM([.A1:.A4])").xml - [:float, :date].each do |t| - cell = Hpricot(RODF::Cell.new(nil, type: t).xml).at('table:table-cell') - cell.innerHTML.should == '' - cell['office:value'].should be_nil - cell['office:date-value'].should be_nil - cell['office:value-type'].should be_nil - end + elem = Hpricot(output).at('table:table-cell') + elem['table:number-matrix-columns-spanned'].should be_nil + elem['table:number-matrix-rows-spanned'].should be_nil end - it "should accept parameterless blocks" do - output = RODF::Cell.create do - paragraph "first" - paragraph "second" - end + it "should allow an style to be specified in the constructor" do + cell = RODF::Cell.new 45.8, type: :float, style: 'left-column-cell' + Hpricot(cell.xml).at('table:table-cell')['table:style-name'].should == 'left-column-cell' + end - output.should have_tag("//table:table-cell/*", count: 2) - output.should have_tag("//text:p") + it "should allow and style to be specified through a method call" do + cell = RODF::Cell.new 45.8, type: :float + cell.style = 'left-column-cell' + Hpricot(cell.xml).at('table:table-cell')['table:style-name'].should == 'left-column-cell' + end - ps = Hpricot(output).search('text:p') - ps[0].innerHTML.should == 'first' - ps[1].innerHTML.should == 'second' + ### DATE + it "should have the date set correctly" do + date = Date.parse('2010-04-15') + cell = Hpricot(RODF::Cell.new(date, type: :date).xml).at('table:table-cell') + cell['office:value-type'].should == 'date' + cell['office:date-value'].should == date.strftime(DATE_FORMAT) + cell['office:value'].should be_nil + cell['office:time-value'].should be_nil + end + + ### TIME + it "should have the time set correctly" do + time = Time.now + cell = Hpricot(RODF::Cell.new(time, type: :time).xml).at('table:table-cell') + cell['office:value-type'].should == 'time' + cell['office:time-value'].should == time.strftime(TIME_FORMAT) + cell['office:value'].should be_nil + cell['office:date-value'].should be_nil + end + + ### PERCENTAGE + it "should have the percentage set correctly" do + number = 0.45 + rodf_cell = RODF::Cell.new(number, type: :percentage) + output = Hpricot(rodf_cell.xml) + cell = output.at('table:table-cell') + cell['office:value-type'].should == 'percentage' + cell['office:value'].should == number.to_s + end + + ### CURRENCY + it "should have the currency set correctly" do + number = 1234.5678 + output = Hpricot(RODF::Cell.new(number, type: :currency).xml) + cell = output.at('table:table-cell') + cell['office:value-type'].should == 'currency' + cell['office:value'].should == number.to_s end end