Class: Google::Spreadsheet

Inherits:
Object
  • Object
show all
Defined in:
lib/google/spreadsheet.rb

Overview

Interface to the GData API.

You shouldn’t have to use this class in your code, except when debugging!

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Spreadsheet

Returns a new instance of Spreadsheet.



18
19
20
# File 'lib/google/spreadsheet.rb', line 18

def initialize(key)
  @key = key
end

Instance Method Details

#add_row(xml, worksheet_id = 1) ⇒ Object



33
34
35
# File 'lib/google/spreadsheet.rb', line 33

def add_row(xml, worksheet_id = 1)
  request_with_error_handling :post, :list, "#{LIST_BASE_URL}/#{@key}/#{worksheet_id}/private/full", xml.to_xml
end

#row_data(field, value, worksheet_id = 1) ⇒ Object



22
23
24
25
26
27
# File 'lib/google/spreadsheet.rb', line 22

def row_data(field, value, worksheet_id = 1)
  request_with_error_handling(:get, :list, "#{LIST_BASE_URL}/#{@key}/#{worksheet_id}/private/full?sq=#{CGI.escape(field)}=#{CGI.escape(value)}") do |response|
    xml = Nokogiri::XML.parse(response.body)
    xml.css('entry').first
  end
end

#update_row(xml, worksheet_id = 1) ⇒ Object



29
30
31
# File 'lib/google/spreadsheet.rb', line 29

def update_row(xml, worksheet_id = 1)
  request_with_error_handling :put, :list, xml.css("link[rel = edit]").first["href"], xml.to_xml
end

#worksheet_id_for(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/google/spreadsheet.rb', line 37

def worksheet_id_for(name)
  request_with_error_handling(:get, :worksheet, "#{WORKSHEET_BASE_URL}/#{@key}/private/full") do |response|
    xml = Nokogiri::XML.parse(response.body)

    # translate converts the attribute's content to lower case,
    # therefore allowing us to match case-insensitive
    xml.xpath(".//atom:entry[translate(
        atom:title,
        'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        'abcdefghijklmnopqrstuvwxyz'
      ) = '#{name.downcase}']/atom:id",
      "atom" => "http://www.w3.org/2005/Atom"
    ).text.split("/").last
  end
end