Google Sheets API v4 - 单元格的粗体部分

tra*_*vis 6 google-sheets google-sheets-api

手动编辑 Google 表格时,我注意到只需突出显示文本的一部分并单击“粗体”按钮,即可将单元格的一部分设为粗体。

有没有办法使用 API v4 实现相同的行为?我似乎在文档https://developers.google.com/sheets/api中找不到任何内容

imgur 中部分粗体单元格的图片

Tan*_*ike 6

我认为Sheets API中的“TextFormatRun”与spreadsheets.batchUpdate可以实现您的目标。

作为示例情况,假设电子表格上的单元格“A1”的值为 ,hi i'm bold - and I'm not并且您想要执行粗体hi i'm bold类型hi i'm bold - and I'm not

在这种情况下,以下端点和请求正文可以实现此目的。

端点:

https://sheets.googleapis.com/v4/spreadsheets/###spreadsheetId###:batchUpdate
Run Code Online (Sandbox Code Playgroud)

请求正文:

{"requests": [
    {"updateCells": {
        "range": {"sheetId": "###sheetId###", "startRowIndex": 0, "endRowIndex": 1, "startColumnIndex": 0, "endColumnIndex": 1},
        "rows": [{"values": [{"textFormatRuns":[
            {"format": {"bold": true}, "startIndex": 0},
            {"format": {"bold": false}, "startIndex": 11}
        ]}]}],
        "fields": "textFormatRuns.format.bold"
    }}
]}
Run Code Online (Sandbox Code Playgroud)
  • 使用时,请设置电子表格ID和工作表ID。

参考:

如果我误解了你的问题并且这不是你想要的方向,我很抱歉。