X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=turbot%2Fsheets.py;h=76515ca09547c660a94466fd6d223259211b7f44;hb=265710fa607c4faf79f304cbf323b6fb1a152949;hp=37bd363ac8a85270d90df6d16b4415b47dd72bf2;hpb=6fe04c2dc64b09bb0b8bbe7885781358c28b0810;p=turbot diff --git a/turbot/sheets.py b/turbot/sheets.py index 37bd363..76515ca 100644 --- a/turbot/sheets.py +++ b/turbot/sheets.py @@ -2,46 +2,56 @@ PUZZLE_TEMPLATE_ID = "1drSoyrE4gM3JaGweDkOybwXWdKPIDTfUmB1gQCYS3Uw" PUZZLE_TEMPLATE_SHEETS = ["Text", "Square grid", "Hex Grid", "Formula reference: indexing"] -def sheets_create(turb, name): - """Create a new spreadsheet with the given name. +def sheets_create_folder(turb, folder_name, parents = None): + """Create a new folder within Google Drive - Returns a dict with 'id' and 'url' of the spreadsheet - """ + Returns the id of the created folder.""" - spreadsheet = create_spreadsheet(turb, name) - - return { - 'id': spreadsheet['spreadsheetId'], - 'url': spreadsheet['spreadsheetUrl'] + body = { + "name": folder_name, + "mimeType": "application/vnd.google-apps.folder" } -def create_spreadsheet(turb, name): - """ - Returns the request's dict which has at least the following keys: + if parents: + body["parents"] = parents + + folder = turb.files.create(body=body, fields='id').execute() - ['spreadsheetId']: ID for this spreadsheet - ['spreadsheetUrl']: URL of this spreadsheet - ['sheets'][0]['properties']['sheetId']: ID of first sheet inside + return folder.get('id') + +def sheets_create(turb, name, folder_id): + """Create a new spreadsheet with the given name. + + Returns a dict with 'id' and 'url' of the spreadsheet """ - # Create a new spreadsheet - spreadsheet_body = { - 'properties': { - 'title': name - } + body = { + "name": name, + "parents": [folder_id], + "mimeType": "application/vnd.google-apps.spreadsheet" } - spreadsheet = turb.sheets.create(body=spreadsheet_body).execute() + spreadsheet = turb.files.create(body=body, fields='id').execute() + id = spreadsheet['id'] + + # The files.create call gives us the ID for our new sheet, but we + # need the URL for it as well. So we get that with the sheets API. + spreadsheet = turb.sheets.get(spreadsheetId=id, + fields='spreadsheetUrl').execute() + url = spreadsheet['spreadsheetUrl'] - # Now that we've created a new spreadsheet, we need to also allow - # anyone with the link to the sheet to be able to edit it. - turb.permissions.create(fileId=spreadsheet["spreadsheetId"], + # Finally, we want to also allow anyone with the link to the sheet + # to be able to edit it. + turb.permissions.create(fileId=id, body={'type': 'anyone', 'role': 'writer'}, - fields='id').execute() + fields='').execute() - return spreadsheet + return { + 'id': id, + 'url': url + } -def sheets_create_for_puzzle(turb, puzzle): +def sheets_create_for_puzzle(turb, puzzle, folder_id): """Creates a new sheet for a puzzle of the given name Like sheets_create(), but also copies the puzzle template sheet. @@ -51,11 +61,16 @@ def sheets_create_for_puzzle(turb, puzzle): """ # First create the new spreadsheet - spreadsheet = create_spreadsheet(turb, puzzle['name']) - spreadsheet_id = spreadsheet['spreadsheetId'] + spreadsheet = sheets_create(turb, puzzle['name'], folder_id) + spreadsheet_id = spreadsheet['id'] + spreadsheet_url = spreadsheet['url'] - # Then, copy some useful sheets over from the Template spreadsheet + # And fetch the individual "sheets" from the spreadsheet + spreadsheet = turb.sheets.get(spreadsheetId=spreadsheet_id, + fields='sheets').execute() + first_sheet_id = spreadsheet['sheets'][0]['properties']['sheetId'] + # Then, copy some useful sheets over from the Template spreadsheet response = turb.sheets.get(spreadsheetId=PUZZLE_TEMPLATE_ID).execute() for sheet in response["sheets"]: @@ -66,41 +81,42 @@ def sheets_create_for_puzzle(turb, puzzle): body={ "destinationSpreadsheetId": spreadsheet_id }).execute() - # Rename each copied sheet to match original name - rename_sheet(turb, spreadsheet_id, res['sheetId'], - sheet["properties"]["title"]) + # Rename each copied sheet to match the name from the template + sheet_name = sheet["properties"]["title"] + # Except for "Text" which we rename to the puzzle name + if sheet_name == "Text": + sheet_name = puzzle['name'] + rename_sheet(turb, spreadsheet_id, res['sheetId'], sheet_name) - # Next, delete the blank sheet that's was created before the template - sheet_id = spreadsheet['sheets'][0]['properties']['sheetId'] - delete_sheet(turb, spreadsheet_id, sheet_id) + # Next, delete the blank sheet made at spreadsheet creation time + delete_sheet(turb, spreadsheet_id, first_sheet_id) # Insert some useful links into the sheet where expected if 'url' in puzzle: url_link = puzzle['url'] url_text = "Original puzzle link" - spreadsheet_insert_data( + spreadsheet_update_data( turb, spreadsheet_id, "B1:B1", '=HYPERLINK("{}","{}")'.format(url_link, url_text)) if 'channel_url' in puzzle: url_link = puzzle['channel_url'] url_text = "Slack channel link" - spreadsheet_insert_data( + spreadsheet_update_data( turb, spreadsheet_id, "B2:B2", '=HYPERLINK("{}","{}")'.format(url_link, url_text)) return { 'id': spreadsheet_id, - 'url': spreadsheet['spreadsheetUrl'] + 'url': spreadsheet_url } -def spreadsheet_insert_data(turb, spreadsheet_id, range, text): +def spreadsheet_update_data(turb, spreadsheet_id, range, text): - turb.sheets.values().append( + turb.sheets.values().update( spreadsheetId=spreadsheet_id, range=range, valueInputOption='USER_ENTERED', - insertDataOption='INSERT_ROWS', body={ 'range': range, 'values': [