forked from googleworkspace/python-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspreadsheet_writer.py
More file actions
139 lines (123 loc) Β· 4.4 KB
/
spreadsheet_writer.py
File metadata and controls
139 lines (123 loc) Β· 4.4 KB
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
# Copyright 2018 Google LLC
#
# 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.
# pylint: disable=E1102
# python3
"""Functionality for creating and writing to a spreadsheet."""
def CreateSpreadsheet(sheets_service, title, sheet_titles):
"""Creates an empty spreadsheet.
It creates a spreadsheet with the provided title, and creates a sheet for
each entry in the sheet_titles list with the corresponding sheet title.
"""
sheets = []
for sheet_title in sheet_titles:
sheet = {
'properties': {
'title': sheet_title,
},
}
sheets.append(sheet)
spreadsheet = {
'properties': {
'title': title,
},
'sheets': sheets,
}
return sheets_service.spreadsheets().create(body=spreadsheet).execute()
class SpreadsheetWriter(object):
"""Queues writes for modifying a spreadsheet.
Call ExecuteBatchUpdate to flush pending writes.
"""
def __init__(self, sheets_service, spreadsheet_id):
self._sheets_service = sheets_service
self._spreadsheet_id = spreadsheet_id
self._requests = []
def InsertColumn(self, sheet_id, column_index):
request = {
'insertDimension': {
'range': {
'sheetId': sheet_id,
'dimension': 'COLUMNS',
'startIndex': column_index,
'endIndex': column_index + 1,
},
}
}
self._requests.append(request)
def PopulateColumn(self, sheet_id, column_index, column_id, values):
# Include the column ID in the column values
values = [column_id] + values
# Populate the column with the values
rows = []
for value in values:
row_data = {
'values': [
{
'userEnteredValue': {
'stringValue': value
}
}
]
}
rows.append(row_data)
update_request = {
'updateCells': {
'rows': rows,
'fields': 'userEnteredValue',
'start': {
'sheetId': sheet_id,
'rowIndex': 0,
'columnIndex': column_index
}
}
}
self._requests.append(update_request)
# Add developer metadata to the column to make it easier to read later
# by being able to just query it by the column ID
metadata_request = {
'createDeveloperMetadata': {
'developerMetadata': {
'metadataKey': 'column_id',
'metadataValue': column_id,
'location': {
'dimensionRange': {
'sheetId': sheet_id,
'dimension': 'COLUMNS',
'startIndex': column_index,
'endIndex': column_index + 1,
}
},
'visibility': 'DOCUMENT',
}
}
}
self._requests.append(metadata_request)
def AddTemplateIdToSpreadsheetMetadata(self, template_id):
request = {
'createDeveloperMetadata': {
'developerMetadata': {
'metadataKey': 'template_id',
'metadataValue': template_id,
'location': {
'spreadsheet': True
},
'visibility': 'DOCUMENT',
}
}
}
self._requests.append(request)
def ExecuteBatchUpdate(self):
body = {'requests': self._requests}
self._requests = []
return self._sheets_service.spreadsheets().batchUpdate(
spreadsheetId=self._spreadsheet_id, body=body).execute()