繁体   English   中英

Apps 脚本:如何将 Google Sheet 文件通过电子邮件发送到 HTML 服务输入中指定的电子邮件?

[英]Apps Script: How to Email Google Sheet file to Email Specified in HTML Service Input?

我正在努力在我的 Google Sheets 文件中构建一个 HTML 服务 UI,允许用户在 HTML 表单的弹出对话框中输入目标收件人的电子邮件地址。 我的代码目前无法有效工作,因为运行时未生成电子邮件。

JS代码:

    function openDialog() {
      var html = HtmlService.createHtmlOutputFromFile('Index');
      SpreadsheetApp.getUi()
      .showModalDialog(html, 'Email?');
    }

    function sendEmail() {
    var to = document.getElementbyID("emailInput");
      MailApp.sendEmail( {
        email: to,
        subject: "good morning",
     })
     }

HTML:

<html>
  <head>
    <base target="_top">
</head>
 <body>
      <form id="emailForm">
      Send to Email Address:<br> <input type="email" id="emailInput" 
      name="email" size="40"/><br>
      <button onClick="sendEmail()">Send</button>
      </form>
 </body>
</html>

您不能直接从客户端调用服务器端函数。 您需要使用google.script.run API在两者之间进行通信。 请尝试以下操作:

// Inside your Apps Script .gs code

// listen for an object sent from the HTML form
function sendEmail(formObj) {

  // Extract the email address submitted
  var to = formObj.email;

  // Send the email
  MailApp.sendEmail(to,"good morning", "The email body")
}
...
<body>
  <form id="emailForm">
      <p>Send to Email Address:</p>
      <br> 
      <input type="email" id="emailInput" name="email" size="40"/> <!-- "name" becomes the key in formObject -->
      <br>
      <input type="button" value="Send Email" onclick="google.script.run.sendEmail(this.parentNode)" /> <!-- google.script.run allows you to call server-side functions -->
      </form>
</body>
...

抱歉,我没有足够的声誉来发表评论,但是我对Brian的代码进行了2个小的更改,这些更改将有所帮助:

首先将this.parent更改为this.form

<input type="button" value="Send Email" onclick="google.script.run.sendEmail(this.form)

然后在MailApp.sendEmail函数的收件人上更新语法:

MailApp.sendEmail( {
   to: to,
   subject: "good morning",
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM