spreadsheet 1.1.5 → 1.3.0

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