Google Sheet API 附加到特定 sheet

Google Sheet API append to a specific sheet

根据 API 看来我无法指定 sheet 在何处追加数据。它附加第一个 sheet 中的所有内容。真的是这样吗?

因为我可以创建一个包含多个 sheet 的文件,但是我无法在第二个 sheet 上写入?

如果您只是想写在 sheet 上,请遵循 Basic Writing Sheets guide。您可以使用 PUT 方法在 sheet 上写入:

PUT https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5?valueInputOption=USER_ENTERED

并指定 sheet 的名称和单元格范围,例如:

{
  "range": "Sheet1!A1:D5",
  "majorDimension": "ROWS",
  "values": [
    ["Item", "Cost", "Stocked", "Ship Date"],
    ["Wheel", ".50", "4", "3/1/2016"],
    ["Door", "", "2", "3/15/2016"],
    ["Engine", "0", "1", "30/20/2016"],
    ["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]
  ],
}

关于您的 spreadsheets.values.append 问题,我相信您已经阅读了文档所说的内容:

Appends values to a spreadsheet. The input range is used to search for existing data and find a "table" within that range. Values will be appended to the next row of the table, starting with the first column of the table.

这仅用于在 table 的最后一行添加。

解决方法是在 awful-as-always Google Sheets API docs.[=13 上找到的 {range} 参数中指定 sheet 标题=]

以下是更新 sheet2 上的值的请求...

URL:

PUT https://sheets.googleapis.com/v4/spreadsheets/{someSpreadsheetId}/values/sheet2!A1:D3?valueInputOption=USER_ENTERED

BODY:

{
  "values": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]
}

请注意,第二个 sheet、"sheet2" 的标题在我认为范围 "A1:D3" 的前面加上感叹号分隔符。 sheet2!A1:D3