X-Git-Url: https://git.cworth.org/git?p=turbot;a=blobdiff_plain;f=turbot%2Fsheets.py;h=ff8b1423f3385c8bdb700ccbbbb14aa3c7c5d7a5;hp=f2a9681f7c7483593d6cfad973076dadf802c61e;hb=ee51d944119832b3272bf0bbd330066d6dd5d658;hpb=b999fd0fecf4b209c39785bd9d2be6990ee1716e diff --git a/turbot/sheets.py b/turbot/sheets.py index f2a9681..ff8b142 100644 --- a/turbot/sheets.py +++ b/turbot/sheets.py @@ -1,5 +1,6 @@ PUZZLE_TEMPLATE_ID = "1drSoyrE4gM3JaGweDkOybwXWdKPIDTfUmB1gQCYS3Uw" -PUZZLE_TEMPLATE_SHEETS = ["Text", "Grid"] +PUZZLE_TEMPLATE_SHEETS = ["Text", "Square grid", "Hex Grid", + "Formula reference: indexing"] def sheets_create(turb, name): """Create a new sheet with the given name. @@ -27,13 +28,45 @@ def sheets_create(turb, name): 'url': new_sheet["spreadsheetUrl"] } -def sheets_create_for_puzzle(turb, name): +def sheets_create_for_puzzle(turb, puzzle): """Creates a new sheet for a puzzle of the given name - Like sheets_create(), but also copies the puzzle template sheet.""" + Like sheets_create(), but also copies the puzzle template sheet. + + Here, 'puzzle' is a dict that must have a 'name' key and may optionally + have a 'channel_url' or 'url' key.""" # First create the new sheet - new_sheet = sheets_create(turb, name) + new_sheet = sheets_create(turb, puzzle['name']) + + # Insert some useful links into the sheet + url_link='' + if 'url' in puzzle: + url_text = "Original puzzle is at: {}".format(puzzle['url']) + url_link = puzzle['url'] + else: + url_text = '' + + channel_url_link = '' + if 'channel_url' in puzzle: + channel_url_text = "Discussion for this puzzle is at: {}".format( + puzzle['channel_url']) + channel_url_link = puzzle['channel_url'] + else: + channel_url_text = '' + + turb.sheets.values().append( + spreadsheetId=new_sheet['id'], + range='A1:A2', + valueInputOption='USER_ENTERED', + insertDataOption='INSERT_ROWS', + body={ + 'range': 'A1:A2', + 'values': [ + ['=HYPERLINK("'+url_link+'","'+url_text+'")'], + ['=HYPERLINK("'+channel_url_link+'","'+channel_url_text+'")'] + ] + }).execute() # Copy some sheets from the Template spreadsheet @@ -41,11 +74,58 @@ def sheets_create_for_puzzle(turb, name): for sheet in response["sheets"]: if sheet["properties"]["title"] in PUZZLE_TEMPLATE_SHEETS: - turb.sheets.sheets().copyTo(spreadsheetId=PUZZLE_TEMPLATE_ID, - sheetId=sheet["properties"]["sheetId"], - body={ - "destinationSpreadsheetId": - new_sheet['id'] - }).execute() + res = turb.sheets.sheets().copyTo( + spreadsheetId=PUZZLE_TEMPLATE_ID, + sheetId=sheet["properties"]["sheetId"], + body={ + "destinationSpreadsheetId": new_sheet['id'] + }).execute() + rename_sheet(turb, new_sheet['id'], res['sheetId'], + sheet["properties"]["title"]) return new_sheet + +def rename_sheet(turb, spreadsheet_id, sheet_id, name): + + body = { + 'requests': [{ + 'updateSheetProperties': { + 'properties': { + 'sheetId': sheet_id, + 'title': name + }, + 'fields': 'title' + } + }] + } + + turb.sheets.batchUpdate(spreadsheetId=spreadsheet_id, + body=body + ).execute() + +def rename_spreadsheet(turb, url, newName): + id = extractIdFromSheetUrl(url) + requests = [] + requests.append({ + 'updateSpreadsheetProperties': { + 'properties': { + 'title': newName + }, + 'fields': 'title' + } + }) + + body = { + 'requests': requests + } + + turb.sheets.batchUpdate(spreadsheetId = id, + body=body + ).execute() + +def extractIdFromSheetUrl(url): + # Google sheet ids are between the /d/ and /edit in the url, like + # https://docs.google.com/spreadsheets/d/1dxHBzjen...-LaXeVPrg/edit#gid=0 + startIndex = url.find('/d/') + 3 + endIndex = url.find('/edit') + return url[startIndex : endIndex]