是否有可能使用谷歌电子表格API在单元格中添加评论?(Is it possible to use t

2019-08-01 12:46发布

我在看的CellEntry API(https://developers.google.com/gdata/javadoc/com/google/gdata/data/spreadsheet/CellEntry),看我怎么可以添加评论(理想音符和)到细胞,但没有看到任何明显的像“addComment()”。

人有一个想法?

谢谢

Answer 1:

与谷歌片API v4用户可以使用spreadsheets.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(); }); 



Answer 2:

据谷歌称,它不是在API中呢。

资源



Answer 3:

从答案号楼拉尔斯·贡纳尔·维克 ,这里是在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": []列表中。

需要注意的一些重要的位是:

  • "sheetId"是在URL中端号码
  • 如果你想添加值,和评论,我认为你需要做的是,在两个通道(我想有人来证明我是错上!)

下面是写评论的完整方案:

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添加



文章来源: Is it possible to use the Google Spreadsheet API to add a comment in a cell?