Copy document sheet Google Docs in another document

2

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"));
  } 
}
    
asked by Richi 20.01.2017 в 09:11
source

1 answer

1

With the following function it has served me for what I wanted, I have obtained it from the following stackoverflow thread link .

function copyDoc() {
  try {
    var sourceDoc = DocumentApp.openById(TEMPLATE_ID).getBody();
    var targetDoc =  DocumentApp.create(REPORT_FILE_NAME);
    var totalElements = sourceDoc.getNumChildren();
    for (var j = 0; j < totalElements; ++j) {
      var body = targetDoc.getBody();
      var element = sourceDoc.getChild(j).copy();
      var type = element.getType();
      if (type == DocumentApp.ElementType.PARAGRAPH) {
        body.appendParagraph(element);
      } else if (type == DocumentApp.ElementType.TABLE) {
        body.appendTable(element);
      } else if (type == DocumentApp.ElementType.LIST_ITEM) {
        body.appendListItem(element);
      } else if (type == DocumentApp.ElementType.INLINE_IMAGE) {
        body.appendImage(element);
      }
      //    ...add other conditions (headers, footers...
    }
    //targetDoc.saveAndClose();
    return targetDoc;
  } catch(e) {
    Logger.log("ERROR in function copyDoc \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"));
  } 
}
    
answered by 24.01.2017 / 08:33
source