osheet 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- osheet (0.1.0)
4
+ osheet (0.2.0)
5
5
  enumeration (~> 1.1.0)
6
- xmlss (~> 0.0.2)
6
+ xmlss (~> 0.1.0)
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
@@ -19,7 +19,8 @@ GEM
19
19
  kelredd-useful (~> 0.4.0)
20
20
  leftright (~> 0.9.0)
21
21
  shoulda (~> 2.11)
22
- xmlss (0.0.2)
22
+ xmlss (0.1.0)
23
+ enumeration (~> 1.1.0)
23
24
  nokogiri (~> 1.4.0)
24
25
 
25
26
  PLATFORMS
@@ -30,4 +31,4 @@ DEPENDENCIES
30
31
  enumeration (~> 1.1.0)
31
32
  osheet!
32
33
  test-belt (= 0.2.1)
33
- xmlss (~> 0.0.2)
34
+ xmlss (~> 0.1.0)
data/README.rdoc CHANGED
@@ -78,6 +78,10 @@ Pronounced 'oh-sheeeeeet!' - this gem is a DSL wrapper to the spreadsheet gem th
78
78
 
79
79
  file = wb.to_file('stats') # => <spreadsheet written to ./stats.xls>
80
80
 
81
+ == Examples
82
+
83
+ I've add a few examples to ./examples. Please refer to these for examples on basic usage, using templates, formatting data, and styling data.
84
+
81
85
  == API
82
86
 
83
87
  These classes define how a spreadsheet is constructed.
