Google Sheet API append to a specific sheet

喜你入骨 提交于 2019-12-10 20:06:32

问题


according to the API it seems that I can't specify the sheet where to append the data. It appends everything in the first sheet. Is it really like this?

Beacuse I can create a file with multiple sheets, but then I can't write on the second sheet?


回答1:


If you simply want to write on a sheet, follow the Basic Writing Sheets guide. You can write on a sheet by using a PUT method:

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

and specify the name of sheet and cell range in like:

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

With regard to your spreadsheets.values.append question, I'm sure you've already read what the docs had to say:

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.

This is only for adding at the last row of the table.




回答2:


The solution is to specify the sheet title in the {range} parameter found on the awful-as-always Google Sheets API docs.

Below are the request deets on a updating the value on 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]
  ]
}

Note that the title of the second sheet, "sheet2" prepends what I would have considered the range: "A1:D3" with an exclamation delimiter. sheet2!A1:D3



来源:https://stackoverflow.com/questions/41067256/google-sheet-api-append-to-a-specific-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!