1
I have a script that takes information from a spreadsheet, replaces the text in a "template" document and generates a PDF for later sending by email. See an excerpt of my code below:
// Cria o novo documento, recupera o ID e o abre
var idCopia = DriveApp.getFileById(idTemplate).makeCopy(nomeDoArquivo).getId();
var docCopia = DocumentApp.openById(idCopia);
// recupera o corpo e o rodapé do documento
var bodyCopia = docCopia.getBody();
var footerCopia = docCopia.getFooter();
// faz o replace das variáveis do template, salva e fecha o novo documento
bodyCopia.replaceText('%MESREF%', docMesRef);
bodyCopia.replaceText('%PERIODO%', docPeriodo);
bodyCopia.replaceText('%CLIENTE%', docCliente);
bodyCopia.replaceText('%DATAREL%', docDataRel);
footerCopia.replaceText('%CONTRATO%', docContrato);
docCopia.saveAndClose();
// abre o documento temporario como PDF utilizando o seu ID
var pdf = DriveApp.getFileById(idCopia).getAs("application/pdf");
I didn’t glue the whole job together because it’s too big. The important thing is to know that the script, in this section, collects the spreadsheet data, replaces expressions in a template file (for example: %word%), saves and generates a PDF copy.
However, I need to include in this document 4 graphs that were generated in this same spreadsheet. I’ve done a lot of research and can’t find a clean alternative to doing this.
Currently, I inserted these 4 linked charts in the template file. So before I run the script, I open the template, update the 4 charts and only then run the script to generate the PDF of that template, with the updated graphics. Obviously, this alternative is very dumb on my part, but I couldn’t find a way to update these graphics via script.
I would like to run this script and also insert the 4 charts or update the existing ones in the template so that, when generating the PDF, is correct.
I ask for help from the community!