google-spreadsheet-ruby 0.1.2 → 0.1.3
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/lib/google_spreadsheet.rb +127 -111
- metadata +6 -6
data/lib/google_spreadsheet.rb
CHANGED
@@ -8,18 +8,22 @@ require "open-uri"
|
|
8
8
|
require "cgi"
|
9
9
|
require "uri"
|
10
10
|
require "rubygems"
|
11
|
-
require
|
12
|
-
|
11
|
+
require "nokogiri"
|
13
12
|
require "oauth"
|
14
13
|
Net::HTTP.version_1_2
|
15
14
|
|
16
15
|
module GoogleSpreadsheet
|
17
|
-
|
16
|
+
|
18
17
|
# Authenticates with given +mail+ and +password+, and returns GoogleSpreadsheet::Session
|
19
18
|
# if succeeds. Raises GoogleSpreadsheet::AuthenticationError if fails.
|
20
19
|
# Google Apps account is supported.
|
21
|
-
|
22
|
-
|
20
|
+
#
|
21
|
+
# +proxy+ can be nil or return value of Net::HTTP.Proxy. If +proxy+ is specified, all
|
22
|
+
# HTTP access in the session uses the proxy. If +proxy+ is nil, it uses the proxy
|
23
|
+
# specified by http_proxy environment variable if available. Otherwise it performs direct
|
24
|
+
# access.
|
25
|
+
def self.login(mail, password, proxy = nil)
|
26
|
+
return Session.login(mail, password, proxy)
|
23
27
|
end
|
24
28
|
|
25
29
|
# Authenticates with given OAuth token.
|
@@ -48,8 +52,10 @@ module GoogleSpreadsheet
|
|
48
52
|
# If +path+ doesn't exist or authentication has failed, prompts mail and password on console,
|
49
53
|
# authenticates with them, stores the session to +path+ and returns it.
|
50
54
|
#
|
55
|
+
# See login for description of parameter +proxy+.
|
56
|
+
#
|
51
57
|
# This method requires Highline library: http://rubyforge.org/projects/highline/
|
52
|
-
def self.saved_session(path = ENV["HOME"] + "/.ruby_google_spreadsheet.token")
|
58
|
+
def self.saved_session(path = ENV["HOME"] + "/.ruby_google_spreadsheet.token", proxy = nil)
|
53
59
|
tokens = {}
|
54
60
|
if File.exist?(path)
|
55
61
|
open(path) do |f|
|
@@ -59,7 +65,7 @@ module GoogleSpreadsheet
|
|
59
65
|
end
|
60
66
|
end
|
61
67
|
end
|
62
|
-
session = Session.new(tokens)
|
68
|
+
session = Session.new(tokens, nil, proxy)
|
63
69
|
session.on_auth_fail = proc() do
|
64
70
|
begin
|
65
71
|
require "highline"
|
@@ -85,45 +91,45 @@ module GoogleSpreadsheet
|
|
85
91
|
end
|
86
92
|
return session
|
87
93
|
end
|
88
|
-
|
89
|
-
|
94
|
+
|
95
|
+
|
90
96
|
module Util #:nodoc:
|
91
|
-
|
97
|
+
|
92
98
|
module_function
|
93
|
-
|
99
|
+
|
94
100
|
def encode_query(params)
|
95
101
|
return params.map(){ |k, v| CGI.escape(k) + "=" + CGI.escape(v) }.join("&")
|
96
102
|
end
|
97
|
-
|
103
|
+
|
98
104
|
def h(str)
|
99
105
|
return CGI.escapeHTML(str.to_s())
|
100
106
|
end
|
101
|
-
|
107
|
+
|
102
108
|
end
|
103
|
-
|
104
|
-
|
109
|
+
|
110
|
+
|
105
111
|
# Raised when spreadsheets.google.com has returned error.
|
106
112
|
class Error < RuntimeError
|
107
|
-
|
113
|
+
|
108
114
|
end
|
109
|
-
|
110
|
-
|
115
|
+
|
116
|
+
|
111
117
|
# Raised when GoogleSpreadsheet.login has failed.
|
112
118
|
class AuthenticationError < GoogleSpreadsheet::Error
|
113
|
-
|
119
|
+
|
114
120
|
end
|
115
|
-
|
116
|
-
|
121
|
+
|
122
|
+
|
117
123
|
# Use GoogleSpreadsheet.login or GoogleSpreadsheet.saved_session to get
|
118
124
|
# GoogleSpreadsheet::Session object.
|
119
125
|
class Session
|
120
|
-
|
126
|
+
|
121
127
|
include(Util)
|
122
128
|
extend(Util)
|
123
|
-
|
129
|
+
|
124
130
|
# The same as GoogleSpreadsheet.login.
|
125
|
-
def self.login(mail, password)
|
126
|
-
session = Session.new()
|
131
|
+
def self.login(mail, password, proxy = nil)
|
132
|
+
session = Session.new(nil, nil, proxy)
|
127
133
|
session.login(mail, password)
|
128
134
|
return session
|
129
135
|
end
|
@@ -134,9 +140,19 @@ module GoogleSpreadsheet
|
|
134
140
|
end
|
135
141
|
|
136
142
|
# Restores session using return value of auth_tokens method of previous session.
|
137
|
-
|
143
|
+
#
|
144
|
+
# See GoogleSpreadsheet.login for description of parameter +proxy+.
|
145
|
+
def initialize(auth_tokens = nil, oauth_token = nil, proxy = nil)
|
138
146
|
@oauth_token = oauth_token
|
139
147
|
@auth_tokens = auth_tokens || {}
|
148
|
+
if proxy
|
149
|
+
@proxy = proxy
|
150
|
+
elsif ENV["http_proxy"] && !ENV["http_proxy"].empty?
|
151
|
+
proxy_url = URI.parse(ENV["http_proxy"])
|
152
|
+
@proxy = Net::HTTP.Proxy(proxy_url.host, proxy_url.port)
|
153
|
+
else
|
154
|
+
@proxy = Net::HTTP
|
155
|
+
end
|
140
156
|
end
|
141
157
|
|
142
158
|
# Authenticates with given +mail+ and +password+, and updates current session object
|
@@ -152,19 +168,19 @@ module GoogleSpreadsheet
|
|
152
168
|
raise(AuthenticationError, "authentication failed for #{mail}: #{ex.message}")
|
153
169
|
end
|
154
170
|
end
|
155
|
-
|
171
|
+
|
156
172
|
# Authentication tokens.
|
157
173
|
attr_reader(:auth_tokens)
|
158
|
-
|
174
|
+
|
159
175
|
# Authentication token.
|
160
176
|
def auth_token(auth = :wise)
|
161
177
|
return @auth_tokens[auth]
|
162
178
|
end
|
163
|
-
|
179
|
+
|
164
180
|
# Proc or Method called when authentication has failed.
|
165
181
|
# When this function returns +true+, it tries again.
|
166
182
|
attr_accessor :on_auth_fail
|
167
|
-
|
183
|
+
|
168
184
|
def auth_header(auth) #:nodoc:
|
169
185
|
token = auth == :none ? nil : @auth_tokens[auth]
|
170
186
|
if token
|
@@ -193,7 +209,7 @@ module GoogleSpreadsheet
|
|
193
209
|
end
|
194
210
|
return result
|
195
211
|
end
|
196
|
-
|
212
|
+
|
197
213
|
# Returns GoogleSpreadsheet::Spreadsheet with given +key+.
|
198
214
|
#
|
199
215
|
# e.g.
|
@@ -203,7 +219,7 @@ module GoogleSpreadsheet
|
|
203
219
|
url = "https://spreadsheets.google.com/feeds/worksheets/#{key}/private/full"
|
204
220
|
return Spreadsheet.new(self, url)
|
205
221
|
end
|
206
|
-
|
222
|
+
|
207
223
|
# Returns GoogleSpreadsheet::Spreadsheet with given +url+. You must specify either of:
|
208
224
|
# - URL of the page you open to access the spreadsheet in your browser
|
209
225
|
# - URL of worksheet-based feed of the spreadseet
|
@@ -224,7 +240,7 @@ module GoogleSpreadsheet
|
|
224
240
|
# Assumes the URL is worksheets feed URL.
|
225
241
|
return Spreadsheet.new(self, url)
|
226
242
|
end
|
227
|
-
|
243
|
+
|
228
244
|
# Returns GoogleSpreadsheet::Worksheet with given +url+.
|
229
245
|
# You must specify URL of cell-based feed of the worksheet.
|
230
246
|
#
|
@@ -234,7 +250,7 @@ module GoogleSpreadsheet
|
|
234
250
|
def worksheet_by_url(url)
|
235
251
|
return Worksheet.new(self, nil, url)
|
236
252
|
end
|
237
|
-
|
253
|
+
|
238
254
|
# Creates new spreadsheet and returns the new GoogleSpreadsheet::Spreadsheet.
|
239
255
|
#
|
240
256
|
# e.g.
|
@@ -255,7 +271,7 @@ module GoogleSpreadsheet
|
|
255
271
|
"link[@rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']").first['href']
|
256
272
|
return Spreadsheet.new(self, ss_url, title)
|
257
273
|
end
|
258
|
-
|
274
|
+
|
259
275
|
def request(method, url, params = {}) #:nodoc:
|
260
276
|
# Always uses HTTPS.
|
261
277
|
url = url.gsub(%r{^http://}, "https://")
|
@@ -269,7 +285,7 @@ module GoogleSpreadsheet
|
|
269
285
|
extra_header = {}
|
270
286
|
end
|
271
287
|
response_type = params[:response_type] || :xml
|
272
|
-
|
288
|
+
|
273
289
|
while true
|
274
290
|
response = request_raw(method, url, data, extra_header, auth)
|
275
291
|
if response.code == "401" && @on_auth_fail && @on_auth_fail.call()
|
@@ -284,9 +300,9 @@ module GoogleSpreadsheet
|
|
284
300
|
return convert_response(response, response_type)
|
285
301
|
end
|
286
302
|
end
|
287
|
-
|
303
|
+
|
288
304
|
private
|
289
|
-
|
305
|
+
|
290
306
|
def request_raw(method, url, data, extra_header, auth)
|
291
307
|
if @oauth_token
|
292
308
|
if method == :delete || method == :get
|
@@ -296,7 +312,7 @@ module GoogleSpreadsheet
|
|
296
312
|
end
|
297
313
|
else
|
298
314
|
uri = URI.parse(url)
|
299
|
-
http =
|
315
|
+
http = @proxy.new(uri.host, uri.port)
|
300
316
|
http.use_ssl = true
|
301
317
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
302
318
|
http.start() do
|
@@ -310,7 +326,7 @@ module GoogleSpreadsheet
|
|
310
326
|
end
|
311
327
|
end
|
312
328
|
end
|
313
|
-
|
329
|
+
|
314
330
|
def convert_response(response, response_type)
|
315
331
|
case response_type
|
316
332
|
when :xml
|
@@ -321,7 +337,7 @@ module GoogleSpreadsheet
|
|
321
337
|
raise("unknown params[:response_type]: %s" % response_type)
|
322
338
|
end
|
323
339
|
end
|
324
|
-
|
340
|
+
|
325
341
|
def authenticate(mail, password, auth)
|
326
342
|
params = {
|
327
343
|
"accountType" => "HOSTED_OR_GOOGLE",
|
@@ -336,28 +352,28 @@ module GoogleSpreadsheet
|
|
336
352
|
:data => encode_query(params), :auth => :none, :header => header, :response_type => :raw)
|
337
353
|
@auth_tokens[auth] = response.slice(/^Auth=(.*)$/, 1)
|
338
354
|
end
|
339
|
-
|
355
|
+
|
340
356
|
end
|
341
|
-
|
342
|
-
|
357
|
+
|
358
|
+
|
343
359
|
# Use methods in GoogleSpreadsheet::Session to get GoogleSpreadsheet::Spreadsheet object.
|
344
360
|
class Spreadsheet
|
345
|
-
|
361
|
+
|
346
362
|
include(Util)
|
347
|
-
|
363
|
+
|
348
364
|
def initialize(session, worksheets_feed_url, title = nil) #:nodoc:
|
349
365
|
@session = session
|
350
366
|
@worksheets_feed_url = worksheets_feed_url
|
351
367
|
@title = title
|
352
368
|
end
|
353
|
-
|
369
|
+
|
354
370
|
# URL of worksheet-based feed of the spreadsheet.
|
355
371
|
attr_reader(:worksheets_feed_url)
|
356
|
-
|
372
|
+
|
357
373
|
# Title of the spreadsheet. So far only available if you get this object by
|
358
374
|
# GoogleSpreadsheet::Session#spreadsheets.
|
359
375
|
attr_reader(:title)
|
360
|
-
|
376
|
+
|
361
377
|
# Key of the spreadsheet.
|
362
378
|
def key
|
363
379
|
if !(@worksheets_feed_url =~
|
@@ -367,7 +383,7 @@ module GoogleSpreadsheet
|
|
367
383
|
end
|
368
384
|
return $1
|
369
385
|
end
|
370
|
-
|
386
|
+
|
371
387
|
# Tables feed URL of the spreadsheet.
|
372
388
|
def tables_feed_url
|
373
389
|
return "https://spreadsheets.google.com/feeds/#{self.key}/tables"
|
@@ -383,7 +399,7 @@ module GoogleSpreadsheet
|
|
383
399
|
new_name ||= (@title ? "Copy of " + @title : "Untitled")
|
384
400
|
get_url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=#{key}&exportFormat=ods"
|
385
401
|
ods = @session.request(:get, get_url, :response_type => :raw)
|
386
|
-
|
402
|
+
|
387
403
|
url = "https://docs.google.com/feeds/documents/private/full"
|
388
404
|
header = {
|
389
405
|
"Content-Type" => "application/x-vnd.oasis.opendocument.spreadsheet",
|
@@ -394,7 +410,7 @@ module GoogleSpreadsheet
|
|
394
410
|
"link[@rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']").first['href']
|
395
411
|
return Spreadsheet.new(@session, ss_url, title)
|
396
412
|
end
|
397
|
-
|
413
|
+
|
398
414
|
# If +permanent+ is +false+, moves the spreadsheet to the trash.
|
399
415
|
# If +permanent+ is +true+, deletes the spreadsheet permanently.
|
400
416
|
def delete(permanent = false)
|
@@ -402,7 +418,7 @@ module GoogleSpreadsheet
|
|
402
418
|
self.document_feed_url + (permanent ? "?delete=true" : ""),
|
403
419
|
:auth => :writely, :header => {"If-Match" => "*"})
|
404
420
|
end
|
405
|
-
|
421
|
+
|
406
422
|
# Renames title of the spreadsheet.
|
407
423
|
def rename(title)
|
408
424
|
doc = @session.request(:get, self.document_feed_url)
|
@@ -420,7 +436,7 @@ module GoogleSpreadsheet
|
|
420
436
|
|
421
437
|
@session.request(:put, edit_url, :data => xml)
|
422
438
|
end
|
423
|
-
|
439
|
+
|
424
440
|
# Returns worksheets of the spreadsheet as array of GoogleSpreadsheet::Worksheet.
|
425
441
|
def worksheets
|
426
442
|
doc = @session.request(:get, @worksheets_feed_url)
|
@@ -433,7 +449,7 @@ module GoogleSpreadsheet
|
|
433
449
|
end
|
434
450
|
return result.freeze()
|
435
451
|
end
|
436
|
-
|
452
|
+
|
437
453
|
# Adds a new worksheet to the spreadsheet. Returns added GoogleSpreadsheet::Worksheet.
|
438
454
|
def add_worksheet(title, max_rows = 100, max_cols = 20)
|
439
455
|
xml = <<-"EOS"
|
@@ -449,19 +465,19 @@ module GoogleSpreadsheet
|
|
449
465
|
"link[@rel='http://schemas.google.com/spreadsheets/2006#cellsfeed']").first['href']
|
450
466
|
return Worksheet.new(@session, self, url, title)
|
451
467
|
end
|
452
|
-
|
468
|
+
|
453
469
|
# Returns list of tables in the spreadsheet.
|
454
470
|
def tables
|
455
471
|
doc = @session.request(:get, self.tables_feed_url)
|
456
472
|
return doc.css('entry').map(){ |e| Table.new(@session, e) }.freeze()
|
457
473
|
end
|
458
|
-
|
474
|
+
|
459
475
|
end
|
460
|
-
|
476
|
+
|
461
477
|
# Use GoogleSpreadsheet::Worksheet#add_table to create table.
|
462
478
|
# Use GoogleSpreadsheet::Worksheet#tables to get GoogleSpreadsheet::Table objects.
|
463
479
|
class Table
|
464
|
-
|
480
|
+
|
465
481
|
include(Util)
|
466
482
|
|
467
483
|
def initialize(session, entry) #:nodoc:
|
@@ -471,7 +487,7 @@ module GoogleSpreadsheet
|
|
471
487
|
@edit_url = entry.css("link[@rel='edit']")[0]['href']
|
472
488
|
@session = session
|
473
489
|
end
|
474
|
-
|
490
|
+
|
475
491
|
# Title of the worksheet the table belongs to.
|
476
492
|
attr_reader(:worksheet_title)
|
477
493
|
|
@@ -490,49 +506,49 @@ module GoogleSpreadsheet
|
|
490
506
|
EOS
|
491
507
|
@session.request(:post, @records_url, :data => xml)
|
492
508
|
end
|
493
|
-
|
509
|
+
|
494
510
|
# Returns records in the table.
|
495
511
|
def records
|
496
512
|
doc = @session.request(:get, @records_url)
|
497
513
|
return doc.css('entry').map(){ |e| Record.new(@session, e) }
|
498
514
|
end
|
499
|
-
|
515
|
+
|
500
516
|
# Deletes this table. Deletion takes effect right away without calling save().
|
501
517
|
def delete
|
502
518
|
@session.request(:delete, @edit_url, :header => {"If-Match" => "*"})
|
503
519
|
end
|
504
|
-
|
520
|
+
|
505
521
|
end
|
506
|
-
|
522
|
+
|
507
523
|
# Use GoogleSpreadsheet::Table#records to get GoogleSpreadsheet::Record objects.
|
508
524
|
class Record < Hash
|
509
525
|
include(Util)
|
510
|
-
|
526
|
+
|
511
527
|
def initialize(session, entry) #:nodoc:
|
512
528
|
@session = session
|
513
529
|
entry.css('gs|field').each() do |field|
|
514
530
|
self[field["name"]] = field.inner_text
|
515
531
|
end
|
516
532
|
end
|
517
|
-
|
533
|
+
|
518
534
|
def inspect #:nodoc:
|
519
535
|
content = self.map(){ |k, v| "%p => %p" % [k, v] }.join(", ")
|
520
536
|
return "\#<%p:{%s}>" % [self.class, content]
|
521
537
|
end
|
522
|
-
|
538
|
+
|
523
539
|
end
|
524
|
-
|
540
|
+
|
525
541
|
# Use GoogleSpreadsheet::Spreadsheet#worksheets to get GoogleSpreadsheet::Worksheet object.
|
526
542
|
class Worksheet
|
527
|
-
|
543
|
+
|
528
544
|
include(Util)
|
529
|
-
|
545
|
+
|
530
546
|
def initialize(session, spreadsheet, cells_feed_url, title = nil) #:nodoc:
|
531
547
|
@session = session
|
532
548
|
@spreadsheet = spreadsheet
|
533
549
|
@cells_feed_url = cells_feed_url
|
534
550
|
@title = title
|
535
|
-
|
551
|
+
|
536
552
|
@cells = nil
|
537
553
|
@input_values = nil
|
538
554
|
@modified = Set.new()
|
@@ -540,7 +556,7 @@ module GoogleSpreadsheet
|
|
540
556
|
|
541
557
|
# URL of cell-based feed of the worksheet.
|
542
558
|
attr_reader(:cells_feed_url)
|
543
|
-
|
559
|
+
|
544
560
|
# URL of worksheet feed URL of the worksheet.
|
545
561
|
def worksheet_feed_url
|
546
562
|
# I don't know good way to get worksheet feed URL from cells feed URL.
|
@@ -553,7 +569,7 @@ module GoogleSpreadsheet
|
|
553
569
|
end
|
554
570
|
return "https://spreadsheets.google.com/feeds/worksheets/#{$1}/private/full/#{$2}"
|
555
571
|
end
|
556
|
-
|
572
|
+
|
557
573
|
# GoogleSpreadsheet::Spreadsheet which this worksheet belongs to.
|
558
574
|
def spreadsheet
|
559
575
|
if !@spreadsheet
|
@@ -566,12 +582,12 @@ module GoogleSpreadsheet
|
|
566
582
|
end
|
567
583
|
return @spreadsheet
|
568
584
|
end
|
569
|
-
|
585
|
+
|
570
586
|
# Returns content of the cell as String. Top-left cell is [1, 1].
|
571
587
|
def [](row, col)
|
572
588
|
return self.cells[[row, col]] || ""
|
573
589
|
end
|
574
|
-
|
590
|
+
|
575
591
|
# Updates content of the cell.
|
576
592
|
# Note that update is not sent to the server until you call save().
|
577
593
|
# Top-left cell is [1, 1].
|
@@ -587,7 +603,7 @@ module GoogleSpreadsheet
|
|
587
603
|
self.max_rows = row if row > @max_rows
|
588
604
|
self.max_cols = col if col > @max_cols
|
589
605
|
end
|
590
|
-
|
606
|
+
|
591
607
|
# Returns the value or the formula of the cell. Top-left cell is [1, 1].
|
592
608
|
#
|
593
609
|
# If user input "=A1+B1" to cell [1, 3], worksheet[1, 3] is "3" for example and
|
@@ -596,25 +612,25 @@ module GoogleSpreadsheet
|
|
596
612
|
reload() if !@cells
|
597
613
|
return @input_values[[row, col]] || ""
|
598
614
|
end
|
599
|
-
|
615
|
+
|
600
616
|
# Row number of the bottom-most non-empty row.
|
601
617
|
def num_rows
|
602
618
|
reload() if !@cells
|
603
619
|
return @cells.keys.map(){ |r, c| r }.max || 0
|
604
620
|
end
|
605
|
-
|
621
|
+
|
606
622
|
# Column number of the right-most non-empty column.
|
607
623
|
def num_cols
|
608
624
|
reload() if !@cells
|
609
625
|
return @cells.keys.map(){ |r, c| c }.max || 0
|
610
626
|
end
|
611
|
-
|
627
|
+
|
612
628
|
# Number of rows including empty rows.
|
613
629
|
def max_rows
|
614
630
|
reload() if !@cells
|
615
631
|
return @max_rows
|
616
632
|
end
|
617
|
-
|
633
|
+
|
618
634
|
# Updates number of rows.
|
619
635
|
# Note that update is not sent to the server until you call save().
|
620
636
|
def max_rows=(rows)
|
@@ -622,13 +638,13 @@ module GoogleSpreadsheet
|
|
622
638
|
@max_rows = rows
|
623
639
|
@meta_modified = true
|
624
640
|
end
|
625
|
-
|
641
|
+
|
626
642
|
# Number of columns including empty columns.
|
627
643
|
def max_cols
|
628
644
|
reload() if !@cells
|
629
645
|
return @max_cols
|
630
646
|
end
|
631
|
-
|
647
|
+
|
632
648
|
# Updates number of columns.
|
633
649
|
# Note that update is not sent to the server until you call save().
|
634
650
|
def max_cols=(cols)
|
@@ -636,13 +652,13 @@ module GoogleSpreadsheet
|
|
636
652
|
@max_cols = cols
|
637
653
|
@meta_modified = true
|
638
654
|
end
|
639
|
-
|
655
|
+
|
640
656
|
# Title of the worksheet (shown as tab label in Web interface).
|
641
657
|
def title
|
642
658
|
reload() if !@title
|
643
659
|
return @title
|
644
660
|
end
|
645
|
-
|
661
|
+
|
646
662
|
# Updates title of the worksheet.
|
647
663
|
# Note that update is not sent to the server until you call save().
|
648
664
|
def title=(title)
|
@@ -650,12 +666,12 @@ module GoogleSpreadsheet
|
|
650
666
|
@title = title
|
651
667
|
@meta_modified = true
|
652
668
|
end
|
653
|
-
|
669
|
+
|
654
670
|
def cells #:nodoc:
|
655
671
|
reload() if !@cells
|
656
672
|
return @cells
|
657
673
|
end
|
658
|
-
|
674
|
+
|
659
675
|
# An array of spreadsheet rows. Each row contains an array of
|
660
676
|
# columns. Note that resulting array is 0-origin so
|
661
677
|
# worksheet.rows[0][0] == worksheet[1, 1].
|
@@ -666,7 +682,7 @@ module GoogleSpreadsheet
|
|
666
682
|
end
|
667
683
|
return result.freeze()
|
668
684
|
end
|
669
|
-
|
685
|
+
|
670
686
|
# Reloads content of the worksheets from the server.
|
671
687
|
# Note that changes you made by []= is discarded if you haven't called save().
|
672
688
|
def reload()
|
@@ -674,7 +690,7 @@ module GoogleSpreadsheet
|
|
674
690
|
@max_rows = doc.css('gs|rowCount').text.to_i
|
675
691
|
@max_cols = doc.css('gs|colCount').text.to_i
|
676
692
|
@title = doc.css('feed > title')[0].text
|
677
|
-
|
693
|
+
|
678
694
|
@cells = {}
|
679
695
|
@input_values = {}
|
680
696
|
doc.css('feed > entry').each() do |entry|
|
@@ -683,19 +699,19 @@ module GoogleSpreadsheet
|
|
683
699
|
col = cell["col"].to_i()
|
684
700
|
@cells[[row, col]] = cell.inner_text
|
685
701
|
@input_values[[row, col]] = cell["inputValue"]
|
686
|
-
|
702
|
+
|
687
703
|
end
|
688
704
|
@modified.clear()
|
689
705
|
@meta_modified = false
|
690
706
|
return true
|
691
707
|
end
|
692
|
-
|
708
|
+
|
693
709
|
# Saves your changes made by []=, etc. to the server.
|
694
710
|
def save()
|
695
711
|
sent = false
|
696
|
-
|
712
|
+
|
697
713
|
if @meta_modified
|
698
|
-
|
714
|
+
|
699
715
|
ws_doc = @session.request(:get, self.worksheet_feed_url)
|
700
716
|
edit_url = ws_doc.css("link[@rel='edit']").first['href']
|
701
717
|
xml = <<-"EOS"
|
@@ -706,16 +722,16 @@ module GoogleSpreadsheet
|
|
706
722
|
<gs:colCount>#{h(self.max_cols)}</gs:colCount>
|
707
723
|
</entry>
|
708
724
|
EOS
|
709
|
-
|
725
|
+
|
710
726
|
@session.request(:put, edit_url, :data => xml)
|
711
|
-
|
727
|
+
|
712
728
|
@meta_modified = false
|
713
729
|
sent = true
|
714
|
-
|
730
|
+
|
715
731
|
end
|
716
|
-
|
732
|
+
|
717
733
|
if !@modified.empty?
|
718
|
-
|
734
|
+
|
719
735
|
# Gets id and edit URL for each cell.
|
720
736
|
# Note that return-empty=true is required to get those info for empty cells.
|
721
737
|
cell_entries = {}
|
@@ -730,11 +746,11 @@ module GoogleSpreadsheet
|
|
730
746
|
col = entry.css('gs|cell').first['col'].to_i
|
731
747
|
cell_entries[[row, col]] = entry
|
732
748
|
end
|
733
|
-
|
749
|
+
|
734
750
|
# Updates cell values using batch operation.
|
735
751
|
# If the data is large, we split it into multiple operations, otherwise batch may fail.
|
736
752
|
@modified.each_slice(250) do |chunk|
|
737
|
-
|
753
|
+
|
738
754
|
xml = <<-EOS
|
739
755
|
<feed xmlns="http://www.w3.org/2005/Atom"
|
740
756
|
xmlns:batch="http://schemas.google.com/gdata/batch"
|
@@ -760,7 +776,7 @@ module GoogleSpreadsheet
|
|
760
776
|
xml << <<-"EOS"
|
761
777
|
</feed>
|
762
778
|
EOS
|
763
|
-
|
779
|
+
|
764
780
|
result = @session.request(:post, "#{@cells_feed_url}/batch", :data => xml)
|
765
781
|
result.css('atom|entry').each() do |entry|
|
766
782
|
interrupted = entry.css('batch|interrupted').first
|
@@ -773,41 +789,41 @@ module GoogleSpreadsheet
|
|
773
789
|
[entry.css('atom|id').text, entry.css('batch|status').first['reason']])
|
774
790
|
end
|
775
791
|
end
|
776
|
-
|
792
|
+
|
777
793
|
end
|
778
|
-
|
794
|
+
|
779
795
|
@modified.clear()
|
780
796
|
sent = true
|
781
|
-
|
797
|
+
|
782
798
|
end
|
783
799
|
return sent
|
784
800
|
end
|
785
|
-
|
801
|
+
|
786
802
|
# Calls save() and reload().
|
787
803
|
def synchronize()
|
788
804
|
save()
|
789
805
|
reload()
|
790
806
|
end
|
791
|
-
|
807
|
+
|
792
808
|
# Deletes this worksheet. Deletion takes effect right away without calling save().
|
793
809
|
def delete()
|
794
810
|
ws_doc = @session.request(:get, self.worksheet_feed_url)
|
795
811
|
edit_url = ws_doc.css("link[@rel='edit']").first['href']
|
796
812
|
@session.request(:delete, edit_url)
|
797
813
|
end
|
798
|
-
|
814
|
+
|
799
815
|
# Returns true if you have changes made by []= which haven't been saved.
|
800
816
|
def dirty?
|
801
817
|
return !@modified.empty?
|
802
818
|
end
|
803
|
-
|
819
|
+
|
804
820
|
# Creates table for the worksheet and returns GoogleSpreadsheet::Table.
|
805
821
|
# See this document for details:
|
806
822
|
# http://code.google.com/intl/en/apis/spreadsheets/docs/3.0/developers_guide_protocol.html#TableFeeds
|
807
823
|
def add_table(table_title, summary, columns, options)
|
808
824
|
default_options = { :header_row => 1, :num_rows => 0, :start_row => 2}
|
809
825
|
options = default_options.merge(options)
|
810
|
-
|
826
|
+
|
811
827
|
column_xml = ""
|
812
828
|
columns.each() do |index, name|
|
813
829
|
column_xml += "<gs:column index='#{h(index)}' name='#{h(name)}'/>\n"
|
@@ -829,7 +845,7 @@ module GoogleSpreadsheet
|
|
829
845
|
result = @session.request(:post, self.spreadsheet.tables_feed_url, :data => xml)
|
830
846
|
return Table.new(@session, result)
|
831
847
|
end
|
832
|
-
|
848
|
+
|
833
849
|
# Returns list of tables for the workwheet.
|
834
850
|
def tables
|
835
851
|
return self.spreadsheet.tables.select(){ |t| t.worksheet_title == self.title }
|
@@ -846,6 +862,6 @@ module GoogleSpreadsheet
|
|
846
862
|
end
|
847
863
|
|
848
864
|
end
|
849
|
-
|
850
|
-
|
865
|
+
|
866
|
+
|
851
867
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-spreadsheet-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Hiroshi Ichikawa
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-05-06 00:00:00 +09:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -64,7 +64,7 @@ files:
|
|
64
64
|
- README.rdoc
|
65
65
|
- lib/google_spreadsheet.rb
|
66
66
|
has_rdoc: true
|
67
|
-
homepage:
|
67
|
+
homepage: https://github.com/gimite/google-spreadsheet-ruby
|
68
68
|
licenses: []
|
69
69
|
|
70
70
|
post_install_message:
|
@@ -96,7 +96,7 @@ requirements: []
|
|
96
96
|
rubyforge_project:
|
97
97
|
rubygems_version: 1.3.7
|
98
98
|
signing_key:
|
99
|
-
specification_version:
|
99
|
+
specification_version: 3
|
100
100
|
summary: This is a library to read/write Google Spreadsheet.
|
101
101
|
test_files: []
|
102
102
|
|