spreadsheet 0.8.9 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
  before_install:
3
3
  - gem install bundler
4
+ bundler_args: --binstubs
4
5
  script: "ruby -w ./test/suite.rb"
5
6
  rvm:
6
7
  - ruby-head
@@ -22,4 +23,9 @@ matrix:
22
23
  - rvm: jruby-head
23
24
  - rvm: jruby-19mode
24
25
  - rvm: jruby-18mode
25
- - rvm: ree
26
+ - rvm: ree
27
+ notifications:
28
+ email:
29
+ recipients:
30
+ - yasaka@ywesee.com
31
+ - zdavatz@ywesee.com
@@ -1,54 +1,72 @@
1
- = Getting Started with Spreadsheet
1
+ # Getting Started with Spreadsheet
2
2
  This guide is meant to get you started using Spreadsheet. By the end of it,
3
3
  you should be able to read and write Spreadsheets.
4
4
 
5
5
 
6
- == Reading is easy!
6
+ ## Reading is easy!
7
7
  First, make sure all that code is loaded:
8
8
 
9
- require 'spreadsheet'
9
+ ```ruby
10
+ require 'spreadsheet'
11
+ ```
10
12
 
11
13
  Worksheets come in various Encodings. You need to tell Spreadsheet which
12
14
  Encoding you want to deal with. The Default is UTF-8
13
15
 
14
- Spreadsheet.client_encoding = 'UTF-8'
16
+ ```ruby
17
+ Spreadsheet.client_encoding = 'UTF-8'
18
+ ```
15
19
 
16
20
  Let's open a workbook:
17
21
 
18
- book = Spreadsheet.open '/path/to/an/excel-file.xls'
22
+ ```ruby
23
+ book = Spreadsheet.open '/path/to/an/excel-file.xls'
24
+ ```
19
25
 
20
26
  We can either access all the Worksheets in a Workbook...
21
27
 
22
- book.worksheets
28
+ ```ruby
29
+ book.worksheets
30
+ ```
23
31
 
24
32
  ...or access them by index or name (encoded in your client_encoding)
25
33
 
26
- sheet1 = book.worksheet 0
27
- sheet2 = book.worksheet 'Sheet1'
34
+ ```ruby
35
+ sheet1 = book.worksheet 0
36
+ sheet2 = book.worksheet 'Sheet1'
37
+ ```
28
38
 
29
39
  Now you can either iterate over all rows that contain some data. A call to
30
40
  Worksheet.each without argument will omit empty rows at the beginning of the
31
41
  Worksheet:
32
42
 
33
- sheet1.each do |row|
34
- # do something interesting with a row
35
- end
43
+ ```ruby
44
+ sheet1.each do |row|
45
+ # do something interesting with a row
46
+ end
47
+ ```
36
48
 
37
49
  Or you can tell Worksheet how many rows should be omitted at the beginning.
38
50
  The following starts at the 3rd row, regardless of whether or not it or the
39
51
  preceding rows contain any data:
40
52
 
41
- sheet2.each 2 do |row|
42
- # do something interesting with a row
43
- end
53
+ ```ruby
54
+ sheet2.each 2 do |row|
55
+ # do something interesting with a row
56
+ end
57
+ ```
44
58
 
45
59
  Or you can access rows directly, by their index (0-based):
46
60
 
47
- row = sheet1.row(3)
61
+ ```ruby
62
+ row = sheet1.row(3)
63
+ ```
48
64
 
49
65
  To access the values stored in a Row, treat the Row like an Array.
50
66
 
51
- row[0]
67
+ ```ruby
68
+ row[0]
69
+ ```
52
70
 
53
71
  -> this will return a String, a Float, an Integer, a Formula, a Link or a Date
54
72
  or DateTime object - or nil if the cell is empty.
@@ -56,102 +74,124 @@ or DateTime object - or nil if the cell is empty.
56
74
  More information about the formatting of a cell can be found in the Format
57
75
  with the equivalent index
58
76
 
59
- row.format 2
77
+ ```ruby
78
+ row.format 2
79
+ ```
60
80
 
61
-
62
- == Writing is easy
81
+ ## Writing is easy
63
82
  As before, make sure you have Spreadsheet required and the client_encoding
64
83
  set. Then make a new Workbook:
65
84
 
66
- book = Spreadsheet::Workbook.new
85
+ ```ruby
86
+ book = Spreadsheet::Workbook.new
87
+ ```
67
88
 
68
89
  Add a Worksheet and you're good to go:
69
90
 
70
- sheet1 = book.create_worksheet
91
+ ```ruby
92
+ sheet1 = book.create_worksheet
93
+ ```
71
94
 
72
95
  This will create a Worksheet with the Name "Worksheet1". If you prefer another
