Class: GoogleDrive::Spreadsheet

Inherits:
File
  • Object
show all
Includes:
Util
Defined in:
lib/google_drive/spreadsheet.rb

Overview

A spreadsheet.

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

Constant Summary collapse

SUPPORTED_EXPORT_FORMAT =
Set.new(["xlsx", "csv", "pdf"])

Constants included from Util

Util::EXT_TO_CONTENT_TYPE

Instance Attribute Summary

Attributes inherited from File

#api_file

Instance Method Summary collapse

Methods included from Util

concat_url, construct_and_query, construct_query, convert_params, delegate_api_methods, encode_query, get_singleton_class, h, new_upload_io

Methods inherited from File

#acl, #acl_feed_url, #available_content_types, #copy, #delete, #download_to_file, #download_to_io, #download_to_string, #export_as_file, #export_as_string, #export_to_io, #human_url, #initialize, #inspect, #reload_metadata, #rename, #resource_id, #resource_type, #title, #update_from_file, #update_from_io, #update_from_media, #update_from_string

Constructor Details

This class inherits a constructor from GoogleDrive::File

Instance Method Details

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

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/google_drive/spreadsheet.rb', line 73

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, self.worksheets_feed_url, :data => xml)
  return Worksheet.new(@session, self, doc.root)
end

#document_feed_urlObject

URL of feed used in the deprecated document list feed API.



36
37
38
# File 'lib/google_drive/spreadsheet.rb', line 36

def document_feed_url
  return "https://docs.google.com/feeds/documents/private/full/" + CGI.escape(self.resource_id)
end

#keyObject

Key of the spreadsheet.



25
26
27
# File 'lib/google_drive/spreadsheet.rb', line 25

def key
  return self.id
end

#spreadsheet_feed_urlObject

Spreadsheet feed URL of the spreadsheet.



41
42
43
# File 'lib/google_drive/spreadsheet.rb', line 41

def spreadsheet_feed_url
  return "https://spreadsheets.google.com/feeds/spreadsheets/private/full/" + self.id
end

#worksheet_by_gid(gid) ⇒ Object

Returns a GoogleDrive::Worksheet with the given gid.

Returns nil if not found.



67
68
69
70
# File 'lib/google_drive/spreadsheet.rb', line 67

def worksheet_by_gid(gid)
  gid = gid.to_s()
  return self.worksheets.find(){ |ws| ws.gid == gid }
end

#worksheet_by_title(title) ⇒ Object

Returns a GoogleDrive::Worksheet with the given title in the spreadsheet.

Returns nil if not found. Returns the first one when multiple worksheets with the title are found.



60
61
62
# File 'lib/google_drive/spreadsheet.rb', line 60

def worksheet_by_title(title)
  return self.worksheets.find(){ |ws| ws.title == title }
end

#worksheetsObject

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



46
47
48
49
50
51
52
53
54
# File 'lib/google_drive/spreadsheet.rb', line 46

def worksheets
  doc = @session.request(:get, self.worksheets_feed_url)
  if doc.root.name != "feed"
    raise(GoogleDrive::Error,
        "%s doesn't look like a worksheets feed URL because its root is not <feed>." %
        self.worksheets_feed_url)
  end
  return doc.css("entry").map(){ |e| Worksheet.new(@session, self, e) }.freeze
end

#worksheets_feed_urlObject

URL of worksheet-based feed of the spreadsheet.



30
31
32
33
# File 'lib/google_drive/spreadsheet.rb', line 30

def worksheets_feed_url
  return "https://spreadsheets.google.com/feeds/worksheets/%s/private/full" %
      self.id
end