|
23 | 23 | # iii/ change .from_existing to .from_raw
|
24 | 24 |
|
25 | 25 |
|
| 26 | +def _value_to_cell(val): |
| 27 | + if str(val).isdigit(): |
| 28 | + try: |
| 29 | + return {'userEnteredValue': {'numberValue': float(val)}} |
| 30 | + except: |
| 31 | + return {'userEnteredValue': {'numberValue': int(val)}} |
| 32 | + return {'userEnteredValue': {'stringValue': str(val)}} |
| 33 | + |
| 34 | + |
| 35 | +def _cells_to_row(cells): |
| 36 | + return [_value_to_cell(cell) for cell in cells] |
| 37 | + |
| 38 | + |
| 39 | +def _format_sheet(sheet): |
| 40 | + title = sheet.get('title', 'NEW SHEET') |
| 41 | + values = sheet.get('values', []) |
| 42 | + return { |
| 43 | + 'properties': { |
| 44 | + 'title': title |
| 45 | + }, |
| 46 | + 'data': { |
| 47 | + 'rowData': [ |
| 48 | + {'values': _cells_to_row(val)} for val in values |
| 49 | + ] |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
26 | 54 | def _grid_to_a1(sheet_name, start, end):
|
27 | 55 | start_row, start_col = start
|
28 | 56 | end_row, end_col = end
|
@@ -62,6 +90,16 @@ def get_spreadsheet(self, id):
|
62 | 90 |
|
63 | 91 | return Spreadsheet.from_existing(data, self)
|
64 | 92 |
|
| 93 | + def create_spreadsheet(self, sheets=[], **kwargs): |
| 94 | + data = self._resource.spreadsheets().create( |
| 95 | + body={ |
| 96 | + 'properties': kwargs, |
| 97 | + 'sheets': [_format_sheet(s) for s in sheets] |
| 98 | + } |
| 99 | + ).execute() |
| 100 | + |
| 101 | + return Spreadsheet.from_existing(data, self) |
| 102 | + |
65 | 103 | def get_values(self, spreadsheet_id, range_name):
|
66 | 104 | """Initialize a new block and return it"""
|
67 | 105 |
|
@@ -122,26 +160,31 @@ def __init__(self, client=None, **kwargs):
|
122 | 160 | # initalize the other properties
|
123 | 161 | super(self.__class__, self).__init__(**kwargs)
|
124 | 162 |
|
125 |
| - @classmethod |
126 |
| - def from_existing(cls, data, client=None): |
127 |
| - """initiates using existing Spreadsheet resource""" |
128 |
| - |
129 |
| - new_data = keys_to_snake(data) |
130 |
| - return cls(client, **new_data) |
131 |
| - |
132 | 163 | def __iter__(self):
|
133 | 164 | return self.yield_sheets()
|
134 | 165 |
|
| 166 | + def __getitem__(self, key): |
| 167 | + try: |
| 168 | + if key.isdigit(): |
| 169 | + return self.get_sheet_by_id(key) |
| 170 | + else: |
| 171 | + return self.get_sheet_by_name(key) |
| 172 | + except ValueError: |
| 173 | + raise TypeError('Sheet not found') |
| 174 | + |
135 | 175 | def __enter__(self):
|
136 | 176 | return self
|
137 | 177 |
|
138 | 178 | def __exit__(self, exception_type, exception_value, traceback):
|
139 |
| - # if self.__updates: |
140 |
| - # self.client.push_updates(self._id, self._updates) |
141 |
| - # # TODO: add success handlers |
142 |
| - # del self._updates[:] |
143 | 179 | self.update()
|
144 | 180 |
|
| 181 | + @classmethod |
| 182 | + def from_existing(cls, data, client=None): |
| 183 | + """initiates using existing Spreadsheet resource""" |
| 184 | + |
| 185 | + new_data = keys_to_snake(data) |
| 186 | + return cls(client, **new_data) |
| 187 | + |
145 | 188 | @property
|
146 | 189 | def id(self):
|
147 | 190 | return self._spreadsheet_id
|
@@ -216,15 +259,6 @@ def update(self):
|
216 | 259 | # TODO: add success handlers
|
217 | 260 | del self._updates[:]
|
218 | 261 |
|
219 |
| - def __getitem__(self, key): |
220 |
| - try: |
221 |
| - if key.isdigit(): |
222 |
| - return self.get_sheet_by_id(key) |
223 |
| - else: |
224 |
| - return self.get_sheet_by_name(key) |
225 |
| - except ValueError: |
226 |
| - raise TypeError('Sheet not found') |
227 |
| - |
228 | 262 | class NamedRange(object):
|
229 | 263 | """represents a NamedRange resource, can
|
230 | 264 | return it's range in A1 notation
|
|
0 commit comments