I am trying to copy a sheet of a Google Doc document into another document, but I am unable to do so. Can someone help me?
I'm trying with the object Document
and Body
but I can not get it, the maximum I've achieved is copying the content but without format:
bodyReport.setText(copyBody.getText());
I'm thinking about doing it manually and it's going through the elements and formatting it, but of course, it's quite laborious.
Function source:
function createReport() {
try {
if (TEMPLATE_ID === '') {
SpreadsheetApp.getUi().alert("No existe definida en TEMPLATE_ID la plantilla del informe");
return
}
var docCopy = DocumentApp.openById(TEMPLATE_ID),
docReport = DocumentApp.create(REPORT_FILE_NAME),
bodyCopy = docCopy.getBody(),
bodyReport = docReport.getBody()
var activeSheet = SpreadsheetApp.getActiveSheet(),
numberOfColumns = activeSheet.getLastColumn(),
numberOfRows = activeSheet.getLastRow()
var activeRow = activeSheet.getRange(1, 1, numberOfRows, numberOfColumns).getValues();
for (var row = 1; row < numberOfRows; row++) {
bodyCopy.replaceText('%' + fieldTematica + '%', activeRow[row][columnTematica]);
bodyCopy.replaceText('%' + fieldImpacto + '%', activeRow[row][columnImpacto]);
bodyCopy.replaceText('%' + fieldUrgencia + '%', activeRow[row][columnUrgencia]);
bodyCopy.replaceText('%' + fieldAreaCoordinadora + '%', activeRow[row][columnAreaCoordinadora]);
bodyCopy.replaceText('%' + fieldActuacionCurso + '%', activeRow[row][columnActuacionCurso]);
bodyCopy.appendPageBreak();
}
//bodyReport.appendParagraph(bodyCopy.getParagraphs());
bodyReport.setText(copyBody.getText());
docCopy.saveAndClose();
docReport.saveAndClose();
//var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'))
//copyFile.setTrashed(true)
SpreadsheetApp.getUi().alert('Se ha creado un nuevo PDF en su unidad Google Drive')
} catch(e) {
Logger.log("ERROR in function createPdf \r\nMessage: " + e.message + "\r\nFile gs: " + e.fileName + "\r\nLine: " + e.lineNumber)
Logger.log("\r\nUser: " + Session.getActiveUser().getEmail() + ", Date: " + Utilities.formatDate(new Date(), "GMT+1", "yyyyMMdd'_'HH:mm:ss"));
}
}