我使用了google sheets api v4,因为它看起来非常有趣,还具有渲染图表的能力。我正在使用谷歌api客户端php。
首先,我创建了一个有两张表格的新电子表格,并在第一张表格上填写数据。
然后我想渲染一个图表,基于第一张表上的数据,在第二张表上。我想从饼图开始,因为你只有一个数据系列。
我总是以以下错误消息结束:
"message": "Invalid requests[0]. "addChart:没有id: 1"的网格
我设置的唯一id是我已经创建的第二个工作表的图表锚定单元格的id:
$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);
看一下电子表格,有一个id: 1的表格,它也有网格类型,所以我不知道问题可能在这里。
Update这是我的addChart请求的post Body:
{
"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
}
}
}
}
}
}
]
}
当我将它与示例进行比较时,我能找到的唯一一个包含添加图表的示例https://codelabs.developers.google.com/codelabs/sheets-api/#9,对我来说它看起来是正确的。
好了,我找到解决办法了。我在想,sheetId是表的索引,但它是一个id一个表得到一旦它被创建。
所以解决方案是得到正确的id:
$sourceId = $googleSheetsSpreadsheet->getSheets()[0]->getProperties()->getSheetId();
$targetId = $googleSheetsSpreadsheet->getSheets()[1]->getProperties()->getSheetId();
这些id是在您上传电子表格时生成的,所以恐怕还不可能在一个创建请求中创建带有图表的工作表,但您必须首先创建所需的工作表,然后才能在另一个请求中添加图表。