@@ -0,0 +1,84 @@
1
+ # To run:
2
+ # $ bundle install
3
+ # $ bundle exec ruby examples/basic_with_templates.rb
4
+ # $ open examples/basic_with_templates.xls
5
+
6
+ require 'rubygems'
7
+ require 'osheet'
8
+
9
+ fields = ['Sex', 'Age', 'Height', 'Weight']
10
+ data = {
11
+ 'Tom' => ['M', 52, "6'2\"", '220 lbs.'],
12
+ 'Dick' => ['M', 33, "6'5\"", '243 lbs.'],
13
+ 'Sally' => ['F', 29, "5'3\"", '132 lbs.']
14
+ }
15
+
16
+ # this will dump the above data to a single-sheet workbook w/ no styles
17
+
18
+
19
+ Osheet::Workbook.new {
20
+ title "basic"
21
+
22
+ template(:column, :data) { |field, index|
23
+ width 80
24
+ meta(
25
+ :label => field.to_s,
26
+ :index => index
27
+ )
28
+ }
29
+
30
+ template(:row, :title) {
31
+ cell {
32
+ colspan columns.count
33
+ data worksheet.name
34
+ }
35
+ }
36
+
37
+ template(:row, :empty) {
38
+ cell {
39
+ colspan columns.count
40
+ data ''
41
+ }
42
+ }
43
+
44
+ template(:row, :header) {
45
+ columns.each do |column|
46
+ cell {
47
+ data column.meta[:label]
48
+ }
49
+ end
50
+ }
51
+
52
+ template(:row, :data) { |name, stats|
53
+ cell {
54
+ data name
55
+ }
56
+ stats.each do |stat|
57
+ cell {
58
+ data stat
59
+ }
60
+ end
61
+ }
62
+
63
+ worksheet {
64
+ name "Stats: #{fields.join(', ')}"
65
+
66
+ column {
67
+ width 200
68
+ meta(
69
+ :label => "Name"
70
+ )
71
+ }
72
+ fields.each_with_index do |f, i|
73
+ column :data, f, i
74
+ end
75
+
76
+ row :title
77
+ row :empty
78
+ row :header
79
+
80
+ data.each do |name, stats|
81
+ row :data, name, stats
82
+ end
83
+ }
84
+ }.to_file('examples/basic_with_templates.xls')
@@ -0,0 +1,241 @@
1
+ # To run:
2
+ # $ bundle install
3
+ # $ bundle exec ruby examples/styles.rb
4
+ # $ open examples/styles.xls
5
+
6
+ require 'rubygems'
7
+ require 'osheet'
8
+
9
+ Osheet::Workbook.new {
10
+ title "styles"
11
+ template(:cell, :styled) { |style, attribute|
12
+ data attribute == :wrap ? (attribute.to_s+' ')*20 : attribute.to_s
13
+ style_class "#{style} #{attribute}"
14
+ }
15
+
16
+ # align styles
17
+ style('.align.left') { align :left }
18
+ style('.align.center') { align :center }
19
+ style('.align.right') { align :right }
20
+ style('.align.top') { align :top }
21
+ style('.align.middle') { align :middle }
22
+ style('.align.bottom') { align :bottom }
23
+ style('.align.wrap') { align :wrap }
24
+ style('.align.rotA') { align 90 }
25
+ style('.align.rotB') { align -90 }
26
+ style('.align.rotC') { align 45 }
27
+
28
+ worksheet {
29
+ name "align"
30
+
31
+ (0..3).each do
32
+ column {
33
+ width 100
34
+ }
35
+ end
36
+
37
+ { 'Horizontal alignment' => [:left, :center, :right],
38
+ 'Vertical alignment' => [:top, :middle, :bottom],
39
+ 'Wrap text' => [:wrap],
40
+ 'Rotate text' => [:rotA, :rotB, :rotC]
41
+ }.each do |k,v|
42
+ row {
43
+ height 50
44
+ v.each {|a| cell(:styled, 'align', a) }
45
+ }
46
+ end
47
+ }
48
+
49
+
50
+
51
+ # font styles
52
+ style('.font.underline') { font :underline }
53
+ style('.font.double_underline') { font :double_underline }
54
+ style('.font.accounting_underline') { font :accounting_underline }
55
+ style('.font.double_accounting_underline') { font :double_accounting_underline }
56
+ style('.font.subscript') { font :subscript }
57
+ style('.font.superscript') { font :superscript }
58
+ style('.font.strikethrough') { font :strikethrough }
59
+ style('.font.shadow') { font :shadow }
60
+ style('.font.bold') { font :bold }
61
+ style('.font.italic') { font :italic }
62
+ style('.font.sizeA') { font 6 }
63
+ style('.font.sizeB') { font 14 }
64
+ style('.font.colorA') { font '#FF0000' }
65
+ style('.font.colorB') { font '#00FF00' }
66
+
67
+ worksheet {
68
+ name "font"
69
+
70
+ (0..5).each do
71
+ column {
72
+ width 100
73
+ }
74
+ end
75
+
76
+ row {
77
+ [:underline, :double_underline, :accounting_underline, :double_accounting_underline].each do |a|
78
+ cell {
79
+ data a.to_s
80
+ style_class "font #{a}"
81
+ }
82
+ end
83
+ }
84
+ row {
85
+ [:subscript, :superscript, :strikethrough, :shadow].each do |a|
86
+ cell {
87
+ data a.to_s
88
+ style_class "font #{a}"
89
+ }
90
+ end
91
+ }
92
+ row {
93
+ [:bold, :italic].each do |a|
94
+ cell {
95
+ data a.to_s
96
+ style_class "font #{a}"
97
+ }
98
+ end
99
+ }
100
+ row {
101
+ [:sizeA, :sizeB].each do |a|
102
+ cell {
103
+ data a.to_s
104
+ style_class "font #{a}"
105
+ }
106
+ end
107
+ }
108
+ row {
109
+ [:colorA, :colorB].each do |a|
110
+ cell {
111
+ data a.to_s
112
+ style_class "font #{a}"
113
+ }
114
+ end
115
+ }
116
+ }
117
+
118
+
119
+
120
+ # bg styles
121
+ style('.bg.color') {
122
+ bg '#FF0000'
123
+ font '#FFFFFF'
124
+ }
125
+ style('.bg.pattern') {
126
+ bg :horz_stripe
127
+ }
128
+ style('.bg.pattern.color') {
129
+ bg '#FF0000', :horz_stripe => '#000000'
130
+ font '#FFFFFF'
131
+ }
132
+
133
+ worksheet {
134
+ name "bg"
135
+
136
+ column {
137
+ width 100
138
+ }
139
+
140
+ row {
141
+ height 50
142
+ cell {
143
+ style_class "bg color"
144
+ data 'COLOR'
145
+ }
146
+ }
147
+ row {
148
+ height 50
149
+ cell {
150
+ style_class "bg pattern"
151
+ data 'PATTERN'
152
+ }
153
+ }
154
+ row {
155
+ height 50
156
+ cell {
157
+ style_class "bg pattern color"
158
+ data 'PATTERN COLOR'
159
+ }
160
+ }
161
+ }
162
+
163
+
164
+
165
+ # border styles
166
+ style('.border.top.color') { border_top '#FF0000' }
167
+ style('.border.right.color') { border_right '#00FF00' }
168
+ style('.border.bottom.color') { border_bottom '#0000FF' }
169
+ style('.border.left.color') { border_left '#FFFF00' }
170
+ style('.border.top.weight') { border_top :hairline }
171
+ style('.border.right.weight') { border_right :thin }
172
+ style('.border.bottom.weight') { border_bottom :medium }
173
+ style('.border.left.weight') { border_left :thick }
174
+ style('.border.top.style') { border_top :continuous }
175
+ style('.border.right.style') { border_right :dash }
176
+ style('.border.bottom.style') { border_bottom :dot }
177
+ style('.border.left.style') { border_left :dash_dot }
178
+ style('.border.all') {
179
+ border :continuous, :thick, '#00FFFF'
180
+ }
181
+
182
+ worksheet {
183
+ name "border"
184
+
185
+ column {
186
+ width 20
187
+ }
188
+ column {
189
+ width 200
190
+ }
191
+
192
+ row {}
193
+ row {
194
+ height 50
195
+ cell {}
196
+ cell {
197
+ style_class "border top color weight style"
198
+ data 'top red hairline continuous'
199
+ }
200
+ }
201
+ row {}
202
+ row {
203
+ height 50
204
+ cell {}
205
+ cell {
206
+ style_class "border right color weight style"
207
+ data 'right green thin dash'
208
+ }
209
+ }
210
+ row {}
211
+ row {
212
+ height 50
213
+ cell {}
214
+ cell {
215
+ style_class "border bottom color weight style"
216
+ data 'bottom blue medium dat'
217
+ }
218
+ }
219
+ row {}
220
+ row {
221
+ height 50
222
+ cell {}
223
+ cell {
224
+ style_class "border left color weight style"
225
+ data 'left yellow thick dast_dot'
226
+ }
227
+ }
228
+ row {}
229
+ row {
230
+ height 50
231
+ cell {}
232
+ cell {
233
+ style_class "border all"
234
+ data 'all aqua'
235
+ }
236
+ }
237
+ }
238
+
239
+
240
+
241
+ }.to_file('examples/styles.xls')
@@ -1,3 +1,3 @@
1
1
  module Osheet
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -16,12 +16,21 @@ module Osheet
16
16
  end
17
17
 
18
18
  def name(value=nil)
19
- !value.nil? ? @name = value : @name
19
+ !value.nil? ? @name = sanitized_name(value) : @name
20
20
  end
21
21
 
22
22
  def attributes
23
23
  { :name => @name }
24
24
  end
25
25
 
26
+ private
27
+
28
+ def sanitized_name(name_value)
29
+ if @workbook && @workbook.worksheets.collect{|ws| ws.name}.include?(name_value)
30
+ raise ArgumentError, "the sheet name '#{name_value}' is already in use. choose a sheet name that is not used by another sheet"
31
+ end
32
+ name_value
33
+ end
34
+
26
35
  end
27
36
  end
@@ -21,6 +21,16 @@ module Osheet::XmlssWriter
21
21
  unless oworkbook.kind_of?(::Osheet::Workbook)
22
22
  raise ArgumentError, "'#{oworkbook.inspect}' is not an Osheet::Workbook"
23
23
  end
24
+ # if oworkbook && oworkbook.worksheets.collect{|ws| ws.name}.include?(name_value)
25
+ # # puts "ERRORR!!!!!!!!!!!!!!"
26
+ # # puts "names: #{names.inspect}"
27
+ # # puts "name value: #{name_value}"
28
+ # raise ArgumentError, "the sheet name '#{name_value}' is already in use. enter a sheet name that is not used by another sheet"
29
+ # end
30
+ # # puts "names: #{names.inspect}"
31
+ # # puts "name value: #{name_value}"
32
+ # # name_value
33
+
24
34
  @ostyles = oworkbook.styles
