]> git.cworth.org Git - turbot/blob - turbot/sheets.py
f2a9681f7c7483593d6cfad973076dadf802c61e
[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     # Now that we've created a new sheet, we need to also allow anyone
20     # with the link to the sheet to be able to edit it.
21     turb.permissions.create(fileId=new_sheet["spreadsheetId"],
22                             body={'type': 'anyone', 'role': 'writer'},
23                             fields='id').execute()
24
25     return {
26         'id': new_sheet["spreadsheetId"],
27         'url': new_sheet["spreadsheetUrl"]
28     }
29
30 def sheets_create_for_puzzle(turb, name):
31     """Creates a new sheet for a puzzle of the given name
32
33     Like sheets_create(), but also copies the puzzle template sheet."""
34
35     # First create the new sheet
36     new_sheet = sheets_create(turb, name)
37
38     # Copy some sheets from the Template spreadsheet
39
40     response = turb.sheets.get(spreadsheetId=PUZZLE_TEMPLATE_ID).execute()
41
42     for sheet in response["sheets"]:
43         if sheet["properties"]["title"] in PUZZLE_TEMPLATE_SHEETS:
44             turb.sheets.sheets().copyTo(spreadsheetId=PUZZLE_TEMPLATE_ID,
45                                         sheetId=sheet["properties"]["sheetId"],
46                                         body={
47                                             "destinationSpreadsheetId":
48                                             new_sheet['id']
49                                         }).execute()
50
51     return new_sheet