-
Notifications
You must be signed in to change notification settings - Fork 933
/
models.py
1110 lines (852 loc) · 34.3 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
gspread.models
~~~~~~~~~~~~~~
This module contains common spreadsheets' models.
"""
from .exceptions import WorksheetNotFound, CellNotFound
from .utils import (
a1_to_rowcol,
rowcol_to_a1,
cast_to_a1_notation,
numericise_all,
finditem,
fill_gaps,
cell_list_to_rect,
quote
)
from .urls import (
SPREADSHEET_URL,
SPREADSHEET_VALUES_URL,
SPREADSHEET_BATCH_UPDATE_URL,
SPREADSHEET_VALUES_APPEND_URL,
SPREADSHEET_VALUES_CLEAR_URL
)
try:
unicode
except NameError:
basestring = unicode = str
class Spreadsheet(object):
"""The class that represents a spreadsheet."""
def __init__(self, client, properties):
self.client = client
self._properties = properties
@property
def id(self):
"""Spreadsheet ID."""
return self._properties['id']
@property
def title(self):
"""Spreadsheet title."""
try:
return self._properties['title']
except KeyError:
metadata = self.fetch_sheet_metadata()
self._properties.update(metadata['properties'])
return self._properties['title']
@property
def updated(self):
""".. deprecated:: 2.0
This feature is not supported in Sheets API v4.
"""
import warnings
warnings.warn(
"Spreadsheet.updated() is deprecated, "
"this feature is not supported in Sheets API v4",
DeprecationWarning,
stacklevel=2
)
@property
def sheet1(self):
"""Shortcut property for getting the first worksheet."""
return self.get_worksheet(0)
def __iter__(self):
for sheet in self.worksheets():
yield(sheet)
def __repr__(self):
return '<%s %s id:%s>' % (self.__class__.__name__,
repr(self.title),
self.id)
def batch_update(self, body):
"""Lower-level method that directly calls `spreadsheets.batchUpdate <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate>`_.
:param dict body: `Request body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate#request-body>`_.
:returns: `Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate#response-body>`_.
:rtype: dict
.. versionadded:: 3.0
"""
r = self.client.request(
'post',
SPREADSHEET_BATCH_UPDATE_URL % self.id,
json=body
)
return r.json()
def values_append(self, range, params, body):
"""Lower-level method that directly calls `spreadsheets.values.append <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append>`_.
:param str range: The `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_
of a range to search for a logical table of data. Values will be appended after the last row of the table.
:param dict params: `Query parameters <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#query-parameters>`_.
:param dict body: `Request body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#request-body>`_.
:returns: `Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#response-body>`_.
:rtype: dict
.. versionadded:: 3.0
"""
url = SPREADSHEET_VALUES_APPEND_URL % (self.id, quote(range))
r = self.client.request('post', url, params=params, json=body)
return r.json()
def values_clear(self, range):
"""Lower-level method that directly calls `spreadsheets.values.clear <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear>`_.
:param str range: The `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_ of the values to clear.
:returns: `Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear#response-body>`_.
:rtype: dict
.. versionadded:: 3.0
"""
url = SPREADSHEET_VALUES_CLEAR_URL % (self.id, quote(range))
r = self.client.request('post', url)
return r.json()
def values_get(self, range, params=None):
"""Lower-level method that directly calls `spreadsheets.values.get <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get>`_.
:param str range: The `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_ of the values to retrieve.
:param dict params: (optional) `Query parameters <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get#query-parameters>`_.
:returns: `Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get#response-body>`_.
:rtype: dict
.. versionadded:: 3.0
"""
url = SPREADSHEET_VALUES_URL % (self.id, quote(range))
r = self.client.request('get', url, params=params)
return r.json()
def values_update(self, range, params=None, body=None):
"""Lower-level method that directly calls `spreadsheets.values.update <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update>`_.
:param str range: The `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_ of the values to update.
:param dict params: (optional) `Query parameters <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update#query-parameters>`_.
:param dict body: (optional) `Request body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update#request-body>`_.
:returns: `Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update#response-body>`_.
:rtype: dict
Example::
sh.values_update(
'Sheet1!A2',
params={
'valueInputOption': 'USER_ENTERED'
},
body={
'values': [[1, 2, 3]]
}
)
.. versionadded:: 3.0
"""
url = SPREADSHEET_VALUES_URL % (self.id, quote(range))
r = self.client.request('put', url, params=params, json=body)
return r.json()
def fetch_sheet_metadata(self):
params = {'includeGridData': 'false'}
url = SPREADSHEET_URL % self.id
r = self.client.request('get', url, params=params)
return r.json()
def get_worksheet(self, index):
"""Returns a worksheet with specified `index`.
:param index: An index of a worksheet. Indexes start from zero.
:type index: int
:returns: an instance of :class:`gspread.models.Worksheet`
or `None` if the worksheet is not found.
Example. To get first worksheet of a spreadsheet:
>>> sht = client.open('My fancy spreadsheet')
>>> worksheet = sht.get_worksheet(0)
"""
sheet_data = self.fetch_sheet_metadata()
try:
properties = sheet_data['sheets'][index]['properties']
return Worksheet(self, properties)
except (KeyError, IndexError):
return None
def worksheets(self):
"""Returns a list of all :class:`worksheets <gspread.models.Worksheet>`
in a spreadsheet.
"""
sheet_data = self.fetch_sheet_metadata()
return [Worksheet(self, x['properties']) for x in sheet_data['sheets']]
def worksheet(self, title):
"""Returns a worksheet with specified `title`.
:param title: A title of a worksheet. If there're multiple
worksheets with the same title, first one will
be returned.
:type title: str
:returns: an instance of :class:`gspread.models.Worksheet`.
Example. Getting worksheet named 'Annual bonuses'
>>> sht = client.open('Sample one')
>>> worksheet = sht.worksheet('Annual bonuses')
"""
sheet_data = self.fetch_sheet_metadata()
try:
item = finditem(
lambda x: x['properties']['title'] == title,
sheet_data['sheets']
)
return Worksheet(self, item['properties'])
except (StopIteration, KeyError):
raise WorksheetNotFound(title)
def add_worksheet(self, title, rows, cols):
"""Adds a new worksheet to a spreadsheet.
:param title: A title of a new worksheet.
:type title: str
:param rows: Number of rows.
:type rows: int
:param cols: Number of columns.
:type cols: int
:returns: a newly created :class:`worksheets <gspread.models.Worksheet>`.
"""
body = {
'requests': [{
'addSheet': {
'properties': {
'title': title,
'sheetType': 'GRID',
'gridProperties': {
'rowCount': rows,
'columnCount': cols
}
}
}
}]
}
data = self.batch_update(body)
properties = data['replies'][0]['addSheet']['properties']
worksheet = Worksheet(self, properties)
return worksheet
def duplicate_sheet(
self,
source_sheet_id,
insert_sheet_index=None,
new_sheet_id=None,
new_sheet_name=None
):
"""Duplicates the contents of a sheet.
:param int source_sheet_id: The sheet ID to duplicate.
:param int insert_sheet_index: (optional) The zero-based index
where the new sheet should be inserted.
The index of all sheets after this are
incremented.
:param int new_sheet_id: (optional) The ID of the new sheet.
If not set, an ID is chosen. If set, the ID
must not conflict with any existing sheet ID.
If set, it must be non-negative.
:param str new_sheet_name: (optional) The name of the new sheet.
If empty, a new name is chosen for you.
:returns: a newly created :class:`<gspread.models.Worksheet>`.
.. versionadded:: 3.1.0
"""
body = {
'requests': [{
'duplicateSheet': {
'sourceSheetId': source_sheet_id,
'insertSheetIndex': insert_sheet_index,
'newSheetId': new_sheet_id,
'newSheetName': new_sheet_name
}
}]
}
data = self.batch_update(body)
properties = data['replies'][0]['duplicateSheet']['properties']
worksheet = Worksheet(self, properties)
return worksheet
def del_worksheet(self, worksheet):
"""Deletes a worksheet from a spreadsheet.
:param worksheet: The worksheet to be deleted.
:type worksheet: :class:`~gspread.Worksheet`
"""
body = {
'requests': [{
'deleteSheet': {'sheetId': worksheet._properties['sheetId']}
}]
}
return self.batch_update(body)
def share(self, value, perm_type, role, notify=True, email_message=None, with_link=False):
"""Share the spreadsheet with other accounts.
:param value: user or group e-mail address, domain name
or None for 'default' type.
:type value: str, None
:param perm_type: The account type.
Allowed values are: ``user``, ``group``, ``domain``,
``anyone``.
:type perm_type: str
:param role: The primary role for this user.
Allowed values are: ``owner``, ``writer``, ``reader``.
:type role: str
:param notify: (optional) Whether to send an email to the target user/domain.
:type notify: str
:param email_message: (optional) The email to be sent if notify=True
:type email_message: str
:param with_link: (optional) Whether the link is required for this permission
:type with_link: bool
Example::
# Give Otto a write permission on this spreadsheet
sh.share('otto@example.com', perm_type='user', role='writer')
# Transfer ownership to Otto
sh.share('otto@example.com', perm_type='user', role='owner')
"""
self.client.insert_permission(
self.id,
value=value,
perm_type=perm_type,
role=role,
notify=notify,
email_message=email_message,
with_link=with_link
)
def list_permissions(self):
"""Lists the spreadsheet's permissions.
"""
return self.client.list_permissions(self.id)
def remove_permissions(self, value, role='any'):
"""Remove permissions from a user or domain.
:param value: User or domain to remove permissions from
:type value: str
:param role: (optional) Permission to remove. Defaults to all
permissions.
:type role: str
Example::
# Remove Otto's write permission for this spreadsheet
sh.remove_permissions('otto@example.com', role='writer')
# Remove all Otto's permissions for this spreadsheet
sh.remove_permissions('otto@example.com')
"""
permission_list = self.client.list_permissions(self.id)
key = 'emailAddress' if '@' in value else 'domain'
filtered_id_list = [
p['id'] for p in permission_list
if p[key] == value and (p['role'] == role or role == 'any')
]
for permission_id in filtered_id_list:
self.client.remove_permission(self.id, permission_id)
return filtered_id_list
class Worksheet(object):
"""The class that represents a single sheet in a spreadsheet
(aka "worksheet").
"""
def __init__(self, spreadsheet, properties):
self.spreadsheet = spreadsheet
self.client = spreadsheet.client
self._properties = properties
def __repr__(self):
return '<%s %s id:%s>' % (self.__class__.__name__,
repr(self.title),
self.id)
@property
def id(self):
"""Worksheet ID."""
return self._properties['sheetId']
@property
def title(self):
"""Worksheet title."""
return self._properties['title']
@property
def updated(self):
""".. deprecated:: 2.0
This feature is not supported in Sheets API v4.
"""
import warnings
warnings.warn(
"Worksheet.updated() is deprecated, "
"this feature is not supported in Sheets API v4",
DeprecationWarning,
stacklevel=2
)
@property
def row_count(self):
"""Number of rows."""
return self._properties['gridProperties']['rowCount']
@property
def col_count(self):
"""Number of columns."""
return self._properties['gridProperties']['columnCount']
def acell(self, label, value_render_option='FORMATTED_VALUE'):
"""Returns an instance of a :class:`gspread.models.Cell`.
:param label: Cell label in A1 notation
Letter case is ignored.
:type label: str
:param value_render_option: (optional) Determines how values should be
rendered in the the output. See
`ValueRenderOption`_ in the Sheets API.
:type value_render_option: str
.. _ValueRenderOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
Example:
>>> worksheet.acell('A1')
<Cell R1C1 "I'm cell A1">
"""
return self.cell(
*(a1_to_rowcol(label)),
value_render_option=value_render_option
)
def cell(self, row, col, value_render_option='FORMATTED_VALUE'):
"""Returns an instance of a :class:`gspread.models.Cell` located at
`row` and `col` column.
:param row: Row number.
:type row: int
:param col: Column number.
:type col: int
:param value_render_option: (optional) Determines how values should be
rendered in the the output. See
`ValueRenderOption`_ in the Sheets API.
:type value_render_option: str
.. _ValueRenderOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
Example:
>>> worksheet.cell(1, 1)
<Cell R1C1 "I'm cell A1">
"""
range_label = '%s!%s' % (self.title, rowcol_to_a1(row, col))
data = self.spreadsheet.values_get(
range_label,
params={'valueRenderOption': value_render_option}
)
try:
value = data['values'][0][0]
except KeyError:
value = ''
return Cell(row, col, value)
@cast_to_a1_notation
def range(self, name):
"""Returns a list of :class:`Cell` objects from a specified range.
:param name: A string with range value in A1 notation, e.g. 'A1:A5'.
:type name: str
Alternatively, you may specify numeric boundaries. All values
index from 1 (one):
:param first_row: Row number
:type first_row: int
:param first_col: Row number
:type first_col: int
:param last_row: Row number
:type last_row: int
:param last_col: Row number
:type last_col: int
Example::
>>> # Using A1 notation
>>> worksheet.range('A1:B7')
[<Cell R1C1 "42">, ...]
>>> # Same with numeric boundaries
>>> worksheet.range(1, 1, 7, 2)
[<Cell R1C1 "42">, ...]
"""
range_label = '%s!%s' % (self.title, name)
data = self.spreadsheet.values_get(range_label)
start, end = name.split(':')
(row_offset, column_offset) = a1_to_rowcol(start)
(last_row, last_column) = a1_to_rowcol(end)
values = data.get('values', [])
rect_values = fill_gaps(
values,
rows=last_row - row_offset + 1,
cols=last_column - column_offset + 1
)
return [
Cell(row=i + row_offset, col=j + column_offset, value=value)
for i, row in enumerate(rect_values)
for j, value in enumerate(row)
]
def get_all_values(self):
"""Returns a list of lists containing all cells' values as strings.
.. note::
Empty trailing rows and columns will not be included.
"""
data = self.spreadsheet.values_get(self.title)
try:
return fill_gaps(data['values'])
except KeyError:
return []
def get_all_records(
self,
empty2zero=False,
head=1,
default_blank="",
allow_underscores_in_numeric_literals=False,
):
"""Returns a list of dictionaries, all of them having the contents
of the spreadsheet with the head row as keys and each of these
dictionaries holding the contents of subsequent rows of cells
as values.
Cell values are numericised (strings that can be read as ints
or floats are converted).
:param empty2zero: (optional) Determines whether empty cells are
converted to zeros.
:type empty2zero: bool
:param head: (optional) Determines wich row to use as keys, starting
from 1 following the numeration of the spreadsheet.
:type head: int
:param default_blank: (optional) Determines whether empty cells are
converted to something else except empty string
or zero.
:type default_blank: str
:param allow_underscores_in_numeric_literals: (optional) Allow underscores
in numeric literals,
as introduced in PEP 515
:type allow_underscores_in_numeric_literals: bool
"""
idx = head - 1
data = self.get_all_values()
# Return an empty list if the sheet doesn't have enough rows
if len(data) <= idx:
return []
keys = data[idx]
values = [
numericise_all(
row,
empty2zero,
default_blank,
allow_underscores_in_numeric_literals,
)
for row in data[idx + 1:]
]
return [dict(zip(keys, row)) for row in values]
def row_values(self, row, value_render_option='FORMATTED_VALUE'):
"""Returns a list of all values in a `row`.
Empty cells in this list will be rendered as :const:`None`.
:param row: Row number.
:type row: int
:param value_render_option: (optional) Determines how values should be
rendered in the the output. See
`ValueRenderOption`_ in the Sheets API.
:type value_render_option: str
.. _ValueRenderOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
"""
range_label = '%s!A%s:%s' % (self.title, row, row)
data = self.spreadsheet.values_get(
range_label,
params={'valueRenderOption': value_render_option}
)
try:
return data['values'][0]
except KeyError:
return []
def col_values(self, col, value_render_option='FORMATTED_VALUE'):
"""Returns a list of all values in column `col`.
Empty cells in this list will be rendered as :const:`None`.
:param col: Column number.
:type col: int
:param value_render_option: (optional) Determines how values should be
rendered in the the output. See
`ValueRenderOption`_ in the Sheets API.
:type value_render_option: str
.. _ValueRenderOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
"""
start_label = rowcol_to_a1(1, col)
range_label = '%s!%s:%s' % (self.title, start_label, start_label[:-1])
data = self.spreadsheet.values_get(
range_label,
params={
'valueRenderOption': value_render_option,
'majorDimension': 'COLUMNS'
}
)
try:
return data['values'][0]
except KeyError:
return []
def update_acell(self, label, value):
"""Updates the value of a cell.
:param label: Cell label in A1 notation.
Letter case is ignored.
:type label: str
:param value: New value.
Example::
worksheet.update_acell('A1', '42')
"""
return self.update_cell(*(a1_to_rowcol(label)), value=value)
def update_cell(self, row, col, value):
"""Updates the value of a cell.
:param row: Row number.
:type row: int
:param col: Column number.
:type col: int
:param value: New value.
Example::
worksheet.update_cell(1, 1, '42')
"""
range_label = '%s!%s' % (self.title, rowcol_to_a1(row, col))
data = self.spreadsheet.values_update(
range_label,
params={
'valueInputOption': 'USER_ENTERED'
},
body={
'values': [[value]]
}
)
return data
def update_cells(self, cell_list, value_input_option='RAW'):
"""Updates many cells at once.
:param cell_list: List of :class:`Cell` objects to update.
:param value_input_option: (optional) Determines how input data should
be interpreted. See `ValueInputOption`_ in
the Sheets API.
:type value_input_option: str
.. _ValueInputOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
Example::
# Select a range
cell_list = worksheet.range('A1:C7')
for cell in cell_list:
cell.value = 'O_o'
# Update in batch
worksheet.update_cells(cell_list)
"""
values_rect = cell_list_to_rect(cell_list)
start = rowcol_to_a1(min(c.row for c in cell_list), min(c.col for c in cell_list))
end = rowcol_to_a1(max(c.row for c in cell_list), max(c.col for c in cell_list))
range_label = '%s!%s:%s' % (self.title, start, end)
data = self.spreadsheet.values_update(
range_label,
params={
'valueInputOption': value_input_option
},
body={
'values': values_rect
}
)
return data
def resize(self, rows=None, cols=None):
"""Resizes the worksheet. Specify one of ``rows`` or ``cols``.
:param rows: (optional) New number of rows.
:type rows: int
:param cols: (optional) New number columns.
:type cols: int
"""
grid_properties = {}
if rows is not None:
grid_properties['rowCount'] = rows
if cols is not None:
grid_properties['columnCount'] = cols
if not grid_properties:
raise TypeError("Either 'rows' or 'cols' should be specified.")
fields = ','.join(
'gridProperties/%s' % p for p in grid_properties.keys()
)
body = {
'requests': [{
'updateSheetProperties': {
'properties': {
'sheetId': self.id,
'gridProperties': grid_properties
},
'fields': fields
}
}]
}
return self.spreadsheet.batch_update(body)
def update_title(self, title):
"""Renames the worksheet.
:param title: A new title.
:type title: str
"""
body = {
'requests': [{
'updateSheetProperties': {
'properties': {
'sheetId': self.id,
'title': title
},
'fields': 'title'
}
}]
}
response = self.spreadsheet.batch_update(body)
self._properties['title'] = title
return response
def add_rows(self, rows):
"""Adds rows to worksheet.
:param rows: Number of new rows to add.
:type rows: int
"""
self.resize(rows=self.row_count + rows)
def add_cols(self, cols):
"""Adds colums to worksheet.
:param cols: Number of new columns to add.
:type cols: int
"""
self.resize(cols=self.col_count + cols)
def append_row(self, values, value_input_option='RAW'):
"""Adds a row to the worksheet and populates it with values.
Widens the worksheet if there are more values than columns.
:param values: List of values for the new row.
:param value_input_option: (optional) Determines how input data should
be interpreted. See `ValueInputOption`_ in
the Sheets API.
:type value_input_option: str
.. _ValueInputOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
"""
params = {
'valueInputOption': value_input_option
}
body = {
'values': [values]
}
return self.spreadsheet.values_append(self.title, params, body)
def insert_row(
self,
values,
index=1,
value_input_option='RAW'
):
"""Adds a row to the worksheet at the specified index
and populates it with values.
Widens the worksheet if there are more values than columns.
:param values: List of values for the new row.
:param index: (optional) Offset for the newly inserted row.
:type index: int
:param value_input_option: (optional) Determines how input data should
be interpreted. See `ValueInputOption`_ in
the Sheets API.
:type value_input_option: str
.. _ValueInputOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
"""
body = {
"requests": [{
"insertDimension": {
"range": {
"sheetId": self.id,
"dimension": "ROWS",
"startIndex": index - 1,
"endIndex": index
}
}
}]
}
self.spreadsheet.batch_update(body)
range_label = '%s!%s' % (self.title, 'A%s' % index)
data = self.spreadsheet.values_update(
range_label,
params={
'valueInputOption': value_input_option
},
body={
'values': [values]
}
)
return data
def delete_row(self, index):
""""Deletes the row from the worksheet at the specified index.
:param index: Index of a row for deletion.
:type index: int
"""
body = {
"requests": [{
"deleteDimension": {
"range": {
"sheetId": self.id,
"dimension": "ROWS",
"startIndex": index - 1,
"endIndex": index
}
}
}]
}
return self.spreadsheet.batch_update(body)
def clear(self):
"""Clears all cells in the worksheet.
"""
return self.spreadsheet.values_clear(self.title)
def _finder(self, func, query):
data = self.spreadsheet.values_get(self.title)
try:
values = fill_gaps(data['values'])
except KeyError:
values = []
cells = [
Cell(row=i + 1, col=j + 1, value=value)
for i, row in enumerate(values)
for j, value in enumerate(row)
]
if isinstance(query, basestring):
match = lambda x: x.value == query
else:
match = lambda x: query.search(x.value)
return func(match, cells)
def find(self, query):
"""Finds the first cell matching the query.
:param query: A literal string to match or compiled regular expression.
:type query: str, :py:class:`re.RegexObject`
"""