[英]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.