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 sheets_create_for_puzzle(turb, puzzle, folder_id):
52 """Creates a new sheet for a puzzle of the given name
54 Like sheets_create(), but also copies the puzzle template sheet.
56 Here, 'puzzle' is a dict that must have a 'name' key and may optionally
57 have a 'channel_url' or 'url' key.
60 # First create the new spreadsheet
61 spreadsheet = sheets_create(turb, puzzle['name'], folder_id)
62 spreadsheet_id = spreadsheet['id']
63 spreadsheet_url = spreadsheet['url']
65 # And fetch the individual "sheets" from the spreadsheet
66 spreadsheet = turb.sheets.get(spreadsheetId=spreadsheet_id,
67 fields='sheets').execute()
68 first_sheet_id = spreadsheet['sheets'][0]['properties']['sheetId']
70 # Then, copy some useful sheets over from the Template spreadsheet
71 response = turb.sheets.get(spreadsheetId=PUZZLE_TEMPLATE_ID).execute()
73 for sheet in response["sheets"]:
74 if sheet["properties"]["title"] in PUZZLE_TEMPLATE_SHEETS:
75 res = turb.sheets.sheets().copyTo(
76 spreadsheetId=PUZZLE_TEMPLATE_ID,
77 sheetId=sheet["properties"]["sheetId"],
79 "destinationSpreadsheetId": spreadsheet_id
81 # Rename each copied sheet to match the name from the template
82 sheet_name = sheet["properties"]["title"]
83 # Except for "Text" which we rename to the puzzle name
84 if sheet_name == "Text":
85 sheet_name = puzzle['name']
86 rename_sheet(turb, spreadsheet_id, res['sheetId'], sheet_name)
88 # Next, delete the blank sheet made at spreadsheet creation time
89 delete_sheet(turb, spreadsheet_id, first_sheet_id)
91 # Insert some useful links into the sheet where expected
93 url_link = puzzle['url']
94 url_text = "Original puzzle link"
95 spreadsheet_insert_data(
96 turb, spreadsheet_id, "B1:B1",
97 '=HYPERLINK("{}","{}")'.format(url_link, url_text))
99 if 'channel_url' in puzzle:
100 url_link = puzzle['channel_url']
101 url_text = "Slack channel link"
102 spreadsheet_insert_data(
103 turb, spreadsheet_id, "B2:B2",
104 '=HYPERLINK("{}","{}")'.format(url_link, url_text))
107 'id': spreadsheet_id,
108 'url': spreadsheet_url
111 def spreadsheet_insert_data(turb, spreadsheet_id, range, text):
113 turb.sheets.values().append(
114 spreadsheetId=spreadsheet_id,
116 valueInputOption='USER_ENTERED',
117 insertDataOption='INSERT_ROWS',
126 def delete_sheet(turb, spreadsheet_id, sheet_id):
136 turb.sheets.batchUpdate(spreadsheetId=spreadsheet_id,
140 def rename_sheet(turb, spreadsheet_id, sheet_id, name):
144 'updateSheetProperties': {
154 turb.sheets.batchUpdate(spreadsheetId=spreadsheet_id,
158 def spreadsheet_id_from_url(url):
159 # Google sheet ids are between the /d/ and /edit in the url, like
160 # https://docs.google.com/spreadsheets/d/1dxHBzjen...-LaXeVPrg/edit#gid=0
161 start = url.find('/d/') + 3
162 end = url.find('/edit')
163 return url[start:end]
165 def rename_spreadsheet(turb, spreadsheet_url, name):
167 spreadsheet_id = spreadsheet_id_from_url(spreadsheet_url)
171 'updateSpreadsheetProperties': {
180 turb.sheets.batchUpdate(spreadsheetId=spreadsheet_id,