gimite-google-spreadsheet-ruby 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/google_spreadsheet.rb +128 -7
  2. metadata +2 -2
@@ -236,7 +236,7 @@ module GoogleSpreadsheet
236
236
  # session.worksheet_by_url(
237
237
  # "http://spreadsheets.google.com/feeds/cells/pz7XtlQC-PYxNmbBVgyiNWg/od6/private/full")
238
238
  def worksheet_by_url(url)
239
- return Worksheet.new(self, url)
239
+ return Worksheet.new(self, nil, url)
240
240
  end
241
241
 
242
242
  end
@@ -260,16 +260,30 @@ module GoogleSpreadsheet
260
260
  # GoogleSpreadsheet::Session#spreadsheets.
261
261
  attr_reader(:title)
262
262
 
263
+ # Key of the spreadsheet.
264
+ def key
265
+ if !(@worksheets_feed_url =~
266
+ %r{http://spreadsheets.google.com/feeds/worksheets/(.*)/private/full})
267
+ raise(GoogleSpreadsheet::Error,
268
+ "worksheets feed URL is in unknown format: #{@worksheets_feed_url}")
269
+ end
270
+ return $1
271
+ end
272
+
273
+ # Tables feed URL of the spreadsheet.
274
+ def tables_feed_url
275
+ return "http://spreadsheets.google.com/feeds/#{self.key}/tables"
276
+ end
277
+
263
278
  # Returns worksheets of the spreadsheet as array of GoogleSpreadsheet::Worksheet.
264
279
  def worksheets
265
280
  doc = @session.get(@worksheets_feed_url)
266
281
  result = []
267
282
  for entry in doc.search("entry")
268
283
  title = entry.search("title").text
269
-
270
284
  url = entry.search(
271
285
  "link[@rel='http://schemas.google.com/spreadsheets/2006#cellsfeed']")[0]["href"]
272
- result.push(Worksheet.new(@session, url, title))
286
+ result.push(Worksheet.new(@session, self, url, title))
273
287
  end
274
288
  return result.freeze()
275
289
  end
@@ -287,27 +301,90 @@ module GoogleSpreadsheet
287
301
  doc = @session.post(@worksheets_feed_url, xml)
288
302
  url = doc.search(
289
303
  "link[@rel='http://schemas.google.com/spreadsheets/2006#cellsfeed']")[0]["href"]
290
- return Worksheet.new(@session, url, title)
304
+ return Worksheet.new(@session, self, url, title)
305
+ end
306
+
307
+ # Returns list of tables in the spreadsheet.
308
+ def tables
309
+ doc = @session.get(self.tables_feed_url)
310
+ return doc.search("entry").map(){ |e| Table.new(@session, e) }.freeze()
291
311
  end
292
312
 
293
313
  end
294
314
 
315
+ # Use GoogleSpreadsheet::Worksheet#add_table to create table.
316
+ # Use GoogleSpreadsheet::Worksheet#tables to get GoogleSpreadsheet::Table objects.
317
+ class Table
318
+
319
+ include(Util)
320
+
321
+ def initialize(session, entry) #:nodoc:
322
+ @columns = {}
323
+ @worksheet_title = entry.search("gs:worksheet")[0]["name"]
324
+ @records_url = entry.search("content")[0]["src"]
325
+ @session = session
326
+ end
327
+
328
+ # Title of the worksheet the table belongs to.
329
+ attr_reader(:worksheet_title)
330
+
331
+ # Adds a record.
332
+ def add_record(values)
333
+ fields = ""
334
+ values.each do |name, value|
335
+ fields += "<gs:field name='#{h(name)}'>#{h(value)}</gs:field>"
336
+ end
337
+ xml =<<-EOS
338
+ <entry
339
+ xmlns="http://www.w3.org/2005/Atom"
340
+ xmlns:gs="http://schemas.google.com/spreadsheets/2006">
341
+ #{fields}
342
+ </entry>
343
+ EOS
344
+ @session.post(@records_url, xml)
345
+ end
346
+
347
+ # Returns records in the table.
348
+ def records
349
+ doc = @session.get(@records_url)
350
+ return doc.search("entry").map(){ |e| Record.new(@session, e) }
351
+ end
352
+
353
+ end
354
+
355
+ # Use GoogleSpreadsheet::Table#records to get GoogleSpreadsheet::Record objects.
356
+ class Record < Hash
357
+
358
+ def initialize(session, entry) #:nodoc:
359
+ @session = session
360
+ for field in entry.search('gs:field')
361
+ self[field["name"]] = field.inner_text
362
+ end
363
+ end
364
+
365
+ def inspect #:nodoc:
366
+ content = self.map(){ |k, v| "%p => %p" % [k, v] }.join(", ")
367
+ return "\#<%p:{%s}>" % [self.class, content]
368
+ end
369
+
370
+ end
295
371
 
296
372
  # Use GoogleSpreadsheet::Spreadsheet#worksheets to get GoogleSpreadsheet::Worksheet object.
297
373
  class Worksheet
298
374
 
299
375
  include(Util)
300
376
 
301
- def initialize(session, cells_feed_url, title = nil) #:nodoc:
377
+ def initialize(session, spreadsheet, cells_feed_url, title = nil) #:nodoc:
302
378
  @session = session
379
+ @spreadsheet = spreadsheet
303
380
  @cells_feed_url = cells_feed_url
304
381
  @title = title
305
-
382
+
306
383
  @cells = nil
307
384
  @input_values = nil
308
385
  @modified = Set.new()
309
386
  end
310
-
387
+
311
388
  # URL of cell-based feed of the worksheet.
312
389
  attr_reader(:cells_feed_url)
313
390
 
@@ -324,6 +401,19 @@ module GoogleSpreadsheet
324
401
  return "http://spreadsheets.google.com/feeds/worksheets/#{$1}/private/full/#{$2}"
325
402
  end
326
403
 
404
+ # GoogleSpreadsheet::Spreadsheet which this worksheet belongs to.
405
+ def spreadsheet
406
+ if !@spreadsheet
407
+ if !(@cells_feed_url =~
408
+ %r{^http://spreadsheets.google.com/feeds/cells/(.*)/(.*)/private/full$})
409
+ raise(GoogleSpreadsheet::Error,
410
+ "cells feed URL is in unknown format: #{@cells_feed_url}")
411
+ end
412
+ @spreadsheet = @session.spreadsheet_by_key($1)
413
+ end
414
+ return @spreadsheet
415
+ end
416
+
327
417
  # Returns content of the cell as String. Top-left cell is [1, 1].
328
418
  def [](row, col)
329
419
  return self.cells[[row, col]] || ""
@@ -548,6 +638,37 @@ module GoogleSpreadsheet
548
638
  return !@modified.empty?
549
639
  end
550
640
 
641
+ # Creates table for the worksheet and returns GoogleSpreadsheet::Table.
642
+ # See this document for details:
643
+ # http://code.google.com/intl/en/apis/spreadsheets/docs/3.0/developers_guide_protocol.html#TableFeeds
644
+ def add_table(table_title, summary, columns)
645
+ column_xml = ""
646
+ columns.each do |index, name|
647
+ column_xml += "<gs:column index='#{h(index)}' name='#{h(name)}'/>\n"
648
+ end
649
+
650
+ xml = <<-"EOS"
651
+ <entry xmlns="http://www.w3.org/2005/Atom"
652
+ xmlns:gs="http://schemas.google.com/spreadsheets/2006">
653
+ <title type='text'>#{h(table_title)}</title>
654
+ <summary type='text'>#{h(summary)}</summary>
655
+ <gs:worksheet name='#{h(self.title)}' />
656
+ <gs:header row='1' />
657
+ <gs:data numRows='0' startRow='2'>
658
+ #{column_xml}
659
+ </gs:data>
660
+ </entry>
661
+ EOS
662
+
663
+ result = @session.post(self.spreadsheet.tables_feed_url, xml)
664
+ return Table.new(@session, result)
665
+ end
666
+
667
+ # Returns list of tables for the workwheet.
668
+ def tables
669
+ return self.spreadsheet.tables.select(){ |t| t.worksheet_title == self.title }
670
+ end
671
+
551
672
  end
552
673
 
553
674
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gimite-google-spreadsheet-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Ichikawa
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-05 00:00:00 -07:00
12
+ date: 2009-09-13 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency