I am trying to update the chart dynamically that is embedded in a Google Slides, but it throws an error at me.
This is the function that I came up with to replace some specific text that is placed in my base Google slide.
public function getGoogleDrivePdf($fileId, $substitutions)
{
$client = $this->getGoogleClient();
$driveService = new \Google_Service_Drive($client);
$slidesService = new \Google_Service_Slides($client);
$copy = new \Google_Service_Drive_DriveFile([
'name' => $substitutions['student_name'] . ' presentation',
]);
$driveResponse = $driveService->files->copy($fileId, $copy);
$slideFileId = $driveResponse->id;
$requests = [];
foreach ($substitutions as $key => $value) {
$requests[] = new \Google_Service_Slides_Request(
[
'replaceAllText' => [
'containsText' => [
'text' => '{{'.$key.'}}',
'matchCase' => true
],
'replaceText' => $value
]
]
);
}
$requests[] = new \Google_Service_Slides_Request(
[
'createSheetsChart' => [
'spreadsheetId' => 'N_St1Lm-oxd4aWOjQ6o.......',
'chartId' => ...040....,
'linkingMode' => 'NOT_LINKED_IMAGE',
'elementProperties' => [
'pageObjectId' => '.....9fd6d_.....',
'size' => [
'height' => $emu4M,
'width' => $emu4M
],
'transform' => [
'scaleX' => 1,
'scaleY' => 1,
'translateX' => 100000,
'translateY' => 100000,
'unit' => 'EMU'
]
]
]
]
);
$batchUpdateRequest = new \Google_Service_Slides_BatchUpdatePresentationRequest([
'requests' => $requests,
]);
$updateResponse = $slidesService->presentations->batchUpdate($slideFileId, $batchUpdateRequest);
$response = $driveService->files->export(
$slideFileId,
'application/pdf',
[
'alt' => 'media',
]
);
$content = $response->getBody()->getContents();
$driveService->files->delete($slideFileId);
// Download
$response = new Response();
$response->headers->set('Content-Type', 'application/pdf');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $substitutions['slide_filename'] . '"');
$response->setContent($content);
return $response;
}
This function replaces the text I want that are in the Google Slide and downloads the slide as a PDF file without an issue.
But, now I want to add a chart to the slide and change the chart values dynamically. I tried to accomplish this by changing the values in source Google Sheet to replaceable text but It throws me this error.
{ "error": { "code": 400, "message": "Invalid requests[9].createSheetsChart: The specified chart could not be found in the spreadsheet.", "errors": [ { "message": "Invalid requests[9].createSheetsChart: The specified chart could not be found in the spreadsheet.", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT" } }
Any idea how to fix this or any workarounds?
now I want to add a chart to the slide and change the chart values dynamically. I tried to accomplish this by changing the values in source Google Sheet to replaceable text but It throws me this error., can you provide your current script?change the chart values dynamically? By the way, I think that the error ofThe specified chart could not be found in the spreadsheet."is due to the chart ID is not found in the Spreadsheet. So, I cannot still understand your expected result. Can I ask you about the details of it?dynamicallyyou expect. I deeply apologize for my poor English skill.