I played around with the google sheets api v4 because it looks quite interesting with the ability to also render charts. I'm using the google api client for php.
First of all I created me a new spreadsheet with two sheets and filled in data on the first sheet. This worked as expected.
Then I wanted to render a chart, based on the data on the first sheet, on the second sheet. I wanted to start of the easy way with a Pie Chart because you only have one data series there.
I always end up with the following error message:
"message": "Invalid requests[0].addChart: No grid with id: 1"
The only id that I set is the one for the charts anchor cell for the second sheet I already created:
$googleSheetsSheetGridCoordinate = new Google_Service_Sheets_GridCoordinate();
$googleSheetsSheetGridCoordinate->setSheetId(1);
$googleSheetsSheetGridCoordinate->setColumnIndex(0);
$googleSheetsSheetGridCoordinate->setRowIndex(0);
$googleSheetsSheetOverlayPosition = new Google_Service_Sheets_OverlayPosition();
$googleSheetsSheetOverlayPosition->setAnchorCell($googleSheetsSheetGridCoordinate);
$googleSheetsSheetOverlayPosition->setHeightPixels(500);
$googleSheetsSheetOverlayPosition->setWidthPixels(700);
Taking a look into the spreadsheet, there is a sheet with id: 1 and it has also the type grid, so i have no idea what the problem might be here.
Update Here is the post Body of my addChart request:
{
"requests":[
{
"addChart":{
"chart":{
"spec":{
"title":"Pie Chart",
"pieChart":{
"legendPosition":"BOTTOM_LEGEND",
"domain":{
"sourceRange":{
"sources":[
{
"endRowIndex":3,
"sheetId":0,
"startColumnIndex":0,
"startRowIndex":2
}
]
}
},
"series":{
"sourceRange":{
"sources":{
"endRowIndex":4,
"sheetId":0,
"startColumnIndex":0,
"startRowIndex":3
}
}
}
}
},
"position":{
"overlayPosition":{
"heightPixels":500,
"widthPixels":700,
"anchorCell":{
"columnIndex":0,
"rowIndex":0,
"sheetId":1
}
}
}
}
}
}
]
}
When I compare it with the example, the only one I could find that covers adding charts, https://codelabs.developers.google.com/codelabs/sheets-api/#9, it looks correct to me.