73
96
  name, you may do either of the following:
74
97
 
75
- sheet2 = book.create_worksheet :name => 'My Second Worksheet'
76
- sheet1.name = 'My First Worksheet'
98
+ ```ruby
99
+ sheet2 = book.create_worksheet :name => 'My Second Worksheet'
100
+ sheet1.name = 'My First Worksheet'
101
+ ```
77
102
 
78
103
  Now, add data to the Worksheet, using either Worksheet#[]=,
79
104
  Worksheet#update_row, or work directly on Row using any of the Array-Methods
80
105
  that modify an Array in place:
81
106
 
82
- sheet1.row(0).concat %w{Name Country Acknowlegement}
83
- sheet1[1,0] = 'Japan'
84
- row = sheet1.row(1)
85
- row.push 'Creator of Ruby'
86
- row.unshift 'Yukihiro Matsumoto'
87
- sheet1.row(2).replace [ 'Daniel J. Berger', 'U.S.A.',
88
- 'Author of original code for Spreadsheet::Excel' ]
89
- sheet1.row(3).push 'Charles Lowe', 'Author of the ruby-ole Library'
90
- sheet1.row(3).insert 1, 'Unknown'
91
- sheet1.update_row 4, 'Hannes Wyss', 'Switzerland', 'Author'
107
+ ```ruby
108
+ sheet1.row(0).concat %w{Name Country Acknowlegement}
109
+ sheet1[1,0] = 'Japan'
110
+ row = sheet1.row(1)
111
+ row.push 'Creator of Ruby'
112
+ row.unshift 'Yukihiro Matsumoto'
113
+ sheet1.row(2).replace [ 'Daniel J. Berger', 'U.S.A.',
114
+ 'Author of original code for Spreadsheet::Excel' ]
115
+ sheet1.row(3).push 'Charles Lowe', 'Author of the ruby-ole Library'
116
+ sheet1.row(3).insert 1, 'Unknown'
117
+ sheet1.update_row 4, 'Hannes Wyss', 'Switzerland', 'Author'
118
+ ```
92
119
 
93
120
  Add some Formatting for flavour:
94
121
 
95
- sheet1.row(0).height = 18
122
+ ```ruby
123
+ sheet1.row(0).height = 18
96
124
 
97
- format = Spreadsheet::Format.new :color => :blue,
98
- :weight => :bold,
99
- :size => 18
100
- sheet1.row(0).default_format = format
125
+ format = Spreadsheet::Format.new :color => :blue,
126
+ :weight => :bold,
127
+ :size => 18
128
+ sheet1.row(0).default_format = format
101
129
 
102
- bold = Spreadsheet::Format.new :weight => :bold
103
- 4.times do |x| sheet1.row(x + 1).set_format(0, bold) end
130
+ bold = Spreadsheet::Format.new :weight => :bold
131
+ 4.times do |x| sheet1.row(x + 1).set_format(0, bold) end
132
+ ```
104
133
 
105
134
  And finally, write the Excel File:
106
135
 
107
- book.write '/path/to/output/excel-file.xls'
108
-
136
+ ```ruby
137
+ book.write '/path/to/output/excel-file.xls'
138
+ ```
109
139
 
110
- == Modifying an existing Document
140
+ ## Modifying an existing Document
111
141
 
112
142
  Spreadsheet has some limited support for modifying an existing Document. This
113
143
  is done by copying verbatim those parts of an Excel-document which Spreadsheet
114
144
  can't modify (yet), recalculating relevant offsets, and writing the data that
115
145
  can be changed.
116
146
  Here's what should work:
147
+
117
148
  * Adding, changing and deleting cells.
118
149
  * You should be able to fill in Data to be evaluated by predefined Formulas
150
+
119
151
  Limitations:
152
+
120
153
  * Spreadsheet can only write BIFF8 (Excel97 and higher). The results of
121
- modifying an earlier version of Excel are undefined.
154
+ modifying an earlier version of Excel are undefined.
122
155
  * Spreadsheet does not modify Formatting at present. That means in particular
123
- that if you set the Value of a Cell to a Date, it can only be read as a
124
- Date if its Format was set correctly prior to the change.
156
+ that if you set the Value of a Cell to a Date, it can only be read as a
157
+ Date if its Format was set correctly prior to the change.
125
158
  * Although it is theoretically possible, it is not recommended to write the
126
- resulting Document back to the same File/IO that it was read from.
159
+ resulting Document back to the same File/IO that it was read from.
127
160
 
128
161
  And here's how it works:
129
162
 
