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, http_request, uri_encode

Constructor Details

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

:nodoc:



275
276
277
278
279
# File 'lib/google_spreadsheet.rb', line 275

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.



286
287
288
# File 'lib/google_spreadsheet.rb', line 286

def title
  @title
end

#worksheets_feed_urlObject (readonly)

URL of worksheet-based feed of the spreadsheet.



282
283
284
# File 'lib/google_spreadsheet.rb', line 282

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.



317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/google_spreadsheet.rb', line 317

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

#keyObject

Key of the spreadsheet.



289
290
291
292
293
294
295
296
# File 'lib/google_spreadsheet.rb', line 289

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.



333
334
335
336
# File 'lib/google_spreadsheet.rb', line 333

def tables
  doc = @session.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.



299
300
301
# File 'lib/google_spreadsheet.rb', line 299

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.



304
305
306
307
308
309
310
311
312
313
314
# File 'lib/google_spreadsheet.rb', line 304

def worksheets
  doc = @session.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