猿问

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

我有两个 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 form

function 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">

有人可以提供建议吗,我真的很困惑,希望这只是小事。


谢谢。


炎炎设计
浏览 81回答 1
1回答

月关宝盒

我使用隐藏元素来区分它们。ah1.html:<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <base target="_top">&nbsp; <script>&nbsp; &nbsp; function processForm(form) {&nbsp; &nbsp; &nbsp; console.log(form);&nbsp; &nbsp; &nbsp; google.script.run&nbsp; &nbsp; &nbsp; .withSuccessHandler(function(obj){&nbsp; &nbsp; &nbsp; &nbsp; console.log(obj);&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("eml" + obj.number).value="";&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("sub" + obj.number).value="";&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("msg" + obj.number).value="";&nbsp; &nbsp; &nbsp; &nbsp; //document.getElementById("ret" + obj.number).innerHTML=obj.msg;&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; .processForm(form);&nbsp; &nbsp; }&nbsp; &nbsp; console.log('My Code');&nbsp; </script>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp;<form name="form1">&nbsp; &nbsp; &nbsp;<input type="text" name="email" id="eml1" placeholder="Enter Email" />&nbsp; &nbsp; &nbsp;<input type="text" name='subject' id="sub1" placeholder="Enter Subject" />&nbsp; &nbsp; &nbsp;<textarea rows="4" name="message" cols="30" id="msg1" placeholder="Enter Message"></textarea>&nbsp; &nbsp; &nbsp;<input type="hidden" name="number" value="1" />&nbsp; &nbsp; &nbsp;<input type="button" value="Submit" onClick="processForm(this.parentNode);" />&nbsp; &nbsp;</form>&nbsp; &nbsp;<div id="ret1"></div>&nbsp; &nbsp;<form name="form2">&nbsp; &nbsp; &nbsp;<input type="text" name="email" id="eml2" placeholder="Enter Email" />&nbsp; &nbsp; &nbsp;<input type="text" name="subject" id="sub2" placeholder="Enter Subject"/>&nbsp; &nbsp; &nbsp;<textarea rows="4" name="message" cols="30" id="msg2" placeholder="Enter Message"></textarea>&nbsp; &nbsp; &nbsp;<input type="hidden" name="number" value="2" />&nbsp; &nbsp; &nbsp;<input type="button" value="Submit" onClick="processForm(this.parentNode);" />&nbsp; &nbsp;</form>&nbsp; &nbsp; <div id="ret2"></div>&nbsp; </body>&nbsp; <input type="button" value="close" onClick="google.script.host.close();" /></html>code.gs:function launchSideBar() {&nbsp; SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile("ah1"));}function processForm(obj) {&nbsp; console.log(obj);&nbsp; const ss=SpreadsheetApp.getActive();&nbsp; const sh=ss.getSheetByName('email');&nbsp; sh.appendRow([obj.email,obj.subject,obj.message]);&nbsp; return {number:obj.number,msg:"Email Sent"}}
随时随地看视频慕课网APP

相关分类

Html5
我要回答