Skip to content

Instantly share code, notes, and snippets.

@hexvolt
Created October 28, 2019 00:31
Show Gist options
  • Save hexvolt/4eee9435b9398a17ddd1588ad6d830fe to your computer and use it in GitHub Desktop.
Save hexvolt/4eee9435b9398a17ddd1588ad6d830fe to your computer and use it in GitHub Desktop.
insert note to google spreadsheet
from gspread.urls import SPREADSHEETS_API_V4_BASE_URL
def insert_note(worksheet, label, note):
"""
Insert note ito the google worksheet for a certain cell.
Compatible with gspread.
"""
spreadsheet_id = worksheet.spreadsheet.id
worksheet_id = worksheet.id
row, col = a1_to_coords(label) # [0, 0] is A1
url = f"{SPREADSHEETS_API_V4_BASE_URL}/{spreadsheet_id}:batchUpdate"
payload = {
"requests": [
{
"updateCells": {
"range": {
"sheetId": worksheet_id,
"startRowIndex": row,
"endRowIndex": row + 1,
"startColumnIndex": col,
"endColumnIndex": col + 1
},
"rows": [
{
"values": [
{
"note": note
}
]
}
],
"fields": "note"
}
}
]
}
worksheet.spreadsheet.client.request("post", url, json=payload)
@patroqueeet
Copy link

a1_to_coords is from gspread.utils import a1_to_rowcol?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment