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:



346
347
348
349
350
# File 'lib/google_spreadsheet.rb', line 346

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.



357
358
359
# File 'lib/google_spreadsheet.rb', line 357

def title
  @title
end

#worksheets_feed_urlObject (readonly)

URL of worksheet-based feed of the spreadsheet.



353
354
355
# File 'lib/google_spreadsheet.rb', line 353

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.



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

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.



398
399
400
401
402
# File 'lib/google_spreadsheet.rb', line 398

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.



375
376
377
# File 'lib/google_spreadsheet.rb', line 375

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

#duplicate(new_name = nil) ⇒ Object

Creates copy of this spreadsheet with the given name.



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/google_spreadsheet.rb', line 380

def duplicate(new_name = nil)
  new_name ||= (@title ? "Copy of " + @title : "Untitled")
  get_url = "http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=#{key}&exportFormat=ods"
  ods = @session.request(:get, get_url, :response_type => :raw)
  
  url = "http://docs.google.com/feeds/documents/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.



360
361
362
363
364
365
366
367
# File 'lib/google_spreadsheet.rb', line 360

def key
  if !(@worksheets_feed_url =~
      %r{http://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.



434
435
436
437
# File 'lib/google_spreadsheet.rb', line 434

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.



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

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

#worksheetsObject

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



405
406
407
408
409
410
411
412
413
414
415
# File 'lib/google_spreadsheet.rb', line 405

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