Class: GoogleSpreadsheet::Spreadsheet

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/google_spreadsheet.rb

Overview

Use methods in GoogleSpreadsheet::Session to get GoogleSpreadsheet::Spreadsheet object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

as_utf8, encode_query, h

Constructor Details

#initialize(session, worksheets_feed_url, title = nil) ⇒ Spreadsheet

:nodoc:



358
359
360
361
362
# File 'lib/google_spreadsheet.rb', line 358

def initialize(session, worksheets_feed_url, title = nil) #:nodoc:
  @session = session
  @worksheets_feed_url = worksheets_feed_url
  @title = title
end

Instance Attribute Details

#titleObject (readonly)

Title of the spreadsheet. So far only available if you get this object by GoogleSpreadsheet::Session#spreadsheets.



369
370
371
# File 'lib/google_spreadsheet.rb', line 369

def title
  @title
end

#worksheets_feed_urlObject (readonly)

URL of worksheet-based feed of the spreadsheet.



365
366
367
# File 'lib/google_spreadsheet.rb', line 365

def worksheets_feed_url
  @worksheets_feed_url
end

Instance Method Details

#add_worksheet(title, max_rows = 100, max_cols = 20) ⇒ Object

Adds a new worksheet to the spreadsheet. Returns added GoogleSpreadsheet::Worksheet.



430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/google_spreadsheet.rb', line 430

def add_worksheet(title, max_rows = 100, max_cols = 20)
  xml = <<-"EOS"
    <entry xmlns='http://www.w3.org/2005/Atom'
           xmlns:gs='http://schemas.google.com/spreadsheets/2006'>
      <title>#{h(title)}</title>
      <gs:rowCount>#{h(max_rows)}</gs:rowCount>
      <gs:colCount>#{h(max_cols)}</gs:colCount>
    </entry>
  EOS
  doc = @session.request(:post, @worksheets_feed_url, :data => xml)
  url = as_utf8(doc.search(
    "link[@rel='http://schemas.google.com/spreadsheets/2006#cellsfeed']")[0]["href"])
  return Worksheet.new(@session, self, url, title)
end

#delete(permanent = false) ⇒ Object

If permanent is false, moves the spreadsheet to the trash. If permanent is true, deletes the spreadsheet permanently.



410
411
412
413
414
# File 'lib/google_spreadsheet.rb', line 410

def delete(permanent = false)
  @session.request(:delete,
    self.document_feed_url + (permanent ? "?delete=true" : ""),
    :auth => :writely, :header => {"If-Match" => "*"})
end

#document_feed_urlObject

URL of feed used in document list feed API.



387
388
389
# File 'lib/google_spreadsheet.rb', line 387

def document_feed_url
  return "https://docs.google.com/feeds/default/private/full/spreadsheet%3A#{self.key}"
end

#duplicate(new_name = nil) ⇒ Object

Creates copy of this spreadsheet with the given name.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/google_spreadsheet.rb', line 392

def duplicate(new_name = nil)
  new_name ||= (@title ? "Copy of " + @title : "Untitled")
  get_url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=#{key}&exportFormat=ods"
  ods = @session.request(:get, get_url, :response_type => :raw)
  
  url = "https://docs.google.com/feeds/default/private/full"
  header = {
    "Content-Type" => "application/x-vnd.oasis.opendocument.spreadsheet",
    "Slug" => URI.encode(new_name),
  }
  doc = @session.request(:post, url, :data => ods, :auth => :writely, :header => header)
  ss_url = as_utf8(doc.search(
    "link[@rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']")[0]["href"])
  return Spreadsheet.new(@session, ss_url, title)
end

#keyObject

Key of the spreadsheet.



372
373
374
375
376
377
378
379
# File 'lib/google_spreadsheet.rb', line 372

def key
  if !(@worksheets_feed_url =~
      %r{^https?://spreadsheets.google.com/feeds/worksheets/(.*)/private/full$})
    raise(GoogleSpreadsheet::Error,
      "worksheets feed URL is in unknown format: #{@worksheets_feed_url}")
  end
  return $1
end

#tablesObject

Returns list of tables in the spreadsheet.



446
447
448
449
# File 'lib/google_spreadsheet.rb', line 446

def tables
  doc = @session.request(:get, self.tables_feed_url)
  return doc.search("entry").map(){ |e| Table.new(@session, e) }.freeze()
end

#tables_feed_urlObject

Tables feed URL of the spreadsheet.



382
383
384
# File 'lib/google_spreadsheet.rb', line 382

def tables_feed_url
  return "https://spreadsheets.google.com/feeds/#{self.key}/tables"
end

#worksheetsObject

Returns worksheets of the spreadsheet as array of GoogleSpreadsheet::Worksheet.



417
418
419
420
421
422
423
424
425
426
427
# File 'lib/google_spreadsheet.rb', line 417

def worksheets
  doc = @session.request(:get, @worksheets_feed_url)
  result = []
  for entry in doc.search("entry")
    title = as_utf8(entry.search("title").text)
    url = as_utf8(entry.search(
      "link[@rel='http://schemas.google.com/spreadsheets/2006#cellsfeed']")[0]["href"])
    result.push(Worksheet.new(@session, self, url, title))
  end
  return result.freeze()
end