Class: GoogleDrive::Spreadsheet

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

Overview

A spreadsheet.

e.g., Use methods spreadsheet_by_title, spreadsheet_by_url, create_spreadsheet in GoogleDrive::Session to get GoogleDrive::Spreadsheet object.

Constant Summary collapse

SUPPORTED_EXPORT_FORMAT =
Set.new(%w[xlsx csv pdf])

Constants included from Util

Util::EXT_TO_CONTENT_TYPE, Util::IMPORTABLE_CONTENT_TYPE_MAP

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

Methods inherited from File

#acl, #acl_feed_url, #available_content_types, #copy, #delete, #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_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.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/google_drive/spreadsheet.rb', line 78

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)
  Worksheet.new(@session, self, doc.root)
end

#document_feed_urlObject

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



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

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

#download_to_file(_path, _params = {}) ⇒ Object

Not available for GoogleDrive::Spreadsheet. Use export_as_file instead.

Raises:

  • (NotImplementedError)


92
93
94
95
96
97
98
# File 'lib/google_drive/spreadsheet.rb', line 92

def download_to_file(_path, _params = {})
  raise(
    NotImplementedError,
    'download_to_file is not available for GoogleDrive::Spreadsheet. ' \
    'Use export_as_file instead.'
  )
end

#download_to_io(_io, _params = {}) ⇒ Object

Not available for GoogleDrive::Spreadsheet. Use export_to_io instead.

Raises:

  • (NotImplementedError)


110
111
112
113
114
115
116
# File 'lib/google_drive/spreadsheet.rb', line 110

def download_to_io(_io, _params = {})
  raise(
    NotImplementedError,
    'download_to_io is not available for GoogleDrive::Spreadsheet. ' \
    'Use export_to_io instead.'
  )
end

#download_to_string(_params = {}) ⇒ Object

Not available for GoogleDrive::Spreadsheet. Use export_as_string instead.

Raises:

  • (NotImplementedError)


101
102
103
104
105
106
107
# File 'lib/google_drive/spreadsheet.rb', line 101

def download_to_string(_params = {})
  raise(
    NotImplementedError,
    'download_to_string is not available for GoogleDrive::Spreadsheet. ' \
    'Use export_as_string instead.'
  )
end

#keyObject

Key of the spreadsheet.



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

def key
  id
end

#spreadsheet_feed_urlObject

Spreadsheet feed URL of the spreadsheet.



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

def spreadsheet_feed_url
  'https://spreadsheets.google.com/feeds/spreadsheets/private/full/' + id
end

#worksheet_by_gid(gid) ⇒ Object

Returns a GoogleDrive::Worksheet with the given gid.

Returns nil if not found.



71
72
73
74
# File 'lib/google_drive/spreadsheet.rb', line 71

def worksheet_by_gid(gid)
  gid = gid.to_s
  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.



64
65
66
# File 'lib/google_drive/spreadsheet.rb', line 64

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

#worksheetsObject

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



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/google_drive/spreadsheet.rb', line 47

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

#worksheets_feed_urlObject

URL of worksheet-based feed of the spreadsheet.



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

def worksheets_feed_url
  format(
    'https://spreadsheets.google.com/feeds/worksheets/%s/private/full', id
  )
end