130
- book = Spreadsheet.open '/path/to/an/excel-file.xls'
131
- sheet = book.worksheet 0
132
- sheet.each do |row|
133
- row[0] *= 2
134
- end
135
- book.write '/path/to/output/excel-file.xls'
136
-
163
+ ```ruby
164
+ book = Spreadsheet.open '/path/to/an/excel-file.xls'
165
+ sheet = book.worksheet 0
166
+ sheet.each do |row|
167
+ row[0] *= 2
168
+ end
169
+ book.write '/path/to/output/excel-file.xls'
170
+ ```
137
171
 
138
- == Date and DateTime
172
+ ## Date and DateTime
139
173
  Excel does not know a separate Datatype for Dates. Instead it encodes Dates
140
174
  into standard floating-point numbers and recognizes a Date-Cell by its
141
175
  formatting-string:
142
176
 
143
- row.format(3).number_format
177
+ ```ruby
178
+ row.format(3).number_format
179
+ ```
144
180
 
145
181
  Whenever a Cell's Format describes a Date or Time, Spreadsheet will give you
146
182
  the decoded Date or DateTime value. Should you need to access the underlying
147
183
  Float, you may do the following:
148
184
 
149
- row.at(3)
185
+ ```ruby
186
+ row.at(3)
187
+ ```
150
188
 
151
189
  If for some reason the Date-recognition fails, you may force Date-decoding:
152
190
 
153
- row.date(3)
154
- row.datetime(3)
191
+ ```ruby
192
+ row.date(3)
193
+ row.datetime(3)
194
+ ```
155
195
 
156
196
  When you set the value of a Cell to a Date, Time or DateTime, Spreadsheet will
157
197
  try to set the cell's number-format to a corresponding value (one of Excel's
@@ -159,16 +199,18 @@ builtin formats). If you have already defined a Date- or DateTime-format,
159
199
  Spreadsheet will use that instead. If a format has already been applied to
160
200
  a particular Cell, Spreadsheet will leave it untouched:
161
201
 
162
- row[4] = Date.new 1975, 8, 21
163
- # -> assigns the builtin Date-Format: 'M/D/YY'
164
- book.add_format Format.new(:number_format => 'DD.MM.YYYY hh:mm:ss')
165
- row[5] = DateTime.new 2008, 10, 12, 11, 59
166
- # -> assigns the added DateTime-Format: 'DD.MM.YYYY hh:mm:ss'
167
- row.set_format 6, Format.new(:number_format => 'D-MMM-YYYY')
168
- row[6] = Time.new 2008, 10, 12
169
- # -> the Format of cell 6 is left unchanged.
170
-
171
- == Outline (Grouping) and Hiding
202
+ ```ruby
203
+ row[4] = Date.new 1975, 8, 21
204
+ # -> assigns the builtin Date-Format: 'M/D/YY'
205
+ book.add_format Format.new(:number_format => 'DD.MM.YYYY hh:mm:ss')
206
+ row[5] = DateTime.new 2008, 10, 12, 11, 59
207
+ # -> assigns the added DateTime-Format: 'DD.MM.YYYY hh:mm:ss'
208
+ row.set_format 6, Format.new(:number_format => 'D-MMM-YYYY')
209
+ row[6] = Time.new 2008, 10, 12
210
+ # -> the Format of cell 6 is left unchanged.
211
+ ```
212
+
213
+ ## Outline (Grouping) and Hiding
172
214
  Spreadsheet supports outline (grouping) and hiding functions from version
173
215
  0.6.5. In order to hide rows or columns, you can use 'hidden' property.
174
216
  As for outline, 'outline_level' property is also available. You can use
@@ -177,72 +219,84 @@ both 'hidden' and 'outline_level' at the same time.
177
219
  You can create a new file with outline and hiding rows and columns as
178
220
  follows:
179
221
 
180
- require 'spreadsheet'
181
-
182
- # create a new book and sheet
183
- book = Spreadsheet::Workbook.new
184
- sheet = book.create_worksheet
185
- 5.times {|j| 5.times {|i| sheet[j,i] = (i+1)*10**j}}
186
-
187
- # column
188
- sheet.column(2).hidden = true
189
- sheet.column(3).hidden = true
190
- sheet.column(2).outline_level = 1
191
- sheet.column(3).outline_level = 1
192
-
193
- # row
194
- sheet.row(2).hidden = true
195
- sheet.row(3).hidden = true
196
- sheet.row(2).outline_level = 1
197
- sheet.row(3).outline_level = 1
198
-
199
- # save file
200
- book.write 'out.xls'
222
+ ```ruby
223
+ require 'spreadsheet'
224
+
225
+ # create a new book and sheet
226
+ book = Spreadsheet::Workbook.new
227
+ sheet = book.create_worksheet
228
+ 5.times {|j| 5.times {|i| sheet[j,i] = (i+1)*10**j}}
229
+
230
+ # column
231
+ sheet.column(2).hidden = true
232
+ sheet.column(3).hidden = true
233
+ sheet.column(2).outline_level = 1
234
+ sheet.column(3).outline_level = 1
235
+
236
+ # row
237
+ sheet.row(2).hidden = true
238
+ sheet.row(3).hidden = true
239
+ sheet.row(2).outline_level = 1
240
+ sheet.row(3).outline_level = 1
241
+
242
+ # save file
243
+ book.write 'out.xls'
244
+ ```
201
245
 
202
246
  Also you can read an existing file and change the hidden and outline
203
247
  properties. Here is the example below:
204
248
 
205
- require 'spreadsheet'
206
-
207
- # read an existing file
208
- file = ARGV[0]
209
- book = Spreadsheet.open(file, 'rb')
210
- sheet= book.worksheet(0)
211
-
212
- # column
213
- sheet.column(2).hidden = true
214
- sheet.column(3).hidden = true
215
- sheet.column(2).outline_level = 1
216
- sheet.column(3).outline_level = 1
217
-
218
- # row
219
- sheet.row(2).hidden = true
220
- sheet.row(3).hidden = true
221
- sheet.row(2).outline_level = 1
222
- sheet.row(3).outline_level = 1
223
-
224
- # save file
225
- book.write "out.xls"
249
+ ```ruby
250
+ require 'spreadsheet'
251
+
252
+ # read an existing file
253
+ file = ARGV[0]
254
+ book = Spreadsheet.open(file, 'rb')
255
+ sheet= book.worksheet(0)
256
+
257
+ # column
258
+ sheet.column(2).hidden = true
259
+ sheet.column(3).hidden = true
260
+ sheet.column(2).outline_level = 1
261
+ sheet.column(3).outline_level = 1
262
+
263
+ # row
264
+ sheet.row(2).hidden = true
265
+ sheet.row(3).hidden = true
266
+ sheet.row(2).outline_level = 1
267
+ sheet.row(3).outline_level = 1
268
+
269
+ # save file
270
+ book.write "out.xls"
271
+ ```
226
272
 
227
273
  Notes
228
- * The outline_level should be under 8, which is due to the Excel data format.
274
+ * The outline_level should be under 8, which is due to the Excel data format.
229
275
 
230
- == More about Encodings
276
+ ## More about Encodings
231
277
  Spreadsheet assumes it's running on Ruby 1.8 with Iconv-support. It is your
232
278
  responsibility to handle Conversion Errors, or to prevent them e.g. by using
233
279
  the Iconv Transliteration and Ignore flags:
234
280
  Spreadsheet.client_encoding = 'LATIN1//TRANSLIT//IGNORE'
235
281
 
282
+ ## Page setup (for printing)
283
+
284
+ ```ruby
285
+ sheet.pagesetup[:orientation] = :landscape # or :portrait (default)
286
+ sheet.pagesetup[:adjust_to] = 85 # default 100
287
+ ```
236
288
 
237
- == Backward Compatibility
289
+ ## Backward Compatibility
238
290
  Spreadsheet is designed to be a drop-in replacement for both ParseExcel and
239
291
  Spreadsheet::Excel. It provides a number of require-paths for backward
240
292
  compatibility with its predecessors. If you have been working with ParseExcel,
241
293
  you have probably used one or more of the following:
242
294
 
243
- require 'parseexcel'
244
- require 'parseexcel/parseexcel'
245
- require 'parseexcel/parser'
295
+ ```ruby
296
+ require 'parseexcel'
297
+ require 'parseexcel/parseexcel'
298
+ require 'parseexcel/parser'
299
+ ```
246
300
 
247
301
  Either of the above will define the ParseExcel.parse method as a facade to
248
302
  Spreadsheet.open. Additionally, this will alter Spreadsheets behavior to define
@@ -255,7 +309,9 @@ If you are upgrading from Spreadsheet::Excel, you were probably using
255
309
  Workbook#add_worksheet and Worksheet#write, write_row or write_column.
256
310
  Use the following to load the code which provides them:
257
311
 
258
- require 'spreadsheet/excel'
312
+ ```ruby
313
+ require 'spreadsheet/excel'
314
+ ```
259
315
 
260
316
  Again, you will have to manually uninstall the spreadsheet-excel library.
261
317
 
data/Gemfile CHANGED
@@ -1,3 +1,9 @@
1
1
  source "https://rubygems.org"
2
+ gem 'ruby-ole'
3
+ gem 'spreadsheet'
2
4
 
3
- gemspec
5
+ group :development do
6
+ gem 'ruby-ole'
7
+ gem 'hoe', '>= 3.4'
8
+ gem 'spreadsheet'
9
+ end