25
35
  @workbook = ::Xmlss::Workbook.new({
26
36
  :worksheets => worksheets(oworkbook.worksheets)
@@ -52,7 +52,7 @@ module Osheet::XmlssWriter::Styles
52
52
  def merged_settings(current, add)
53
53
  # concat values for keys in both sets
54
54
  current.keys.each do |k|
55
- current[k].merge!(add.delete(k))
55
+ current[k].merge!(add.delete(k) || {})
56
56
  end
57
57
  # merge keys for anything not in the current
58
58
  current.merge(add)
@@ -92,22 +92,26 @@ module Osheet::XmlssWriter::Styles
92
92
  def font_settings(font_cmds)
93
93
  font_cmds.inject({}) do |font_settings, font_cmd|
94
94
  if (setting = case font_cmd
95
- when :underline
96
- [:underline, :single]
97
- when :double_underline
98
- [:underline, :double]
99
- when :subscript, :superscript
100
- [:alignment, font_cmd]
101
- when :bold, :italic
102
- [font_cmd, true]
103
- when :strikethrough
104
- [:strike_through, true]
105
95
  when ::Fixnum
106
96
  [:size, font_cmd]
107
97
  when ::String
108
98
  if font_cmd =~ /^#/
109
99
  [:color, font_cmd]
110
100
  end
101
+ when :bold, :italic, :shadow
102
+ [font_cmd, true]
103
+ when :subscript, :superscript
104
+ [:alignment, font_cmd]
105
+ when :strikethrough
106
+ [:strike_through, true]
107
+ when :underline
108
+ [:underline, :single]
109
+ when :double_underline
110
+ [:underline, :double]
111
+ when :accounting_underline
112
+ [:underline, :single_accounting]
113
+ when :double_accounting_underline
114
+ [:underline, :double_accounting]
111
115
  end
112
116
  )
113
117
  font_settings[setting.first] = setting.last
@@ -141,6 +145,9 @@ module Osheet::XmlssWriter::Styles
141
145
  bg_settings[setting.first] = setting.last
142
146
  end
143
147
  end
148
+ if !bg_settings[:color].nil? && bg_settings[:pattern].nil?
149
+ bg_settings[:pattern] = :solid
150
+ end
144
151
  bg_settings
145
152
  end
146
153
  end
data/osheet.gemspec CHANGED
@@ -21,6 +21,6 @@ Gem::Specification.new do |s|
21
21
  s.add_development_dependency("test-belt", ["= 0.2.1"]) # locked to a specific version for test stability
22
22
 
23
23
  s.add_dependency("enumeration", ["~>1.1.0"])
24
- s.add_dependency("xmlss", "~>0.0.2")
24
+ s.add_dependency("xmlss", "~>0.1.0")
25
25
 
26
26
  end
@@ -58,6 +58,25 @@ module Osheet
58
58
  assert_kind_of Worksheet, worksheets.first
59
59
  end
60
60
 
61
+ should "not allow multiple worksheets with the same name" do
62
+ assert_raises ArgumentError do
63
+ Workbook.new {
64
+ title "should fail"
65
+
66
+ worksheet { name "awesome" }
67
+ worksheet { name "awesome" }
68
+ }
69
+ end
70
+ assert_nothing_raised do
71
+ Workbook.new {
72
+ title "should not fail"
73
+
74
+ worksheet { name "awesome" }
75
+ worksheet { name "awesome1" }
76
+ }
77
+ end
78
+ end
79
+
61
80
  should "know it's attribute(s)" do
62
81
  [:title].each do |a|
63
82
  assert subject.attributes.has_key?(a)
@@ -23,7 +23,7 @@ module Osheet
23
23
  }
24
24
  end
25
25
 
26
- should "allow writing an Osheet::Workbook" do
26
+ should "only allow writing an Osheet::Workbook" do
27
27
  assert_nothing_raised do
28
28
  subject.workbook = @workbook
29
29
  end
@@ -32,6 +32,16 @@ module Osheet
32
32
  end
33
33
  end
34
34
 
35
+ should "not allow writing a workbook that has multiple worksheets with the same name" do
36
+ assert_raises ArgumentError do
37
+ subject.workbook = Workbook.new {
38
+ title "invalid"
39
+ worksheet { name "testsheet1" }
40
+ worksheet { name "testsheet1" }
41
+ }
42
+ end
43
+ end
44
+
35
45
  should "create an Xmlss workbook" do
36
46
  assert_nothing_raised do
37
47
  subject.workbook = @workbook
@@ -12,6 +12,7 @@ module Osheet
12
12
  style('.font.size') { font 14 }
13
13
  style('.font.weight') { font :bold }
14
14
  style('.font.style') { font :italic }
15
+ style('.align.center') { align :center }
15
16
  }
