spreadbase 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -53,6 +53,27 @@ Append a row:
53
53
 
54
54
  table_2.append_row( [ 'Fabrizio F.' ] )
55
55
 
56
+ Read a column, or a range of columns:
57
+
58
+ table.column( 0 )
59
+
60
+ # [ 'Dish', 'Roasted 6502', '8080, with an 8-bit bus', '65000 with side dishes of Copper and Blitter' ]
61
+
62
+ table.column( 0 .. 1 )
63
+
64
+ # [ [ 'Dish', 'Roasted 6502', '8080, with an 8-bit bus', '65000 with side dishes of Copper and Blitter' ],
65
+ # [ 'Price', 38.911, 8, 512.0 ] ]
66
+
67
+ Read a row, or a range of rows:
68
+
69
+ table.row( 1 )
70
+
71
+ # [ 'Roasted 6502', 38.911 ]
72
+
73
+ table.row( 1 .. 2 )
74
+
75
+ # [ [ 'Roasted 6502', 38.911 ], [ '8080, with an 8-bit bus', 8 ] ]
76
+
56
77
  Read a cell:
57
78
 
58
79
  price_8080 = document.tables[ 0 ][ 1, 2 ]
@@ -101,6 +122,8 @@ Save the document:
101
122
 
102
123
  document.save
103
124
 
125
+ Enjoy many other APIs.
126
+
104
127
  Notes
105
128
  -----
106
129
 
@@ -90,7 +90,7 @@ module SpreadBase # :nodoc:
90
90
  #
91
91
  # _params_:
92
92
  #
93
- # +row_index+:: int (0-based). see notes about the rows indexing.
93
+ # +row_index+:: int or range (0-based). see notes about the rows indexing.
94
94
  #
95
95
  def row( row_index )
96
96
  check_row_index( row_index )
@@ -104,9 +104,9 @@ module SpreadBase # :nodoc:
104
104
  #
105
105
  # _params_:
106
106
  #
107
- # +row_index+:: int (0-based). see notes about the rows indexing.
107
+ # +row_index+:: int or range (0-based). see notes about the rows indexing.
108
108
  #
109
- # _returns_ the deleted row
109
+ # _returns_ the deleted row[s]
110
110
  #
111
111
  def delete_row( row_index )
112
112
  check_row_index( row_index )
@@ -141,35 +141,60 @@ module SpreadBase # :nodoc:
141
141
  #
142
142
  # _params_:
143
143
  #
144
- # +column_indentifier+:: either an int (0-based) or the excel-format identifier (AA...).
144
+ # +column_indentifier+:: for single access, us either an int (0-based) or the excel-format identifier (AA...).
145
145
  # when int, follow the same idea of the rows indexing (ruby semantics).
146
+ # for multiple access, use a range either of int or excel-format identifiers - pay attention, because ( 'A'..'c' ) is not semantically correct.
147
+ # interestingly, ruby letter ranges convention is the same as the excel columns one.
146
148
  #
147
149
  def column( column_identifier )
148
- column_index = decode_column_identifier( column_identifier )
150
+ if column_identifier.is_a?( Range )
151
+ min_index = decode_column_identifier( column_identifier.min )
152
+ max_index = decode_column_identifier( column_identifier.max )
153
+
154
+ ( min_index .. max_index ).map do | column_index |
155
+ @data.map do | row |
156
+ row[ column_index ]
157
+ end
158
+ end
159
+ else
160
+ column_index = decode_column_identifier( column_identifier )
149
161
 
150
- @data.map do | row |
151
- row[ column_index ]
162
+ @data.map do | row |
163
+ row[ column_index ]
164
+ end
152
165
  end
153
166
  end
154
167
 
155
168
  # Deletes a column.
156
169
  #
157
- # WATCH OUT! This method doesn't have the range restrictions that axis indexes generally has, that is, it's possible to delete a column outside the boundaries of the rows - it will return nil for each of those values.
170
+ # See Table#column for the indexing notes.
158
171
  #
159
172
  # _params_:
160
173
  #
161
- # +column_indentifier+:: either an int (0-based) or the excel-format identifier (AA...).
162
- # when int, follow the same idea of the rows indexing (ruby semantics).
174
+ # +column_indentifier+:: See Table#column
163
175
  #
164
176
  # _returns_ the deleted column
165
177
  #
166
178
  def delete_column( column_identifier )
167
- column_index = decode_column_identifier( column_identifier )
179
+ if column_identifier.is_a?( Range )
180
+ min_index = decode_column_identifier( column_identifier.min )
181
+ max_index = decode_column_identifier( column_identifier.max )
182
+
183
+ reverse_result = max_index.downto( min_index ).map do | column_index |
184
+ @data.map do | row |
185
+ row.slice!( column_index )
186
+ end
187
+ end
168
188
 
169
- @column_width_styles.slice!( column_index )
189
+ reverse_result.reverse
190
+ else
191
+ column_index = decode_column_identifier( column_identifier )
170
192
 
