1 PUZZLE_TEMPLATE_ID = "1drSoyrE4gM3JaGweDkOybwXWdKPIDTfUmB1gQCYS3Uw"
2 PUZZLE_TEMPLATE_SHEETS = ["Text", "Square grid", "Hex Grid",
3 "Formula reference: indexing"]
5 def sheets_create_folder(turb, folder_name):
6 """Create a new folder within Google Drive
8 Returns the id of the created folder."""
12 "mimeType": "application/vnd.google-apps.folder"
15 folder = turb.files.create(body=body, fields='id').execute()
17 return folder.get('id')
19 def sheets_create(turb, name, folder_id):
20 """Create a new spreadsheet with the given name.
22 Returns a dict with 'id' and 'url' of the spreadsheet
27 "parents": [folder_id],
28 "mimeType": "application/vnd.google-apps.spreadsheet"
31 spreadsheet = turb.files.create(body=body, fields='id').execute()
32 id = spreadsheet['id']
34 # The files.create call gives us the ID for our new sheet, but we
35 # need the URL for it as well. So we get that with the sheets API.
36 spreadsheet = turb.sheets.get(spreadsheetId=id,
37 fields='spreadsheetUrl').execute()
38 url = spreadsheet['spreadsheetUrl']
40 # Finally, we want to also allow anyone with the link to the sheet
41 # to be able to edit it.
42 turb.permissions.create(fileId=id,
43 body={'type': 'anyone', 'role': 'writer'},
51 def create_spreadsheet(turb, name):
53 Returns the request's dict which has at least the following keys:
55 ['spreadsheetId']: ID for this spreadsheet
56 ['spreadsheetUrl']: URL of this spreadsheet
57 ['sheets'][0]['properties']['sheetId']: ID of first sheet inside
60 # Create a new spreadsheet
67 spreadsheet = turb.sheets.create(body=spreadsheet_body).execute()
69 # Now that we've created a new spreadsheet, we need to also allow
70 # anyone with the link to the sheet to be able to edit it.
71 turb.permissions.create(fileId=spreadsheet["spreadsheetId"],
72 body={'type': 'anyone', 'role': 'writer'},
73 fields='id').execute()
77 def sheets_create_for_puzzle(turb, puzzle):
78 """Creates a new sheet for a puzzle of the given name
80 Like sheets_create(), but also copies the puzzle template sheet.
82 Here, 'puzzle' is a dict that must have a 'name' key and may optionally
83 have a 'channel_url' or 'url' key.
86 # First create the new spreadsheet
87 spreadsheet = create_spreadsheet(turb, puzzle['name'])
88 spreadsheet_id = spreadsheet['spreadsheetId']
90 # Then, copy some useful sheets over from the Template spreadsheet
92 response = turb.sheets.get(spreadsheetId=PUZZLE_TEMPLATE_ID).execute()
94 for sheet in response["sheets"]:
95 if sheet["properties"]["title"] in PUZZLE_TEMPLATE_SHEETS:
96 res = turb.sheets.sheets().copyTo(
97 spreadsheetId=PUZZLE_TEMPLATE_ID,
98 sheetId=sheet["properties"]["sheetId"],
100 "destinationSpreadsheetId": spreadsheet_id
102 # Rename each copied sheet to match the name from the template
103 sheet_name = sheet["properties"]["title"]
104 # Except for "Text" which we rename to the puzzle name
105 if sheet_name == "Text":
106 sheet_name = puzzle['name']
107 rename_sheet(turb, spreadsheet_id, res['sheetId'], sheet_name)
109 # Next, delete the blank sheet that's was created before the template
110 sheet_id = spreadsheet['sheets'][0]['properties']['sheetId']
111 delete_sheet(turb, spreadsheet_id, sheet_id)
113 # Insert some useful links into the sheet where expected
115 url_link = puzzle['url']
116 url_text = "Original puzzle link"
117 spreadsheet_insert_data(
118 turb, spreadsheet_id, "B1:B1",
119 '=HYPERLINK("{}","{}")'.format(url_link, url_text))
121 if 'channel_url' in puzzle:
122 url_link = puzzle['channel_url']
123 url_text = "Slack channel link"
124 spreadsheet_insert_data(
125 turb, spreadsheet_id, "B2:B2",
126 '=HYPERLINK("{}","{}")'.format(url_link, url_text))
129 'id': spreadsheet_id,
130 'url': spreadsheet['spreadsheetUrl']
133 def spreadsheet_insert_data(turb, spreadsheet_id, range, text):
135 turb.sheets.values().append(
136 spreadsheetId=spreadsheet_id,
138 valueInputOption='USER_ENTERED',
139 insertDataOption='INSERT_ROWS',
148 def delete_sheet(turb, spreadsheet_id, sheet_id):
158 turb.sheets.batchUpdate(spreadsheetId=spreadsheet_id,
162 def rename_sheet(turb, spreadsheet_id, sheet_id, name):
166 'updateSheetProperties': {
176 turb.sheets.batchUpdate(spreadsheetId=spreadsheet_id,
180 def spreadsheet_id_from_url(url):
181 # Google sheet ids are between the /d/ and /edit in the url, like
182 # https://docs.google.com/spreadsheets/d/1dxHBzjen...-LaXeVPrg/edit#gid=0
183 start = url.find('/d/') + 3
184 end = url.find('/edit')
185 return url[start:end]
187 def rename_spreadsheet(turb, spreadsheet_url, name):
189 spreadsheet_id = spreadsheet_id_from_url(spreadsheet_url)
193 'updateSpreadsheetProperties': {
202 turb.sheets.batchUpdate(spreadsheetId=spreadsheet_id,