使用GAS的HTML表单时,邮件功能无法在Google电子表格上运行

时间:2014-10-13 11:44:18

标签: javascript html google-apps-script google-sheets

我使用 Google apps脚本创建了一个HTML表单文件,并将其与电子表格结合使用。但在GAS中,功能SendConfirmationMail对我不起作用。在这里,我已经包含了两个脚本文件以供专家理解并反馈我。

code.gs

//Global variable
template = HtmlService.createHtmlOutputFromFile("index");
username = template.username;
sheet = SpreadsheetApp.openById("Spreadsheet ID").getActiveSheet();

// Get data from HTML form
function doGet(){
  return template;
}

// Post data from HTML form to SpreadSheet
function AddToSheet(username, useremail, comment){
  return sheet.appendRow([username, useremail, comment]);
}

// send email
 function SendConfirmationMail() {
   var message, usermail;
   var sendername = "Dizaraj Dey";
   var subject = "Confirmation Mail";

usermail = template.useremail;

message = "<html><body>”
 +"Hi <b>" +username+ "!!!</b> <br><br> Your request has been successfully submitted. <br>"
+</body></html>"; 

   GmailApp.sendEmail(usermail, subject, message, 
                      {usermail, name: sendername, htmlBody: message});
 }

的index.html

<script>
  function onFailure(error) {
    alert(error.message);
  }
</script>

<script>
  function onSuccess() {
  alert("Submission successful. Please check your email.");
}
</script>

<script>
 function formSubmit() {
            google.script.run.doSomething(document.forms[0]);
        }
</script>

<form id="theform">
<input type="text" name="name" id="username" placeholder="Input your name"/>< br>
<input type="email" name="email" id="useremail" placeholder="Your Email Address" /><br>
<input type="text" name="comment" id=" comment " placeholder="Your comments here" /><b><br>
<input type="submit" id="submitbtn" onclick="WriteInput()" value="Give Up" />
<input type="reset" id="btnReset" value="Reset" />
</form>


<!-- AddToSheet function -->
<script>

function WriteInput() {
    var name, email, comment;
    name = document.getElementById('username').value;
    email = document.getElementById('useremail').value;
    comment = document.getElementById('comment').value;

        google.script.run.withFailureHandler(onFailure)
        .withSuccessHandler(onSuccess)
        .AddToSheet(username, useremail, comment);

}

</script>

注意: 功能 AddToSheet 工作正常。

感谢
Dizaraj Dey

1 个答案:

答案 0 :(得分:0)

更改以下功能,然后重试

function AddToSheet(username, useremail, comment){
  SendConfirmationMail(username, useremail);
  return sheet.appendRow([username, useremail, comment]);
}

function SendConfirmationMail(username, email) {
   var message;
   var sendername = "Dizaraj Dey";
   var subject = "Confirmation Mail";

message = "<html><body>”
 +"Hi <b>" +username+ "!!!</b> <br><br> Your request has been successfully submitted. <br>"
+</body></html>"; 

   GmailApp.sendEmail(email, subject, message, {name: sendername, htmlBody: message});
 }

手动运行邮件功能时未定义的原因是因为它没有从表单获取数据。但是你没有在任何地方调用邮件功能。