171
- @data.map do | row |
172
- row.slice!( column_index )
193
+ @column_width_styles.slice!( column_index )
194
+
195
+ @data.map do | row |
196
+ row.slice!( column_index )
197
+ end
173
198
  end
174
199
  end
175
200
 
@@ -226,6 +251,8 @@ module SpreadBase # :nodoc:
226
251
 
227
252
  positive_limit = allow_append ? @data.size : @data.size - 1
228
253
 
254
+ row_index = row_index.max if row_index.is_a?( Range )
255
+
229
256
  raise "Invalid row index (#{ row_index }) - allowed 0 to #{ positive_limit }" if row_index < 0 || row_index > positive_limit
230
257
  end
231
258
 
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module SpreadBase
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -102,6 +102,15 @@ describe SpreadBase::Table do
102
102
  lambda { @sample_table.row( -1 ) }.should raise_error( RuntimeError, "Invalid row index (-1) - allowed 0 to 2" )
103
103
  end
104
104
 
105
+ it "should access a set of rows by range" do
106
+ @sample_table.row( ( 0 .. 1 ) ).should == [
107
+ [ 1, 1.1, T_BIGDECIMAL ],
108
+ [ T_DATE, T_DATETIME, T_TIME ],
109
+ ]
110
+
111
+ lambda { @sample_table.row( ( 0 .. 5 ) ) }.should raise_error( RuntimeError, "Invalid row index (5) - allowed 0 to 2" )
112
+ end
113
+
105
114
  it "should delete a row" do
106
115
  @sample_table.delete_row( 1 ).should == [ T_DATE, T_DATETIME, T_TIME ]
107
116
 
@@ -113,6 +122,19 @@ describe SpreadBase::Table do
113
122
  lambda { @sample_table.delete_row( -1 ) }.should raise_error( RuntimeError, "Invalid row index (-1) - allowed 0 to 1" )
114
123
  end
115
124
 
125
+ it "should delete a set of rows by range" do
126
+ @sample_table.delete_row( ( 0 .. 1 ) ).should == [
127
+ [ 1, 1.1, T_BIGDECIMAL ],
128
+ [ T_DATE, T_DATETIME, T_TIME ],
129
+ ]
130
+
131
+ @sample_table.data.should == [
132
+ [ true, 'a', nil ]
133
+ ]
134
+
135
+ lambda { @sample_table.delete_row( ( 0 .. 5 ) ) }.should raise_error( RuntimeError, "Invalid row index (5) - allowed 0 to 0" )
136
+ end
137
+
116
138
  it "should insert a row" do
117
139
  @sample_table.insert_row( 1, [ 4, 5, 6 ] )
118
140
 
@@ -152,6 +174,18 @@ describe SpreadBase::Table do
152
174
  @sample_table.column( 3 ).should == [ nil, nil, nil ]
153
175
  end
154
176
 
177
+ it "should access a set of columns by range" do
178
+ @sample_table.column( ( 0 .. 1 ) ).should == [
179
+ [ 1, T_DATE, true ],
180
+ [ 1.1, T_DATETIME, 'a' ],
181
+ ]
182
+
183
+ @sample_table.column( ( 'C' .. 'D' ) ).should == [
184
+ [ T_BIGDECIMAL, T_TIME, nil ],
185
+ [ nil, nil, nil ],
186
+ ]
187
+ end
188
+
155
189
  it "should delete a column" do
156
190
  @sample_table.column_width_styles = [ 'abc', nil, 'cde' ]
157
191
 
@@ -170,6 +204,19 @@ describe SpreadBase::Table do
170
204
  ]
171
205
  end
172
206
 
207
+ it "should delete a set of columns by range" do
208
+ @sample_table.delete_column( ( 0 .. 1 ) ).should == [
209
+ [ 1, T_DATE, true ],
210
+ [ 1.1, T_DATETIME, 'a' ],
211
+ ]
212
+
213
+ @sample_table.data.should == [
214
+ [ T_BIGDECIMAL ],
215
+ [ T_TIME ],
216
+ [ nil ],
217
+ ]
218
+ end
219
+
173
220
  it "should insert a column" do
174
221
  # Setup/fill table
175
222
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spreadbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-27 00:00:00.000000000 Z
12
+ date: 2012-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: zipruby
16
- requirement: &18448480 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 0.3.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *18448480
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.3.6
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec
27
- requirement: &18447960 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,7 +37,12 @@ dependencies:
32
37
  version: 2.9.0
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *18447960
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.9.0
36
46
  description: Library for reading/writing OpenOffice Calc documents.
37
47
  email:
38
48
  - saverio.pub2@gmail.com
@@ -82,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
92
  version: '0'
83
93
  requirements: []
84
94
  rubyforge_project:
85
- rubygems_version: 1.8.17
95
+ rubygems_version: 1.8.23
86
96
  signing_key:
87
97
  specification_version: 3
88
98
  summary: Library for reading/writing OpenOffice Calc documents.