如何在 html 电子邮件内容中显示通过 JS 获取的月份。?

代码.gs


function showMonth() {


  var recipient1 = 'user@gmail.com';

  var subject = "email subject";


  // send email using html page

  var emailTemplate = HtmlService.createTemplateFromFile('file');


  MailApp.sendEmail(recipient1, subject,'',{'htmlBody': emailTemplate.evaluate().getContent(), 'cc': '', 'bcc': ''});


}

文件.html


<!DOCTYPE html>

<html>

<head>

    <base target="_top">

  </head>

<body>


    <h2>My First Web Page</h2>


    <p>My First Paragraph.</p>


    <p id="displaymonth"> </p>


<script>

    var d = new Date();

    var mnth = getMonthDetails();

    function getMonthDetails() {

      switch (d.getMonth()) {

            case 9 : 

            return "October";

        break;

    }

  document.getElementById("displaymonth").innerHTML = "Current month is " + mnth;

</script>


</body>

</html> 

电子邮件输出:


My First Web Page

My First Paragraph.

我试图在 JS 脚本标签中获取日期和月份,并将它们发布到 HTML 内容中。稍后使用 Google Apps Script 的电子邮件模板服务获取相同的 HTML 内容,并向用户发送电子邮件以显示当前月份。


当我在jsfiddle.net等其他在线编辑器上运行代码时,它会在结果页面上显示月份的详细信息。但是,在 Google Apps 脚本上运行代码后,我无法在电子邮件中获得预期的结果。


让我知道一种修复我的代码以查看预期结果的方法。谢谢!


慕尼黑的夜晚无繁华
浏览 225回答 2
2回答

红颜莎娜

这样您的电子邮件和您的 web 应用程序获得相同的内容大多数内容来自一个用 html 编写的文本文件。然后在函数中,getContents()我在其中添加了带有月份的行。该函数为 doGet() 和 sendEmail() 提供内容。aq1.gsfunction getContents() {&nbsp; var folder=DriveApp.getFolderById('folderId');&nbsp; var file=folder.getFiles().next();//assume only one&nbsp; var contents=file.getBlob().getDataAsString()+Utilities.formatString('<p>This month is %s</p>',Utilities.formatDate(new Date(), Session.getScriptTimeZone(),"MMMM" ));//get content from file and append the date to it as a string&nbsp; return contents;}function sendEmail() {&nbsp; var recipient1 = 'recipient email';&nbsp; var subject = "Combining Email and Website Contents";&nbsp; var html=getContents();&nbsp; MailApp.sendEmail(recipient1, subject,'',{htmlBody:html});}function doGet() {&nbsp; return HtmlService.createHtmlOutputFromFile('aq5');}function launchDiaog() {&nbsp; var userInterface=HtmlService.createHtmlOutputFromFile('aq5');&nbsp; SpreadsheetApp.getUi().showModelessDialog(userInterface, "My Dialog");}aq5.html:<!DOCTYPE html><html>&nbsp; <head>&nbsp; <base target="_top">&nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>&nbsp; <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">&nbsp; <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>&nbsp; <script>&nbsp; &nbsp; $(function() {&nbsp; &nbsp; &nbsp; console.log('Ready State Function');&nbsp; &nbsp; &nbsp; google.script.run&nbsp; &nbsp; &nbsp; .withSuccessHandler(function(hl){&nbsp; &nbsp; &nbsp; &nbsp; console.log(hl);&nbsp; &nbsp; &nbsp; &nbsp; $('#mydiv').html(hl);&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; .getContents();&nbsp; &nbsp; });&nbsp; &nbsp; console.log("My Code");&nbsp; </script>&nbsp; </head>&nbsp;&nbsp;&nbsp; <h1>Testing</h1>&nbsp; <div id="mydiv"></div></html>这是 ascii 文本文件的内容:<h2>My First Web Page</h2><p>My First Paragraph.</p><p>My second paragrah</p>但它可以是任何高达 50MB 的东西

四季花海

尝试获取应用程序脚本上的日期,然后将其作为变量传递到您的 HTML 文件中。我认为您的客户端 js 代码不会由 Apps 脚本执行。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript