Can someone help me with the code that I need to convert and save a google doc into a pdf file. THe script is this:
“
var plantillaId = '12233336566666';
var destinationFolderId = '4545656454566';
var temporalFolderID = '456456151566151';
function onFormSubmit() {
var hoja = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form1");
var lastRow = hoja.getLastRow();
var accionFormativaRespuesta = hoja.getRange(lastRow,3).getValue();
var accionesFormativas = accionFormativaRespuesta.split(', ');
accionesFormativas.forEach(function (accionFormativa) {
var destinationFolder = DriveApp.getFolderById(destinationFolderId);
var temporalFolder = DriveApp.getFolderById(temporalFolderID);
var archivoPlantilla = DriveApp.getFileById(plantillaId);
var copiaArchivoPlantilla = archivoPlantilla.makeCopy(temporalFolder);
var copiaID = copiaArchivoPlantilla.getId();
var doc = DocumentApp.openById(copiaID);
doc.setName(nombre + ' ' + primerApellido + ' ' + segundoApellido + ' ' + accionFormativa);
const document = doc.getBody();
var text = document.editAsText();
var startIndex = text.getText().indexOf('{{Acc}}');
if (startIndex !== -1) {
var endIndex = startIndex + '{{Acc}}'.length;
text.replaceText('{{Acc}}', accionFormativa);
text.setBold(startIndex, startIndex + accionFormativa.length - 1, true);
};
// * HERE IT WOULD BE CONTAINED THE CODE THAT HANDLES THE PDF CONVERSION * //
})
}
I have left out much of the not needed code that gets the data from the google sheet and saves into the google doc files for the sake of simplicity. I pretend to create a pdf file for each google doc created through each iteration of “accionesFormativas”. I have tried many different ways but javaScript makes it very difficult to make pdf conversions. Can someone help me out?
I have tried several ways. Lastly with this code:
var copiedDoc = DocumentApp.openById(copiaID);
var copiedBody = copiedDoc.getBody();
var newDoc = DocumentApp.create('Temp Document');
var newBody = newDoc.getBody();
copiedBody.getParagraphs().forEach(function (paragraph) {
newBody.appendParagraph(paragraph.copy());
});
var pdfFileName = nombre + ' ' + primerApellido + ' ' + segundoApellido + ' ' + accionFormativa;
var pdfBlob = DriveApp.createFile(newDoc.getAs(MimeType.PDF));
destinationFolder.createFile(pdfBlob).setName(pdfFileName + ".pdf");
DriveApp.getFileById(newDoc.getId()).setTrashed(true);
copiedDoc.setTrashed(true);
But nothing seems to work right