【问题标题】:Google Docs script editor trying to autogenerate an email from an autogenerated documentGoogle Docs 脚本编辑器尝试从自动生成的文档中自动生成电子邮件
【发布时间】:2019-10-11 08:31:28
【问题描述】:

我发现的关于这个主题的所有内容都包括让一个谷歌表格填充一个谷歌文档,然后发送一封电子邮件。我个人已经将我在网上找到的一些代码粘贴到了文档的脚本编辑器中。现在,在打开文档时,系统会提示用户回答问题框。答案会自动填充创建的新文档。然后该脚本要求发送一封电子邮件。

到目前为止,我的提示正确,新文档已创建,提示框中的信息已正确填写。我还让它向 1 个地址发送电子邮件,这就是它应该做的。电子邮件的主题行也是正确的。问题是我希望在脚本中创建的新 Google 文档成为电子邮件的正文,而我只是不知道如何实现这一点。

这是我在脚本编辑器中的代码。我在最后一行尝试了很多方法来使新文档的正文填充电子邮件正文,但没有运气。谁能告诉我如何使这项工作的编程语言?

    function myFunction() {
      // Display a dialog box for each field you need information for.

      var ui = DocumentApp.getUi();
      //var response = ui.prompt('Enter Name', 'Enter sales person's name', ui.ButtonSet.OK);
      var shiftResponse = ui.prompt('Enter shift, i.e. 7-3 or 3-11');
      var peersResponse = ui.prompt('Enter peers on shift');
      var participantsResponse = ui.prompt('Enter names of face to face encounters');
      var phonelogResponse = ui.prompt('Enter names of people we called on phone log');
      var filescreatedResponse = ui.prompt('Enter names of people we created files for');
      var notesResponse = ui.prompt('Enter any notes about shift');
      var cleanResponse = ui.prompt('Was Crisis Center Cleaned? Enter yes or no');
      var authorResponse = ui.prompt('Enter your name');
      var date = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy");

      //Make a copy of the template file
      var documentId = DriveApp.getFileById('1lXTJPvwlJrXkRJ807daFsFbfaiC_wl7EAQ4giixLeEc').makeCopy().getId();

      //Rename the copied file
      DriveApp.getFileById(documentId).setName(date + " " + shiftResponse.getResponseText() + ' Shift Report');  

      //Get the document body as a variable
      var body = DocumentApp.openById(documentId).getBody();

      //Insert the entries into the document
      body.replaceText('##date##', date);
      body.replaceText('##shift##', shiftResponse.getResponseText());
      body.replaceText('##peers##', peersResponse.getResponseText());
      body.replaceText('##participants##', participantsResponse.getResponseText()); 
      body.replaceText('##phonelog##', phonelogResponse.getResponseText());
      body.replaceText('##filescreated##', filescreatedResponse.getResponseText());  
      body.replaceText('##notes##', notesResponse.getResponseText());
      body.replaceText('##clean##', cleanResponse.getResponseText());
      body.replaceText('##author##', authorResponse.getResponseText());  

      MailApp.sendEmail("jason.chrystal@voicesofhopececilmd.org", "Shift Report", body);
    }

【问题讨论】:

    标签: google-apps-script editor documentation google-docs


    【解决方案1】:

    如文档中所述:

    replaceText 方法需要一个正则表达式模式值作为第一个参数:

    https://developers.google.com/apps-script/reference/document/body#replacetextsearchpattern,-replacement

    MailApp.sendMail 参数的最后一个参数也需要一个字符串,并且您正在给它一个正文类对象。

    将您的第一个参数更改为正确匹配的正则表达式模式,您的代码就可以正常工作了。

    body.replaceText(/^##date##$/, date);
    body.replaceText(/^##shift##$/, shiftResponse.getResponseText());
    body.replaceText(/^##peers##$/, peersResponse.getResponseText());
    etc...
    

    -- 正则表达式未测试。

    如果您对正则表达式不满意,可以使用 body.setText() 作为替代方法,如下所示:

    var oldBodyText = body.getText();
    body = body.setText(oldBodyText.replace('##date##', date));
    
    oldBodyText = body.getText();
    body = body.setText(oldBodyText.replace('##shift##', shiftResponse.getResponseText()));
    
    oldBodyText = body.getText();
    body = body.setText(oldBodyText.replace('##peers##', peersResponse.getResponseText()));
    
    etc...
    
    // And then the last lines:
    var newBody = body.getText();
    MailApp.sendEmail("jason.chrystal@voicesofhopececilmd.org", "Shift Report", newBody);
    
    

    【讨论】:

    • 这不会弄乱已经工作的部分吗?并且会使电子邮件正文内容成为整个新文档?我不想只发送提示答案。
    • 所以我刚刚尝试过,生成的电子邮件的正文内容为“DocumentBody Section”
    • 我怀疑你没有注意到 replaceText 的第一个参数需要是一个正则表达式。
    • 所以,我试过了,电子邮件的正文现在显示为“DocumentBodySection”
    • 你尝试了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    相关资源
    最近更新 更多