如何使用 Google Sheets API 添加超链接?

pyt*_*ete 2 google-sheets-api

我正在尝试编写一个 python 脚本来向谷歌表添加超链接。为此,我正在使用 google api。通过搜索,我发现我需要向其余 api 传递“=HYPERLINK()”类型的消息。

来自文档:https : //developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#ExtendedValue.FIELDS.formula_value

{
  // Union field value can be only one of the following:
  "numberValue": number,
  "stringValue": string,
  "boolValue": boolean,
  "formulaValue": string,
  "errorValue": {
    object(ErrorValue)
  }
  // End of list of possible types for union field value.
}
Run Code Online (Sandbox Code Playgroud)

看起来我应该使用“formulaValue”属性。

编辑:我尝试使用UpdateCells请求

编辑:下面的解决方案。

pyt*_*ete 6

我想到了:

def addHyperlink(self, hyperlink, text, sheetId, rowIndex, colIndex):
    requests = []
    requests.append({
        "updateCells": {
            "rows": [
                {
                    "values": [{
                        "userEnteredValue": {
                            "formulaValue":"=HYPERLINK({},{})".format(hyperlink, text) 
                        }
                    }]
                }
            ],
            "fields": "userEnteredValue",
            "start": {
                "sheetId": sheetId,
                "rowIndex": rowIndex,
                "columnIndex": colIndex
            }
        }})
    body = {
        "requests": requests
    }
    request = self.service.spreadsheets().batchUpdate(spreadsheetId=self.spreadsheetId, body=body)
    return request.execute()
Run Code Online (Sandbox Code Playgroud)