为了账号安全,请及时绑定邮箱和手机立即绑定

在 Google App 脚本中指定 HTML/Javascript 表单

在 Google App 脚本中指定 HTML/Javascript 表单

炎炎设计 2024-01-22 15:54:12
我有两个 HTML 表单。两者都执行类似的任务,您填写它们,然后它们发送电子邮件。填写表单后单击提交按钮后,我调用 Google 应用程序脚本来构建电子邮件并发送。我的问题是,尽管我的设置彼此隔离,但两个提交按钮现在都发送相同的电子邮件。我想问题是我如何告诉GAS指定哪种形式?这是我第一次做这些,现在我很困惑。以下是 Form A html 的结尾:<input class="w3-button w3-block w3-section w3-blue w3-ripple w3-padding" type="button" value="Submit project request" onclick="google.script.run.sendEmail(this.parentNode)" />我的 Form A 应用程序脚本是:// listen for an object sent from the HTML formfunction projectRequest(formObj) {Form A 在其 html 中的名称为:<form name="projectrequest" action="/action_page.php" class="w3-container w3-card-4 w3-light-grey w3-text-blue w3-margin">所以我认为我的问题是因为表单 B 类似并且我正在调用 formObj?表单 B 是我原来的工具,填写后工作正常,但按表单 A 上的相同提交按钮会尝试运行表单 B 的应用程序脚本电子邮件代码。表格 B html 结尾:<input class="w3-button w3-block w3-section w3-blue w3-ripple w3-padding" type="button" value="Send Email" onclick="google.script.run.sendEmail(this.parentNode)" />Form B 应用程序脚本为:function sendEmail(formObj) {html 中的 Form B 名称为:<form name="furtherhelp" action="/action_page.php" class="w3-container w3-card-4 w3-light-grey w3-text-blue w3-margin">有人可以提供建议吗,我真的很困惑,希望这只是小事。谢谢。
查看完整描述

1 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

我使用隐藏元素来区分它们。


ah1.html:


<!DOCTYPE html>

<html>

  <head>

    <base target="_top">

  <script>

    function processForm(form) {

      console.log(form);

      google.script.run

      .withSuccessHandler(function(obj){

        console.log(obj);

        document.getElementById("eml" + obj.number).value="";

        document.getElementById("sub" + obj.number).value="";

        document.getElementById("msg" + obj.number).value="";

        //document.getElementById("ret" + obj.number).innerHTML=obj.msg;

      })

      .processForm(form);

    }

    console.log('My Code');

  </script>

  </head>

  <body>

   <form name="form1">

     <input type="text" name="email" id="eml1" placeholder="Enter Email" />

     <input type="text" name='subject' id="sub1" placeholder="Enter Subject" />

     <textarea rows="4" name="message" cols="30" id="msg1" placeholder="Enter Message"></textarea>

     <input type="hidden" name="number" value="1" />

     <input type="button" value="Submit" onClick="processForm(this.parentNode);" />

   </form>

   <div id="ret1"></div>

   <form name="form2">

     <input type="text" name="email" id="eml2" placeholder="Enter Email" />

     <input type="text" name="subject" id="sub2" placeholder="Enter Subject"/>

     <textarea rows="4" name="message" cols="30" id="msg2" placeholder="Enter Message"></textarea>

     <input type="hidden" name="number" value="2" />

     <input type="button" value="Submit" onClick="processForm(this.parentNode);" />

   </form>

    <div id="ret2"></div>

  </body>

  <input type="button" value="close" onClick="google.script.host.close();" />

</html>

code.gs:


function launchSideBar() {

  SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile("ah1"));

}


function processForm(obj) {

  console.log(obj);

  const ss=SpreadsheetApp.getActive();

  const sh=ss.getSheetByName('email');

  sh.appendRow([obj.email,obj.subject,obj.message]);

  return {number:obj.number,msg:"Email Sent"}

}


查看完整回答
反对 回复 2024-01-22
  • 1 回答
  • 0 关注
  • 33 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信