faset over fra Z3950 til google books
This commit is contained in:
0
python/gdata/spreadsheets/__init__.py
Normal file
0
python/gdata/spreadsheets/__init__.py
Normal file
452
python/gdata/spreadsheets/client.py
Normal file
452
python/gdata/spreadsheets/client.py
Normal file
@@ -0,0 +1,452 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2009 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
"""Contains a client to communicate with the Google Spreadsheets servers.
|
||||
|
||||
For documentation on the Spreadsheets API, see:
|
||||
http://code.google.com/apis/spreadsheets/
|
||||
"""
|
||||
|
||||
|
||||
__author__ = 'j.s@google.com (Jeff Scudder)'
|
||||
|
||||
|
||||
import gdata.client
|
||||
import gdata.gauth
|
||||
import gdata.spreadsheets.data
|
||||
import atom.data
|
||||
import atom.http_core
|
||||
|
||||
|
||||
SPREADSHEETS_URL = ('https://spreadsheets.google.com/feeds/spreadsheets'
|
||||
'/private/full')
|
||||
WORKSHEETS_URL = ('https://spreadsheets.google.com/feeds/worksheets/'
|
||||
'%s/private/full')
|
||||
WORKSHEET_URL = ('https://spreadsheets.google.com/feeds/worksheets/'
|
||||
'%s/private/full/%s')
|
||||
TABLES_URL = 'https://spreadsheets.google.com/feeds/%s/tables'
|
||||
RECORDS_URL = 'https://spreadsheets.google.com/feeds/%s/records/%s'
|
||||
RECORD_URL = 'https://spreadsheets.google.com/feeds/%s/records/%s/%s'
|
||||
|
||||
|
||||
class SpreadsheetsClient(gdata.client.GDClient):
|
||||
api_version = '3'
|
||||
auth_service = 'wise'
|
||||
auth_scopes = gdata.gauth.AUTH_SCOPES['wise']
|
||||
ssl = True
|
||||
|
||||
def get_spreadsheets(self, auth_token=None,
|
||||
desired_class=gdata.spreadsheets.data.SpreadsheetsFeed,
|
||||
**kwargs):
|
||||
"""Obtains a feed with the spreadsheets belonging to the current user.
|
||||
|
||||
Args:
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient.
|
||||
desired_class: class descended from atom.core.XmlElement to which a
|
||||
successful response should be converted. If there is no
|
||||
converter function specified (converter=None) then the
|
||||
desired_class will be used in calling the
|
||||
atom.core.parse function. If neither
|
||||
the desired_class nor the converter is specified, an
|
||||
HTTP reponse object will be returned. Defaults to
|
||||
gdata.spreadsheets.data.SpreadsheetsFeed.
|
||||
"""
|
||||
return self.get_feed(SPREADSHEETS_URL, auth_token=auth_token,
|
||||
desired_class=desired_class, **kwargs)
|
||||
|
||||
GetSpreadsheets = get_spreadsheets
|
||||
|
||||
def get_worksheets(self, spreadsheet_key, auth_token=None,
|
||||
desired_class=gdata.spreadsheets.data.WorksheetsFeed,
|
||||
**kwargs):
|
||||
"""Finds the worksheets within a given spreadsheet.
|
||||
|
||||
Args:
|
||||
spreadsheet_key: str, The unique ID of this containing spreadsheet. This
|
||||
can be the ID from the URL or as provided in a
|
||||
Spreadsheet entry.
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient.
|
||||
desired_class: class descended from atom.core.XmlElement to which a
|
||||
successful response should be converted. If there is no
|
||||
converter function specified (converter=None) then the
|
||||
desired_class will be used in calling the
|
||||
atom.core.parse function. If neither
|
||||
the desired_class nor the converter is specified, an
|
||||
HTTP reponse object will be returned. Defaults to
|
||||
gdata.spreadsheets.data.WorksheetsFeed.
|
||||
"""
|
||||
return self.get_feed(WORKSHEETS_URL % spreadsheet_key,
|
||||
auth_token=auth_token, desired_class=desired_class,
|
||||
**kwargs)
|
||||
|
||||
GetWorksheets = get_worksheets
|
||||
|
||||
def add_worksheet(self, spreadsheet_key, title, rows, cols,
|
||||
auth_token=None, **kwargs):
|
||||
"""Creates a new worksheet entry in the spreadsheet.
|
||||
|
||||
Args:
|
||||
spreadsheet_key: str, The unique ID of this containing spreadsheet. This
|
||||
can be the ID from the URL or as provided in a
|
||||
Spreadsheet entry.
|
||||
title: str, The title to be used in for the worksheet.
|
||||
rows: str or int, The number of rows this worksheet should start with.
|
||||
cols: str or int, The number of columns this worksheet should start with.
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient.
|
||||
"""
|
||||
new_worksheet = gdata.spreadsheets.data.WorksheetEntry(
|
||||
title=atom.data.Title(text=title),
|
||||
row_count=gdata.spreadsheets.data.RowCount(text=str(rows)),
|
||||
col_count=gdata.spreadsheets.data.ColCount(text=str(cols)))
|
||||
return self.post(new_worksheet, WORKSHEETS_URL % spreadsheet_key,
|
||||
auth_token=auth_token, **kwargs)
|
||||
|
||||
AddWorksheet = add_worksheet
|
||||
|
||||
def get_worksheet(self, spreadsheet_key, worksheet_id,
|
||||
desired_class=gdata.spreadsheets.data.WorksheetEntry,
|
||||
auth_token=None, **kwargs):
|
||||
"""Retrieves a single worksheet.
|
||||
|
||||
Args:
|
||||
spreadsheet_key: str, The unique ID of this containing spreadsheet. This
|
||||
can be the ID from the URL or as provided in a
|
||||
Spreadsheet entry.
|
||||
worksheet_id: str, The unique ID for the worksheet withing the desired
|
||||
spreadsheet.
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient.
|
||||
desired_class: class descended from atom.core.XmlElement to which a
|
||||
successful response should be converted. If there is no
|
||||
converter function specified (converter=None) then the
|
||||
desired_class will be used in calling the
|
||||
atom.core.parse function. If neither
|
||||
the desired_class nor the converter is specified, an
|
||||
HTTP reponse object will be returned. Defaults to
|
||||
gdata.spreadsheets.data.WorksheetEntry.
|
||||
|
||||
"""
|
||||
return self.get_entry(WORKSHEET_URL % (spreadsheet_key, worksheet_id,),
|
||||
auth_token=auth_token, desired_class=desired_class,
|
||||
**kwargs)
|
||||
|
||||
GetWorksheet = get_worksheet
|
||||
|
||||
def add_table(self, spreadsheet_key, title, summary, worksheet_name,
|
||||
header_row, num_rows, start_row, insertion_mode,
|
||||
column_headers, auth_token=None, **kwargs):
|
||||
"""Creates a new table within the worksheet.
|
||||
|
||||
Args:
|
||||
spreadsheet_key: str, The unique ID of this containing spreadsheet. This
|
||||
can be the ID from the URL or as provided in a
|
||||
Spreadsheet entry.
|
||||
title: str, The title for the new table within a worksheet.
|
||||
summary: str, A description of the table.
|
||||
worksheet_name: str The name of the worksheet in which this table
|
||||
should live.
|
||||
header_row: int or str, The number of the row in the worksheet which
|
||||
will contain the column names for the data in this table.
|
||||
num_rows: int or str, The number of adjacent rows in this table.
|
||||
start_row: int or str, The number of the row at which the data begins.
|
||||
insertion_mode: str
|
||||
column_headers: dict of strings, maps the column letters (A, B, C) to
|
||||
the desired name which will be viewable in the
|
||||
worksheet.
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient.
|
||||
"""
|
||||
data = gdata.spreadsheets.data.Data(
|
||||
insertion_mode=insertion_mode, num_rows=str(num_rows),
|
||||
start_row=str(start_row))
|
||||
for index, name in column_headers.iteritems():
|
||||
data.column.append(gdata.spreadsheets.data.Column(
|
||||
index=index, name=name))
|
||||
new_table = gdata.spreadsheets.data.Table(
|
||||
title=atom.data.Title(text=title), summary=atom.data.Summary(summary),
|
||||
worksheet=gdata.spreadsheets.data.Worksheet(name=worksheet_name),
|
||||
header=gdata.spreadsheets.data.Header(row=str(header_row)), data=data)
|
||||
return self.post(new_table, TABLES_URL % spreadsheet_key,
|
||||
auth_token=auth_token, **kwargs)
|
||||
|
||||
AddTable = add_table
|
||||
|
||||
def get_tables(self, spreadsheet_key,
|
||||
desired_class=gdata.spreadsheets.data.TablesFeed,
|
||||
auth_token=None, **kwargs):
|
||||
"""Retrieves a feed listing the tables in this spreadsheet.
|
||||
|
||||
Args:
|
||||
spreadsheet_key: str, The unique ID of this containing spreadsheet. This
|
||||
can be the ID from the URL or as provided in a
|
||||
Spreadsheet entry.
|
||||
desired_class: class descended from atom.core.XmlElement to which a
|
||||
successful response should be converted. If there is no
|
||||
converter function specified (converter=None) then the
|
||||
desired_class will be used in calling the
|
||||
atom.core.parse function. If neither
|
||||
the desired_class nor the converter is specified, an
|
||||
HTTP reponse object will be returned. Defaults to
|
||||
gdata.spreadsheets.data.TablesFeed.
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient.
|
||||
"""
|
||||
return self.get_feed(TABLES_URL % spreadsheet_key,
|
||||
desired_class=desired_class, auth_token=auth_token,
|
||||
**kwargs)
|
||||
|
||||
GetTables = get_tables
|
||||
|
||||
def add_record(self, spreadsheet_key, table_id, fields,
|
||||
title=None, auth_token=None, **kwargs):
|
||||
"""Adds a new row to the table.
|
||||
|
||||
Args:
|
||||
spreadsheet_key: str, The unique ID of this containing spreadsheet. This
|
||||
can be the ID from the URL or as provided in a
|
||||
Spreadsheet entry.
|
||||
table_id: str, The ID of the table within the worksheet which should
|
||||
receive this new record. The table ID can be found using the
|
||||
get_table_id method of a gdata.spreadsheets.data.Table.
|
||||
fields: dict of strings mapping column names to values.
|
||||
title: str, optional The title for this row.
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient.
|
||||
"""
|
||||
new_record = gdata.spreadsheets.data.Record()
|
||||
if title is not None:
|
||||
new_record.title = atom.data.Title(text=title)
|
||||
for name, value in fields.iteritems():
|
||||
new_record.field.append(gdata.spreadsheets.data.Field(
|
||||
name=name, text=value))
|
||||
return self.post(new_record, RECORDS_URL % (spreadsheet_key, table_id),
|
||||
auth_token=auth_token, **kwargs)
|
||||
|
||||
AddRecord = add_record
|
||||
|
||||
def get_records(self, spreadsheet_key, table_id,
|
||||
desired_class=gdata.spreadsheets.data.RecordsFeed,
|
||||
auth_token=None, **kwargs):
|
||||
"""Retrieves the records in a table.
|
||||
|
||||
Args:
|
||||
spreadsheet_key: str, The unique ID of this containing spreadsheet. This
|
||||
can be the ID from the URL or as provided in a
|
||||
Spreadsheet entry.
|
||||
table_id: str, The ID of the table within the worksheet whose records
|
||||
we would like to fetch. The table ID can be found using the
|
||||
get_table_id method of a gdata.spreadsheets.data.Table.
|
||||
desired_class: class descended from atom.core.XmlElement to which a
|
||||
successful response should be converted. If there is no
|
||||
converter function specified (converter=None) then the
|
||||
desired_class will be used in calling the
|
||||
atom.core.parse function. If neither
|
||||
the desired_class nor the converter is specified, an
|
||||
HTTP reponse object will be returned. Defaults to
|
||||
gdata.spreadsheets.data.RecordsFeed.
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient.
|
||||
"""
|
||||
return self.get_feed(RECORDS_URL % (spreadsheet_key, table_id),
|
||||
desired_class=desired_class, auth_token=auth_token,
|
||||
**kwargs)
|
||||
|
||||
GetRecords = get_records
|
||||
|
||||
def get_record(self, spreadsheet_key, table_id, record_id,
|
||||
desired_class=gdata.spreadsheets.data.Record,
|
||||
auth_token=None, **kwargs):
|
||||
"""Retrieves a single record from the table.
|
||||
|
||||
Args:
|
||||
spreadsheet_key: str, The unique ID of this containing spreadsheet. This
|
||||
can be the ID from the URL or as provided in a
|
||||
Spreadsheet entry.
|
||||
table_id: str, The ID of the table within the worksheet whose records
|
||||
we would like to fetch. The table ID can be found using the
|
||||
get_table_id method of a gdata.spreadsheets.data.Table.
|
||||
record_id: str, The ID of the record within this table which we want to
|
||||
fetch. You can find the record ID using get_record_id() on
|
||||
an instance of the gdata.spreadsheets.data.Record class.
|
||||
desired_class: class descended from atom.core.XmlElement to which a
|
||||
successful response should be converted. If there is no
|
||||
converter function specified (converter=None) then the
|
||||
desired_class will be used in calling the
|
||||
atom.core.parse function. If neither
|
||||
the desired_class nor the converter is specified, an
|
||||
HTTP reponse object will be returned. Defaults to
|
||||
gdata.spreadsheets.data.RecordsFeed.
|
||||
auth_token: An object which sets the Authorization HTTP header in its
|
||||
modify_request method. Recommended classes include
|
||||
gdata.gauth.ClientLoginToken and gdata.gauth.AuthSubToken
|
||||
among others. Represents the current user. Defaults to None
|
||||
and if None, this method will look for a value in the
|
||||
auth_token member of SpreadsheetsClient."""
|
||||
return self.get_entry(RECORD_URL % (spreadsheet_key, table_id, record_id),
|
||||
desired_class=desired_class, auth_token=auth_token,
|
||||
**kwargs)
|
||||
|
||||
GetRecord = get_record
|
||||
|
||||
|
||||
class SpreadsheetQuery(gdata.client.Query):
|
||||
|
||||
def __init__(self, title=None, title_exact=None, **kwargs):
|
||||
"""Adds Spreadsheets feed query parameters to a request.
|
||||
|
||||
Args:
|
||||
title: str Specifies the search terms for the title of a document.
|
||||
This parameter used without title-exact will only submit partial
|
||||
queries, not exact queries.
|
||||
title_exact: str Specifies whether the title query should be taken as an
|
||||
exact string. Meaningless without title. Possible values are
|
||||
'true' and 'false'.
|
||||
"""
|
||||
gdata.client.Query.__init__(self, **kwargs)
|
||||
self.title = title
|
||||
self.title_exact = title_exact
|
||||
|
||||
def modify_request(self, http_request):
|
||||
gdata.client._add_query_param('title', self.title, http_request)
|
||||
gdata.client._add_query_param('title-exact', self.title_exact,
|
||||
http_request)
|
||||
gdata.client.Query.modify_request(self, http_request)
|
||||
|
||||
ModifyRequest = modify_request
|
||||
|
||||
|
||||
class WorksheetQuery(SpreadsheetQuery):
|
||||
pass
|
||||
|
||||
|
||||
class ListQuery(gdata.client.Query):
|
||||
|
||||
def __init__(self, order_by=None, reverse=None, sq=None, **kwargs):
|
||||
"""Adds List-feed specific query parameters to a request.
|
||||
|
||||
Args:
|
||||
order_by: str Specifies what column to use in ordering the entries in
|
||||
the feed. By position (the default): 'position' returns
|
||||
rows in the order in which they appear in the GUI. Row 1, then
|
||||
row 2, then row 3, and so on. By column:
|
||||
'column:columnName' sorts rows in ascending order based on the
|
||||
values in the column with the given columnName, where
|
||||
columnName is the value in the header row for that column.
|
||||
reverse: str Specifies whether to sort in descending or ascending order.
|
||||
Reverses default sort order: 'true' results in a descending
|
||||
sort; 'false' (the default) results in an ascending sort.
|
||||
sq: str Structured query on the full text in the worksheet.
|
||||
[columnName][binaryOperator][value]
|
||||
Supported binaryOperators are:
|
||||
- (), for overriding order of operations
|
||||
- = or ==, for strict equality
|
||||
- <> or !=, for strict inequality
|
||||
- and or &&, for boolean and
|
||||
- or or ||, for boolean or
|
||||
"""
|
||||
gdata.client.Query.__init__(self, **kwargs)
|
||||
self.order_by = order_by
|
||||
self.reverse = reverse
|
||||
self.sq = sq
|
||||
|
||||
def modify_request(self, http_request):
|
||||
gdata.client._add_query_param('orderby', self.order_by, http_request)
|
||||
gdata.client._add_query_param('reverse', self.reverse, http_request)
|
||||
gdata.client._add_query_param('sq', self.sq, http_request)
|
||||
gdata.client.Query.modify_request(self, http_request)
|
||||
|
||||
ModifyRequest = modify_request
|
||||
|
||||
|
||||
class TableQuery(ListQuery):
|
||||
pass
|
||||
|
||||
|
||||
class CellQuery(gdata.client.Query):
|
||||
|
||||
def __init__(self, min_row=None, max_row=None, min_col=None, max_col=None,
|
||||
range=None, return_empty=None, **kwargs):
|
||||
"""Adds Cells-feed specific query parameters to a request.
|
||||
|
||||
Args:
|
||||
min_row: str or int Positional number of minimum row returned in query.
|
||||
max_row: str or int Positional number of maximum row returned in query.
|
||||
min_col: str or int Positional number of minimum column returned in query.
|
||||
max_col: str or int Positional number of maximum column returned in query.
|
||||
range: str A single cell or a range of cells. Use standard spreadsheet
|
||||
cell-range notations, using a colon to separate start and end of
|
||||
range. Examples:
|
||||
- 'A1' and 'R1C1' both specify only cell A1.
|
||||
- 'D1:F3' and 'R1C4:R3C6' both specify the rectangle of cells with
|
||||
corners at D1 and F3.
|
||||
return_empty: str If 'true' then empty cells will be returned in the feed.
|
||||
If omitted, the default is 'false'.
|
||||
"""
|
||||
gdata.client.Query.__init__(self, **kwargs)
|
||||
self.min_row = min_row
|
||||
self.max_row = max_row
|
||||
self.min_col = min_col
|
||||
self.max_col = max_col
|
||||
self.range = range
|
||||
self.return_empty = return_empty
|
||||
|
||||
def modify_request(self, http_request):
|
||||
gdata.client._add_query_param('min-row', self.min_row, http_request)
|
||||
gdata.client._add_query_param('max-row', self.max_row, http_request)
|
||||
gdata.client._add_query_param('min-col', self.min_col, http_request)
|
||||
gdata.client._add_query_param('max-col', self.max_col, http_request)
|
||||
gdata.client._add_query_param('range', self.range, http_request)
|
||||
gdata.client._add_query_param('return-empty', self.return_empty,
|
||||
http_request)
|
||||
gdata.client.Query.modify_request(self, http_request)
|
||||
|
||||
ModifyRequest = modify_request
|
317
python/gdata/spreadsheets/data.py
Normal file
317
python/gdata/spreadsheets/data.py
Normal file
@@ -0,0 +1,317 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2009 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
# This module is used for version 2 of the Google Data APIs.
|
||||
|
||||
|
||||
"""Provides classes and constants for the XML in the Google Spreadsheets API.
|
||||
|
||||
Documentation for the raw XML which these classes represent can be found here:
|
||||
http://code.google.com/apis/spreadsheets/docs/3.0/reference.html#Elements
|
||||
"""
|
||||
|
||||
|
||||
__author__ = 'j.s@google.com (Jeff Scudder)'
|
||||
|
||||
|
||||
import atom.core
|
||||
import gdata.data
|
||||
|
||||
|
||||
GS_TEMPLATE = '{http://schemas.google.com/spreadsheets/2006}%s'
|
||||
GSX_NAMESPACE = 'http://schemas.google.com/spreadsheets/2006/extended'
|
||||
|
||||
|
||||
INSERT_MODE = 'insert'
|
||||
OVERWRITE_MODE = 'overwrite'
|
||||
|
||||
|
||||
WORKSHEETS_REL = 'http://schemas.google.com/spreadsheets/2006#worksheetsfeed'
|
||||
|
||||
|
||||
class Error(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class FieldMissing(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class HeaderNotSet(Error):
|
||||
"""The desired column header had no value for the row in the list feed."""
|
||||
|
||||
|
||||
class Cell(atom.core.XmlElement):
|
||||
"""The gs:cell element.
|
||||
|
||||
A cell in the worksheet. The <gs:cell> element can appear only as a child
|
||||
of <atom:entry>.
|
||||
"""
|
||||
_qname = GS_TEMPLATE % 'cell'
|
||||
col = 'col'
|
||||
input_value = 'inputValue'
|
||||
numeric_value = 'numericValue'
|
||||
row = 'row'
|
||||
|
||||
|
||||
class ColCount(atom.core.XmlElement):
|
||||
"""The gs:colCount element.
|
||||
|
||||
Indicates the number of columns in the worksheet, including columns that
|
||||
contain only empty cells. The <gs:colCount> element can appear as a child
|
||||
of <atom:entry> or <atom:feed>
|
||||
"""
|
||||
_qname = GS_TEMPLATE % 'colCount'
|
||||
|
||||
|
||||
class Field(atom.core.XmlElement):
|
||||
"""The gs:field element.
|
||||
|
||||
A field single cell within a record. Contained in an <atom:entry>.
|
||||
"""
|
||||
_qname = GS_TEMPLATE % 'field'
|
||||
index = 'index'
|
||||
name = 'name'
|
||||
|
||||
|
||||
class Column(Field):
|
||||
"""The gs:column element."""
|
||||
_qname = GS_TEMPLATE % 'column'
|
||||
|
||||
|
||||
class Data(atom.core.XmlElement):
|
||||
"""The gs:data element.
|
||||
|
||||
A data region of a table. Contained in an <atom:entry> element.
|
||||
"""
|
||||
_qname = GS_TEMPLATE % 'data'
|
||||
column = [Column]
|
||||
insertion_mode = 'insertionMode'
|
||||
num_rows = 'numRows'
|
||||
start_row = 'startRow'
|
||||
|
||||
|
||||
class Header(atom.core.XmlElement):
|
||||
"""The gs:header element.
|
||||
|
||||
Indicates which row is the header row. Contained in an <atom:entry>.
|
||||
"""
|
||||
_qname = GS_TEMPLATE % 'header'
|
||||
row = 'row'
|
||||
|
||||
|
||||
class RowCount(atom.core.XmlElement):
|
||||
"""The gs:rowCount element.
|
||||
|
||||
Indicates the number of total rows in the worksheet, including rows that
|
||||
contain only empty cells. The <gs:rowCount> element can appear as a
|
||||
child of <atom:entry> or <atom:feed>.
|
||||
"""
|
||||
_qname = GS_TEMPLATE % 'rowCount'
|
||||
|
||||
|
||||
class Worksheet(atom.core.XmlElement):
|
||||
"""The gs:worksheet element.
|
||||
|
||||
The worksheet where the table lives.Contained in an <atom:entry>.
|
||||
"""
|
||||
_qname = GS_TEMPLATE % 'worksheet'
|
||||
name = 'name'
|
||||
|
||||
|
||||
class Spreadsheet(gdata.data.GDEntry):
|
||||
"""An Atom entry which represents a Google Spreadsheet."""
|
||||
|
||||
def find_worksheets_feed(self):
|
||||
return self.find_url(WORKSHEETS_REL)
|
||||
|
||||
FindWorksheetsFeed = find_worksheets_feed
|
||||
|
||||
|
||||
class SpreadsheetsFeed(gdata.data.GDFeed):
|
||||
"""An Atom feed listing a user's Google Spreadsheets."""
|
||||
entry = [Spreadsheet]
|
||||
|
||||
|
||||
class WorksheetEntry(gdata.data.GDEntry):
|
||||
"""An Atom entry representing a single worksheet in a spreadsheet."""
|
||||
row_count = RowCount
|
||||
col_count = ColCount
|
||||
|
||||
|
||||
class WorksheetsFeed(gdata.data.GDFeed):
|
||||
"""A feed containing the worksheets in a single spreadsheet."""
|
||||
entry = [WorksheetEntry]
|
||||
|
||||
|
||||
class Table(gdata.data.GDEntry):
|
||||
"""An Atom entry that represents a subsection of a worksheet.
|
||||
|
||||
A table allows you to treat part or all of a worksheet somewhat like a
|
||||
table in a database that is, as a set of structured data items. Tables
|
||||
don't exist until you explicitly create them before you can use a table
|
||||
feed, you have to explicitly define where the table data comes from.
|
||||
"""
|
||||
data = Data
|
||||
header = Header
|
||||
worksheet = Worksheet
|
||||
|
||||
def get_table_id(self):
|
||||
if self.id.text:
|
||||
return self.id.text.split('/')[-1]
|
||||
return None
|
||||
|
||||
GetTableId = get_table_id
|
||||
|
||||
|
||||
class TablesFeed(gdata.data.GDFeed):
|
||||
"""An Atom feed containing the tables defined within a worksheet."""
|
||||
entry = [Table]
|
||||
|
||||
|
||||
class Record(gdata.data.GDEntry):
|
||||
"""An Atom entry representing a single record in a table.
|
||||
|
||||
Note that the order of items in each record is the same as the order of
|
||||
columns in the table definition, which may not match the order of
|
||||
columns in the GUI.
|
||||
"""
|
||||
field = [Field]
|
||||
|
||||
def value_for_index(self, column_index):
|
||||
for field in self.field:
|
||||
if field.index == column_index:
|
||||
return field.text
|
||||
raise FieldMissing('There is no field for %s' % column_index)
|
||||
|
||||
ValueForIndex = value_for_index
|
||||
|
||||
def value_for_name(self, name):
|
||||
for field in self.field:
|
||||
if field.name == name:
|
||||
return field.text
|
||||
raise FieldMissing('There is no field for %s' % name)
|
||||
|
||||
ValueForName = value_for_name
|
||||
|
||||
def get_record_id(self):
|
||||
if self.id.text:
|
||||
return self.id.text.split('/')[-1]
|
||||
return None
|
||||
|
||||
|
||||
class RecordsFeed(gdata.data.GDFeed):
|
||||
"""An Atom feed containing the individuals records in a table."""
|
||||
entry = [Record]
|
||||
|
||||
|
||||
class ListRow(atom.core.XmlElement):
|
||||
"""A gsx column value within a row.
|
||||
|
||||
The local tag in the _qname is blank and must be set to the column
|
||||
name. For example, when adding to a ListEntry, do:
|
||||
col_value = ListRow(text='something')
|
||||
col_value._qname = col_value._qname % 'mycolumnname'
|
||||
"""
|
||||
_qname = '{http://schemas.google.com/spreadsheets/2006/extended}%s'
|
||||
|
||||
|
||||
class ListEntry(gdata.data.GDEntry):
|
||||
"""An Atom entry representing a worksheet row in the list feed.
|
||||
|
||||
The values for a particular column can be get and set using
|
||||
x.get_value('columnheader') and x.set_value('columnheader', 'value').
|
||||
See also the explanation of column names in the ListFeed class.
|
||||
"""
|
||||
|
||||
def get_value(self, column_name):
|
||||
"""Returns the displayed text for the desired column in this row.
|
||||
|
||||
The formula or input which generated the displayed value is not accessible
|
||||
through the list feed, to see the user's input, use the cells feed.
|
||||
|
||||
If a column is not present in this spreadsheet, or there is no value
|
||||
for a column in this row, this method will return None.
|
||||
"""
|
||||
values = self.get_elements(column_name, GSX_NAMESPACE)
|
||||
if len(values) == 0:
|
||||
return None
|
||||
return values[0].text
|
||||
|
||||
def set_value(self, column_name, value):
|
||||
"""Changes the value of cell in this row under the desired column name.
|
||||
|
||||
Warning: if the cell contained a formula, it will be wiped out by setting
|
||||
the value using the list feed since the list feed only works with
|
||||
displayed values.
|
||||
|
||||
No client side checking is performed on the column_name, you need to
|
||||
ensure that the column_name is the local tag name in the gsx tag for the
|
||||
column. For example, the column_name will not contain special characters,
|
||||
spaces, uppercase letters, etc.
|
||||
"""
|
||||
# Try to find the column in this row to change an existing value.
|
||||
values = self.get_elements(column_name, GSX_NAMESPACE)
|
||||
if len(values) > 0:
|
||||
values[0].text = value
|
||||
else:
|
||||
# There is no value in this row for the desired column, so add a new
|
||||
# gsx:column_name element.
|
||||
new_value = ListRow(text=value)
|
||||
new_value._qname = new_value._qname % (column_name,)
|
||||
self._other_elements.append(new_value)
|
||||
|
||||
|
||||
class ListsFeed(gdata.data.GDFeed):
|
||||
"""An Atom feed in which each entry represents a row in a worksheet.
|
||||
|
||||
The first row in the worksheet is used as the column names for the values
|
||||
in each row. If a header cell is empty, then a unique column ID is used
|
||||
for the gsx element name.
|
||||
|
||||
Spaces in a column name are removed from the name of the corresponding
|
||||
gsx element.
|
||||
|
||||
Caution: The columnNames are case-insensitive. For example, if you see
|
||||
a <gsx:e-mail> element in a feed, you can't know whether the column
|
||||
heading in the original worksheet was "e-mail" or "E-Mail".
|
||||
|
||||
Note: If two or more columns have the same name, then subsequent columns
|
||||
of the same name have _n appended to the columnName. For example, if the
|
||||
first column name is "e-mail", followed by columns named "E-Mail" and
|
||||
"E-mail", then the columnNames will be gsx:e-mail, gsx:e-mail_2, and
|
||||
gsx:e-mail_3 respectively.
|
||||
"""
|
||||
entry = [ListEntry]
|
||||
|
||||
|
||||
class CellEntry(gdata.data.BatchEntry):
|
||||
"""An Atom entry representing a single cell in a worksheet."""
|
||||
cell = Cell
|
||||
|
||||
|
||||
class CellsFeed(gdata.data.BatchFeed):
|
||||
"""An Atom feed contains one entry per cell in a worksheet.
|
||||
|
||||
The cell feed supports batch operations, you can send multiple cell
|
||||
operations in one HTTP request.
|
||||
"""
|
||||
entry = [CellEntry]
|
||||
|
||||
def batch_set_cell(row, col, input):
|
||||
pass
|
||||
|
Reference in New Issue
Block a user