Replaced text in a Doc is missing in converted PDF

前端 未结 1 1874
生来不讨喜
生来不讨喜 2021-01-16 07:19

I am starting in Google Apps Script and trying to create a copy of a file (Google Doc), then replace text in the copied document and convert it to PDF.

The function

相关标签:
1条回答
  • 2021-01-16 07:54

    I do use the following function, which works for me:

    function personaliseAttachment(keyTemplate, member, fileName){
    
      var cloneId = DriveApp.getFileById(keyTemplate).makeCopy('cloneAttachment').getId();
      var clone = DocumentApp.openById(cloneId);
    
      var body = clone.getBody();
    
      for (var property in member) {
        if (member.hasOwnProperty(property)) {
          body.replaceText("{{"+property+"}}", member[property]);
        }
      }
      clone.saveAndClose();
    
      var clonePDF = DriveApp.createFile(clone.getAs('application/pdf'));
      clonePDF.setName(fileName);
    
      DriveApp.getFileById(cloneId).setTrashed(true);
    
      return clonePDF;
    }
    
    0 讨论(0)
提交回复
热议问题