Im trying to set up this in Google App Sheets, where there’s info being added to a spreadsheet and a pdf is created from a template. I copied this code from a YT video:
function createBulkPDFs(){
const docFile = DriveApp.getFileById("ID");
const tempFolder = DriveApp.getFolderById("ID");
const pdfFolder = DriveApp.getFolderById("ID");
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Hoja 1");
const data = currentSheet.getRange(2, 1, currentSheet.getLastRow()-1, 24).getValues();
let errors = [];
data.forEach(row => {
try{
createPDF(row[2], row[3], row[4], row[8], row[9], row[16], row[17], row[18], row[19],"manny"+row[2], docFile, tempFolder, pdfFolder);
errors.push([""]);
}catch(err){
errors.push(["Failed"]);
}
});
currentSheet.getRange(2, 26,currentSheet.getLastRow()-1, 1).setValues(errors);
}
function createPDF(cliente, cotizacion, atencion, ciudad, fecha, manoDeObra, traslados, materiales, total, pdfName, docFile, tempFolder, pdfFolder) {
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{Cliente}", cliente);
body.replaceText("{Cotizacion}", cotizacion);
body.replaceText("{Atencion}", atencion);
body.replaceText("{Ciudad}", ciudad);
body.replaceText("{Fecha}", fecha);
body.replaceText("{ManoDeObra}", manoDeObra);
body.replaceText("{Traslados}", traslados);
body.replaceText("{Material}", materiales);
body.replaceText("{Total}", total);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs(MimeType.PDF);
pdfFolder.createFile(pdfContentBlob).setName(pdfName);
tempFolder.removeFile(tempFile);
}
But this creates a new PDF for every single row everytime a new one is added, creating tons of duplicates, how can i set it up so just the new one is created?
I guess this is the line that is responsible for that but i dont know how to change it
data.forEach(row => { try{ createPDF(row[2], row[3], row[4], row[8], row[9], row[16], row[17], row[18], row[19],"manny"+row[2], docFile, tempFolder, pdfFolder); errors.push([""]); }catch(err){ errors.push(["Failed"]); }
Luis Velazquez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.