Skip to content

Commit

Permalink
improve time and other type support
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Jan 29, 2018
1 parent fe9e052 commit f38bca8
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 289 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
166 changes: 0 additions & 166 deletions LICENSE.LGPL

This file was deleted.

33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<a href='https://ko-fi.com/A5071NK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>
# RODF
<a href='https://ko-fi.com/A5071NK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>

This is a library for writing to ODF output from Ruby. It mainly focuses creating ODS spreadsheets.

As well as writing ODS spreadsheets, this library also can write ODT text documents but it is undocumented and will require knowledge of the ODF spec. It currently does not support ODP Slide shows.

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:

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
68 changes: 50 additions & 18 deletions lib/rodf/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with rODF. If not, see <http://www.gnu.org/licenses/>.

require 'date'

require 'builder'

require 'rodf/container'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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
Loading

0 comments on commit f38bca8

Please sign in to comment.