【问题标题】:How to send email and encrypt by google scripts after creating a PDF创建 PDF 后如何通过谷歌脚本发送电子邮件和加密
【发布时间】:2021-01-18 08:23:45
【问题描述】:

客户填写表格后,我使用此代码创建 pdf 文件:

function After_Submit(e){
  
  const info = e.namedValues;
  Create_PDF(info);  
  
  console.log(info);
  

}

function Create_PDF(info) {
  
  const PDF_folder = DriveApp.getFolderById("folder id");
  const TEMP_Folder = DriveApp.getFolderById("folder id");
  const PDF_Template = DriveApp.getFileById("pdf temp id");
  
  const newTempFile = PDF_Template.makeCopy(TEMP_Folder);
  const  OpenDoc = DocumentApp.openById(newTempFile.getId());
  const body = OpenDoc.getBody();
  
  console.log(body);
  
   body.replaceText("{Code}", info['Code'][0]);
   body.replaceText("{Date}", info['Date'][0]);
   body.replaceText("{Name}", info['Name'][0]);
   body.replaceText("{Birthdate}", info['Birthdate'][0])
   body.replaceText("{Address}", info['Address'][0]);
   OpenDoc.saveAndClose();
  
  const BLOBPDF = newTempFile.getAs(MimeType.PDF);
  PDF_folder.createFile(BLOBPDF).setName(info['Name'][0] + " " + info['Code'][0]);
  console.log("PDF created");
  TEMP_Folder.removeFile(newTempFile);

}

显然我也使用了触发器来自动化它。

现在我需要创建一个函数,将该 pdf 发送到客户在表单上提供的电子邮件,并用他的生日“例如”作为密码来保护它!

谁能帮忙?

非常感谢

【问题讨论】:

    标签: pdf google-apps-script google-sheets google-forms


    【解决方案1】:

    很遗憾,Apps Script 不具备在 PDF 文件中设置密码的任何特定特性或功能,要实现这一点,您需要使用第三方应用程序、API 或库。

    要发送带有附件的电子邮件,您可以使用方法MailApp.sendEmail,如下所示(以下代码包含自我解释的cmets):

    function Create_PDF(info) {
      
      const PDF_folder = DriveApp.getFolderById("folder id");
      const TEMP_Folder = DriveApp.getFolderById("folder id");
      const PDF_Template = DriveApp.getFileById("pdf temp id");
      
      const newTempFile = PDF_Template.makeCopy(TEMP_Folder);
      const  OpenDoc = DocumentApp.openById(newTempFile.getId());
      const body = OpenDoc.getBody();
      
      console.log(body);
      
       body.replaceText("{Code}", info['Code'][0]);
       body.replaceText("{Date}", info['Date'][0]);
       body.replaceText("{Name}", info['Name'][0]);
       body.replaceText("{Birthdate}", info['Birthdate'][0])
       body.replaceText("{Address}", info['Address'][0]);
       OpenDoc.saveAndClose();
      
      const BLOBPDF = newTempFile.getAs(MimeType.PDF);
      
    ///////////////////////// MODIFICATION ////////////////////////////////
    
      // Get the file you want to send as an attachment
      var file = PDF_folder.createFile(BLOBPDF).setName(info['Name'][0] + " " + info['Code'][0]);
      
        // Send email using the function sendEmail of MailApp (recipient, subject, message, options)
        MailApp.sendEmail('mike@example.com', 'Attachment example', 'PDF attached', {
        name: 'Attached file',
        attachments: [file.getAs(MimeType.PDF)]
        });
    
    /////////////////////////////////////////////////////////////////////////
        
      console.log("PDF created");
      TEMP_Folder.removeFile(newTempFile);
      
    }

    【讨论】:

    • 嗨!很高兴你解决了。如果我的回答解决了您的问题,请考虑接受和/或投票。 :D
    • 很遗憾,它给了我这样一条消息“感谢您的反馈!声望低于 15 人的投票会被记录下来,但不要更改公开显示的帖子得分。”
    • 是的,那是为了upvoting,但是,你仍然可以接受答案,它是左侧的勾号,在upvote downvote按钮下方。感谢您检查它:D
    猜你喜欢
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    相关资源
    最近更新 更多