google-spreadsheet-ruby 0.1.4 → 0.1.5

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.
Files changed (2) hide show
  1. data/lib/google_spreadsheet.rb +81 -20
  2. metadata +4 -4
@@ -360,6 +360,8 @@ module GoogleSpreadsheet
360
360
  class Spreadsheet
361
361
 
362
362
  include(Util)
363
+
364
+ SUPPORTED_EXPORT_FORMAT = Set.new(["xls", "csv", "pdf", "ods", "tsv", "html"])
363
365
 
364
366
  def initialize(session, worksheets_feed_url, title = nil) #:nodoc:
365
367
  @session = session
@@ -399,7 +401,8 @@ module GoogleSpreadsheet
399
401
  #
400
402
  # e.g. "http://spreadsheets.google.com/ccc?key=pz7XtlQC-PYx-jrVMJErTcg"
401
403
  def human_url
402
- return self.spreadsheet_feed_entry.css("link[@rel='alternate']").first["href"]
404
+ # Uses Document feed because Spreadsheet feed returns wrong URL for Apps account.
405
+ return self.document_feed_entry.css("link[@rel='alternate']").first["href"]
403
406
  end
404
407
 
405
408
  # DEPRECATED: Table and Record feeds are deprecated and they will not be available after
@@ -407,6 +410,9 @@ module GoogleSpreadsheet
407
410
  #
408
411
  # Tables feed URL of the spreadsheet.
409
412
  def tables_feed_url
413
+ warn(
414
+ "DEPRECATED: Google Spreadsheet Table and Record feeds are deprecated and they will " +
415
+ "not be available after March 2012.")
410
416
  return "https://spreadsheets.google.com/feeds/#{self.key}/tables"
411
417
  end
412
418
 
@@ -420,28 +426,40 @@ module GoogleSpreadsheet
420
426
  # Set params[:reload] to true to force reloading the feed.
421
427
  def spreadsheet_feed_entry(params = {})
422
428
  if !@spreadsheet_feed_entry || params[:reload]
423
- @spreadsheet_feed_entry = @session.request(:get, self.spreadsheet_feed_url).css("entry").first
429
+ @spreadsheet_feed_entry =
430
+ @session.request(:get, self.spreadsheet_feed_url).css("entry").first
424
431
  end
425
432
  return @spreadsheet_feed_entry
426
433
  end
427
434
 
428
- # Creates copy of this spreadsheet with the given name.
429
- def duplicate(new_name = nil)
430
- new_name ||= (@title ? "Copy of " + @title : "Untitled")
431
- get_url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=#{key}&exportFormat=ods"
432
- ods = @session.request(:get, get_url, :response_type => :raw)
433
-
434
- url = "https://docs.google.com/feeds/documents/private/full"
435
- header = {
436
- "Content-Type" => "application/x-vnd.oasis.opendocument.spreadsheet",
437
- "Slug" => URI.encode(new_name),
438
- }
439
- doc = @session.request(:post, url, :data => ods, :auth => :writely, :header => header)
435
+ # <entry> element of document list feed as Nokogiri::XML::Element.
436
+ #
437
+ # Set params[:reload] to true to force reloading the feed.
438
+ def document_feed_entry(params = {})
439
+ if !@document_feed_entry || params[:reload]
440
+ @document_feed_entry =
441
+ @session.request(:get, self.document_feed_url, :auth => :writely).css("entry").first
442
+ end
443
+ return @document_feed_entry
444
+ end
445
+
446
+ # Creates copy of this spreadsheet with the given title.
447
+ def duplicate(new_title = nil)
448
+ new_title ||= (self.title ? "Copy of " + self.title : "Untitled")
449
+ post_url = "https://docs.google.com/feeds/default/private/full/"
450
+ header = {"GData-Version" => "3.0", "Content-Type" => "application/atom+xml"}
451
+ xml = <<-"EOS"
452
+ <entry xmlns='http://www.w3.org/2005/Atom'>
453
+ <id>#{h(self.document_feed_url)}</id>
454
+ <title>#{h(new_title)}</title>
455
+ </entry>
456
+ EOS
457
+ doc = @session.request(:post, post_url, :data => xml, :header => header, :auth => :writely)
440
458
  ss_url = doc.css(
441
- "link[@rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']").first["href"]
442
- return Spreadsheet.new(@session, ss_url, title)
459
+ "link[@rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']").first["href"]
460
+ return Spreadsheet.new(@session, ss_url, new_title)
443
461
  end
