]> git.cworth.org Git - turbot/blob - turbot/sheets.py
Create a sheet for the hunt when creating a new hunt
[turbot] / turbot / sheets.py
1 PUZZLE_TEMPLATE_ID = "1drSoyrE4gM3JaGweDkOybwXWdKPIDTfUmB1gQCYS3Uw"
2 PUZZLE_TEMPLATE_SHEETS = ["Text", "Grid"]
3
4 def sheets_create(turb, name):
5     """Create a new sheet with the given name.
6
7     Returns the dict with 'id' and 'url'of the spreadsheet.
8     """
9
10     # Create a new spreadsheet
11     spreadsheet_body = {
12         'properties': {
13             'title': name
14         }
15     }
16
17     new_sheet = turb.sheets.create(body=spreadsheet_body).execute()
18
19     return {
20         'id': new_sheet["spreadsheetId"],
21         'url': new_sheet["spreadsheetUrl"]
22     }
23
24 def sheets_create_for_puzzle(turb, name):
25     """Creates a new sheet for a puzzle of the given name
26
27     Like sheets_create(), but also copies the puzzle template sheet."""
28
29     # First create the new sheet
30     new_sheet = sheets_create(turb, name)
31
32     # Copy some sheets from the Template spreadsheet
33
34     response = turb.sheets.get(spreadsheetId=PUZZLE_TEMPLATE_ID).execute()
35
36     for sheet in response["sheets"]:
37         if sheet["properties"]["title"] in PUZZLE_TEMPLATE_SHEETS:
38             turb.sheets.sheets().copyTo(spreadsheetId=PUZZLE_TEMPLATE_ID,
39                                         sheetId=sheet["properties"]["sheetId"],
40                                         body={
41                                             "destinationSpreadsheetId":
42                                             new_sheet['id']
43                                         }).execute()
44
45     return new_sheet