1 PUZZLE_TEMPLATE_ID = "1drSoyrE4gM3JaGweDkOybwXWdKPIDTfUmB1gQCYS3Uw"
2 PUZZLE_TEMPLATE_SHEETS = ["Text", "Square grid", "Hex Grid",
3 "Formula reference: indexing"]
5 def sheets_create(turb, name):
6 """Create a new sheet with the given name.
8 Returns the dict with 'id' and 'url'of the spreadsheet.
11 # Create a new spreadsheet
18 new_sheet = turb.sheets.create(body=spreadsheet_body).execute()
20 # Now that we've created a new sheet, we need to also allow anyone
21 # with the link to the sheet to be able to edit it.
22 turb.permissions.create(fileId=new_sheet["spreadsheetId"],
23 body={'type': 'anyone', 'role': 'writer'},
24 fields='id').execute()
27 'id': new_sheet["spreadsheetId"],
28 'url': new_sheet["spreadsheetUrl"]
31 def sheets_create_for_puzzle(turb, puzzle):
32 """Creates a new sheet for a puzzle of the given name
34 Like sheets_create(), but also copies the puzzle template sheet.
36 Here, 'puzzle' is a dict that must have a 'name' key and may optionally
37 have a 'channel_url' or 'url' key."""
39 # First create the new sheet
40 new_sheet = sheets_create(turb, puzzle['name'])
42 # Insert some useful links into the sheet
45 url_text = "Original puzzle is at: {}".format(puzzle['url'])
46 url_link = puzzle['url']
51 if 'channel_url' in puzzle:
52 channel_url_text = "Discussion for this puzzle is at: {}".format(
53 puzzle['channel_url'])
54 channel_url_link = puzzle['channel_url']
58 turb.sheets.values().append(
59 spreadsheetId=new_sheet['id'],
61 valueInputOption='USER_ENTERED',
62 insertDataOption='INSERT_ROWS',
66 ['=HYPERLINK("'+url_link+'","'+url_text+'")'],
67 ['=HYPERLINK("'+channel_url_link+'","'+channel_url_text+'")']
71 # Copy some sheets from the Template spreadsheet
73 response = turb.sheets.get(spreadsheetId=PUZZLE_TEMPLATE_ID).execute()
75 for sheet in response["sheets"]:
76 if sheet["properties"]["title"] in PUZZLE_TEMPLATE_SHEETS:
77 turb.sheets.sheets().copyTo(spreadsheetId=PUZZLE_TEMPLATE_ID,
78 sheetId=sheet["properties"]["sheetId"],
80 "destinationSpreadsheetId":
86 def renameSheet(turb, url, newName):
87 id = extractIdFromSheetUrl(url)
90 'updateSpreadsheetProperties': {
102 turb.sheets.batchUpdate(spreadsheetId = id,
106 def extractIdFromSheetUrl(url):
107 # Google sheet ids are between the /d/ and /edit in the url, like
108 # https://docs.google.com/spreadsheets/d/1dxHBzjen...-LaXeVPrg/edit#gid=0
109 startIndex = url.find('/d/') + 3
110 endIndex = url.find('/edit')
111 return url[startIndex : endIndex]