444
-
462
+
445
463
  # If +permanent+ is +false+, moves the spreadsheet to the trash.
446
464
  # If +permanent+ is +true+, deletes the spreadsheet permanently.
447
465
  def delete(permanent = false)
@@ -452,7 +470,7 @@ module GoogleSpreadsheet
452
470
 
453
471
  # Renames title of the spreadsheet.
454
472
  def rename(title)
455
- doc = @session.request(:get, self.document_feed_url)
473
+ doc = @session.request(:get, self.document_feed_url, :auth => :writely)
456
474
  edit_url = doc.css("link[@rel='edit']").first["href"]
457
475
  xml = <<-"EOS"
458
476
  <atom:entry
@@ -465,9 +483,43 @@ module GoogleSpreadsheet
465
483
  </atom:entry>
466
484
  EOS
467
485
 
468
- @session.request(:put, edit_url, :data => xml)
486
+ @session.request(:put, edit_url, :data => xml, :auth => :writely)
469
487
  end
470
-
488
+
489
+ alias title= rename
490
+
491
+ # Exports the spreadsheet in +format+ and returns it as String.
492
+ #
493
+ # +format+ can be either "xls", "csv", "pdf", "ods", "tsv" or "html".
494
+ # In format such as "csv", only the worksheet specified with +worksheet_index+ is exported.
495
+ def export_as_string(format, worksheet_index = nil)
496
+ gid_param = worksheet_index ? "&gid=#{worksheet_index}" : ""
497
+ url =
498
+ "https://spreadsheets.google.com/feeds/download/spreadsheets/Export" +
499
+ "?key=#{key}&exportFormat=#{format}#{gid_param}"
500
+ return @session.request(:get, url, :response_type => :raw)
501
+ end
502
+
503
+ # Exports the spreadsheet in +format+ as a local file.
504
+ #
505
+ # +format+ can be either "xls", "csv", "pdf", "ods", "tsv" or "html".
506
+ # If +format+ is nil, it is guessed from the file name.
507
+ # In format such as "csv", only the worksheet specified with +worksheet_index+ is exported.
508
+ def export_as_file(local_path, format = nil, worksheet_index = nil)
509
+ if !format
510
+ format = File.extname(local_path).gsub(/^\./, "")
511
+ if !SUPPORTED_EXPORT_FORMAT.include?(format)
512
+ raise(ArgumentError,
513
+ ("Cannot guess format from the file name: %s\n" +
514
+ "Specify format argument explicitly") %
515
+ local_path)
516
+ end
517
+ end
518
+ open(local_path, "wb") do |f|
519
+ f.write(export_as_string(format, worksheet_index))
520
+ end
521
+ end
522
+
471
523
  # Returns worksheets of the spreadsheet as array of GoogleSpreadsheet::Worksheet.
472
524
  def worksheets
473
525
  doc = @session.request(:get, @worksheets_feed_url)
@@ -502,6 +554,9 @@ module GoogleSpreadsheet
502
554
  #
503
555
  # Returns list of tables in the spreadsheet.
504
556
  def tables
557
+ warn(
558
+ "DEPRECATED: Google Spreadsheet Table and Record feeds are deprecated and they will " +
559
+ "not be available after March 2012.")
505
560
  doc = @session.request(:get, self.tables_feed_url)
506
561
  return doc.css('entry').map(){ |e| Table.new(@session, e) }.freeze()
507
562
  end
@@ -857,10 +912,16 @@ module GoogleSpreadsheet
857
912
  return !@modified.empty?
858
913
  end
859
914
 
915
+ # DEPRECATED: Table and Record feeds are deprecated and they will not be available after
916
+ # March 2012.
917
+ #
860
918
  # Creates table for the worksheet and returns GoogleSpreadsheet::Table.
861
919
  # See this document for details:
862
920
  # http://code.google.com/intl/en/apis/spreadsheets/docs/3.0/developers_guide_protocol.html#TableFeeds
863
921
  def add_table(table_title, summary, columns, options)
922
+ warn(
923
+ "DEPRECATED: Google Spreadsheet Table and Record feeds are deprecated and they will " +
924
+ "not be available after March 2012.")
864
925
  default_options = { :header_row => 1, :num_rows => 0, :start_row => 2}
865
926
  options = default_options.merge(options)
866
927
 
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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
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: 2011-05-12 00:00:00 +09:00
18
+ date: 2011-05-14 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency