是否可以使用 Google 电子表格 API 在单元格中添加评论?
Posted
技术标签:
【中文标题】是否可以使用 Google 电子表格 API 在单元格中添加评论?【英文标题】:Is it possible to use the Google Spreadsheet API to add a comment in a cell? 【发布时间】:2012-10-06 12:06:05 【问题描述】:我正在查看 CellEntry API (https://developers.google.com/gdata/javadoc/com/google/gdata/data/spreadsheet/CellEntry) 以了解如何添加 cmets(以及理想情况下的注释) ) 到一个单元格,但看不到像“addComment()”这样的明显内容。
有人有想法吗?
谢谢
【问题讨论】:
【参考方案1】:基于Lars Gunnar Vik 的答案,这里有一个 Python 示例。
相关的代码在这里:
body =
"requests": [
"repeatCell":
"range":
"sheetId": 1704890600, # this is the end bit of the url
"startRowIndex": 0,
"endRowIndex": 1,
"startColumnIndex": 0,
"endColumnIndex": 1,
,
"cell": "note": "Hey, I'm a comment!",
"fields": "note",
]
result = (
service.spreadsheets()
.batchUpdate(spreadsheetId=SAMPLE_SPREADSHEET_ID, body=body)
.execute()
)
您可以通过重复 "requests": []
列表中的对象来添加大量 cmets。
需要注意的一些重要点是:
"sheetId"
是 url 中的结束号
如果您想添加值和 cmets,我认为您需要分两次执行(我希望有人能证明我的错误!)
下面是写评论的完整程序:
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ["*", "https://www.googleapis.com/auth/spreadsheets"]
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = "YOUR SPREADSHEET ID"
def main():
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
"credentials.json", SCOPES
)
try:
creds = flow.run_local_server()
except OSError as e:
print(e)
creds = flow.run_console()
# Save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
service = build("sheets", "v4", credentials=creds)
# add a comment
body =
"requests": [
"repeatCell":
"range":
"sheetId": 1704890600, # this is the end bit of the url
"startRowIndex": 0,
"endRowIndex": 1,
"startColumnIndex": 0,
"endColumnIndex": 1,
,
"cell": "note": "Hey, I'm a comment!",
"fields": "note",
]
result = (
service.spreadsheets()
.batchUpdate(spreadsheetId=SAMPLE_SPREADSHEET_ID, body=body)
.execute()
)
print("0 cells updated.".format(result.get("totalUpdatedCells")))
if __name__ == "__main__":
main()
您需要添加自己的电子表格 ID
【讨论】:
这是注释,不是评论【参考方案2】:借助 google sheet API v4,您可以使用电子表格.batchUpdate 设置注释。示例 javascript sdk:
var requests = [];
requests.push(
"repeatCell":
"range":
"sheetId": yourSheetId,
"startRowIndex": 1,
"endRowIndex": 2,
"startColumnIndex": 0,
"endColumnIndex": 1
,
"cell":
note: "Your note"
,
"fields": "note"
);
gapi.client.sheets.spreadsheets.batchUpdate(
spreadsheetId: yourDocumentId,
requests: requests
).then(function(response)
console.log(response);
callback();
);
【讨论】:
【参考方案3】:根据 Google 的说法,它还没有在 API 中。
Source
【讨论】:
以上是关于是否可以使用 Google 电子表格 API 在单元格中添加评论?的主要内容,如果未能解决你的问题,请参考以下文章
Google Sheets API:创建电子表格或将电子表格移动到文件夹中
如何使用 API 格式化 Google 表格电子表格单元格?
如何使用 Python 访问(读取、写入)Google Sheets 电子表格?