16
17
  end
17
18
 
@@ -36,7 +37,7 @@ module Osheet
36
37
  end
37
38
 
38
39
  should "build a style obj from many matching osheet styles" do
39
- xmlss_style = subject.send(:style, 'font size weight style')
40
+ xmlss_style = subject.send(:style, 'font size weight style align center')
40
41
  assert_equal 14, xmlss_style.font.size
41
42
  assert_equal true, xmlss_style.font.bold?
42
43
  assert_equal true, xmlss_style.font.italic?
@@ -102,11 +103,9 @@ module Osheet
102
103
  subject { XmlssWriter::Base.new }
103
104
  before do
104
105
  subject.workbook = Workbook.new {
105
- [
106
- :underline, :double_underline,
107
- :subscript, :superscript,
108
- :bold, :italic, :strikethrough,
109
- :wrap
106
+ [ :underline, :double_underline, :accounting_underline, :double_accounting_underline,
107
+ :subscript, :superscript, :shadow, :strikethrough, :wrap,
108
+ :bold, :italic
110
109
  ].each do |s|
111
110
  style(".font.#{s}") { font s }
112
111
  end
@@ -122,6 +121,8 @@ module Osheet
122
121
  should "build style objs for font underline settings" do
123
122
  assert_equal ::Xmlss::Style::Font.underline(:single), subject.send(:style, 'font underline').font.underline
124
123
  assert_equal ::Xmlss::Style::Font.underline(:double), subject.send(:style, 'font double_underline').font.underline
124
+ assert_equal ::Xmlss::Style::Font.underline(:single_accounting), subject.send(:style, 'font accounting_underline').font.underline
125
+ assert_equal ::Xmlss::Style::Font.underline(:double_accounting), subject.send(:style, 'font double_accounting_underline').font.underline
125
126
  end
126
127
 
127
128
  should "build style objs for font alignment settings" do
@@ -133,6 +134,7 @@ module Osheet
133
134
  assert_equal true, subject.send(:style, 'font bold').font.bold?
134
135
  assert_equal true, subject.send(:style, 'font italic').font.italic?
135
136
  assert_equal true, subject.send(:style, 'font strikethrough').font.strike_through?
137
+ assert_equal true, subject.send(:style, 'font shadow').font.shadow?
136
138
  end
137
139
 
138
140
  should "build style objs for font size" do
@@ -147,7 +149,7 @@ module Osheet
147
149
  end
148
150
 
149
151
  class XmlssWriter::Bg < Test::Unit::TestCase
150
- context("Font bg writer") do
152
+ context("Bg writer") do
151
153
 
152
154
  subject { XmlssWriter::Base.new }
153
155
  before do
@@ -155,6 +157,8 @@ module Osheet
155
157
  style('.bg.color') { bg '#FF0000' }
156
158
  style('.bg.pattern-only') { bg :solid }
157
159
  style('.bg.pattern-color') { bg :horz_stripe => '#0000FF' }
160
+ style('.bg.color-first') { bg '#00FF00', {:horz_stripe => '#0000FF'} }
161
+ style('.bg.pattern-first') { bg({:horz_stripe => '#0000FF'}, '#00FF00') }
158
162
  }
159
163
  end
160
164
 
@@ -162,7 +166,7 @@ module Osheet
162
166
  assert_equal nil, subject.send(:style, 'bg').interior
163
167
  end
164
168
 
165
- should "build style objs for bg color" do
169
+ should "build style objs for bg color and auto set the pattern to solid" do
166
170
  assert_equal '#FF0000', subject.send(:style, 'bg color').interior.color
167
171
  end
168
172
 
@@ -173,6 +177,18 @@ module Osheet
173
177
  assert_equal '#0000FF', subject.send(:style, 'bg pattern-color').interior.pattern_color
174
178
  end
175
179
 
180
+ should "set pattern to solid when setting bg color" do
181
+ assert_equal ::Xmlss::Style::Interior.pattern(:solid), subject.send(:style, 'bg color').interior.pattern
182
+ end
183
+
184
+ should "set pattern to pattern setting when first setting bg color then pattern" do
185
+ assert_equal ::Xmlss::Style::Interior.pattern(:horz_stripe), subject.send(:style, 'bg color-first').interior.pattern
186
+ end
187
+
188
+ should "set pattern to pattern setting when first setting bg pattern then color" do
189
+ assert_equal ::Xmlss::Style::Interior.pattern(:horz_stripe), subject.send(:style, 'bg pattern-first').interior.pattern
190
+ end
191
+
176
192
  end
177
193
  end
178
194
 
@@ -241,9 +257,8 @@ module Osheet
241
257
  subject { XmlssWriter::Base.new }
242
258
 
243
259
  should "build a style obj with formatting" do
244
- # TODO: uncomment when further styles defined
245
- #assert_equal '@', subject.send(:style, '', Osheet::Format.new(:text)).number_format.format
246
- #assert_equal 'mm/dd/yy', subject.send(:style, '', 'mm/dd/yy').number_format.format
260
+ assert_equal '@', subject.send(:style, '', Osheet::Format.new(:text)).number_format.format
261
+ assert_equal 'mm/dd/yy', subject.send(:style, '', Osheet::Format.new(:datetime, 'mm/dd/yy')).number_format.format
247
262
  end
248
263
 
249
264
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osheet
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-07 00:00:00 -05:00
18
+ date: 2011-04-11 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -76,9 +76,9 @@ dependencies:
76
76
  hash: 27
77
77
  segments:
78
78
  - 0
79
+ - 1
79
80
  - 0
80
- - 2
81
- version: 0.0.2
81
+ version: 0.1.0
82
82
  type: :runtime
83
83
  version_requirements: *id004
84
84
  description: A DSL for specifying and generating rich spreasheetML. Specify your spreadsheet using the richness of Ruby and easily produce the corresponding spreadsheetML to render in MS Excel.
@@ -98,7 +98,9 @@ files:
98
98
  - README.rdoc
99
99
  - Rakefile
100
100
  - examples/basic.rb
101
+ - examples/basic_with_templates.rb
101
102
  - examples/formats.rb
103
+ - examples/styles.rb
102
104
  - examples/trivial.rb
103
105
  - lib/osheet.rb
104
106
  - lib/osheet/associations.rb