【问题标题】:Google HTML Service: write values from multiple selects in form to spreadsheetGoogle HTML 服务:将表单中多项选择的值写入电子表格
【发布时间】:2015-07-02 20:14:55
【问题描述】:

在过去的几天里,我一直在为此苦苦挣扎,最终崩溃并承认失败。这是我使用 Google HTML 服务的第一个项目,我想做的事情看起来很简单,但我无法让它工作。这就是我想要发生的事情......

  1. 用户与电子表格交互,需要添加额外的数据行
  2. 用户从自定义菜单项中选择一个选项(让它工作)
  3. 此选择会启动一个 HTML 服务表单(使其正常工作)
  4. 用户从两个下拉列表中选择值并点击提交
  5. 选择的选项读取读取(工作种类...)并传递给 .js(这是我卡住的地方),它将创建行并放置数据。

下面是我的代码:

启动 HTML 服务的函数

    function AddAdditionalApplicant() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      html = HtmlService.createHtmlOutputFromFile('index');
  ss.show(html);
}

index.html

<form name="AddApplicant" onsubmit="formSubmit()">

<p><b>What Type?</b></p>
    <select name="NumOfApp" id="NumOfApp">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="Cosigner">Cosigner</option>
    </select>

<p><b>How Many?</b></p>
    <select name="TypeOfApp" id="TypeOfApp">
      <option value="Roommate">Roommate</option>
      <option value="Cosigner">Cosigner</option>
    </select>

<p></p>
<div>
  <!--<input type="submit" class="button redButton" value="Submit" onclick="formSubmit()">-->
  <input type="submit" class="button redButton" value="Submit">
</div>

</form>

<script type="text/javascript">
function formSubmit() {

//var a=document.getElementById('NumOfApp').selectedIndex;
//var b=document.getElementById('NumOfApp').options;
//alert("Index: " + b[a].index + " is " + b[a].text);

//var x=document.getElementById('TypeOfApp').selectedIndex;
//var y=document.getElementById('TypeOfApp').options;
//alert("Index: " + y[x].index + " is " + y[x].text);

    google.script.run.getValuesFromForm(document.forms[0]);
}
</script>

如果您取消注释被注释掉的行,您将看到值被正确读取。现在,这里是失败的地方......我尝试将表单作为对象传递给函数“getValuesFromFrom”使用

google.script.run.getValuesFromForm(document.forms[0]);

函数 getValuesFromFrom

function getValuesFromForm(AppForm){

Browser.msgbox("success")  /attempt to test and see if the execution gets this far...no go
//var a=AppForm['NumOfApp'].selectedIndex;
//var b=AppForm['NumOfApp'].options;
//Logger.log(b[a])
//
//var x=AppForm.TypeOfApp.selectedIndex;
var type = AppForm.TypeOfApp.options[AppForm.TypeOfApp.selectedIndex].value;
Logger.log(type)

}

什么都没有发生...浏览器 msgBox 没有弹出。我错过了什么?另外,当按下“提交”按钮时,如何让表单自动关闭。非常感谢任何帮助。

编辑: 在与@Sandy Good 反复讨论之后,我意识到 getValuesFromForm 函数中的“AppForm”变量是未定义的,这意味着表单对象没有从 html 传递给函数。我尝试了另一种方法,只是尝试通过像这样更改 html 代码的脚本部分来将字符串变量传递给函数

var x=document.getElementById('TypeOfApp').selectedIndex;
var y=document.getElementById('TypeOfApp').options;
//alert("Index: " + y[x].index + " is " + y[x].text);
var type=y[x].value
//    google.script.run.getValuesFromForm(y[x], b[a]);
  google.script.run.withFailureHandler(google.script.host.close)
    .getValuesFromForm(type);

这是成功的,而这...

var x=document.getElementById('TypeOfApp').selectedIndex;
var y=document.getElementById('TypeOfApp').options;
//alert("Index: " + y[x].index + " is " + y[x].text);
var type=y[x]
//    google.script.run.getValuesFromForm(y[x], b[a]);
  google.script.run.withFailureHandler(google.script.host.close)
    .getValuesFromForm(type);

不是!

所以问题仍然存在,我之前做错了什么?

编辑:7 月 10 日...工作代码

启动 HTML 服务的函数

    function AddAdditionalApplicant() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      html = HtmlService.createHtmlOutputFromFile('index');
  ss.show(html);
}

index.html

<form name="AddApplicant" onsubmit="formSubmit(this)">

<p><b>How Many?</b></p>
    <select name="NumOfApp" id="NumOfApp">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
    </select>

<p><b>What Type?</b></p>
    <select name="TypeOfApp" id="TypeOfApp">
      <option value="Roommate">Roommate</option>
      <option value="Cosigner">Cosigner</option>
    </select>

<p></p>
<div>
  <!--<input type="submit" class="button redButton" value="Submit" onclick="formSubmit()">-->
  <input type="submit" class="button redButton" value="Submit">
</div>

</form>

<script type="text/javascript">
function formSubmit(argTheFormElement) {

  google.script.run
    .withFailureHandler(myFailureFunction)
    .withSuccessHandler(google.script.host.close)
    .getValuesFromForm(argTheFormElement);

}
function myFailureFunction(argError) {
  alert("There was an error: " + argError.message);
  google.script.host.close();
  }

</script>

接收表单元素的函数

function getValuesFromForm(AppFormElement){
var ss = SpreadsheetApp.getActive();
var s = ss.getActiveSheet();
var sname = s.getName();

var num = AppFormElement.NumOfApp
var type =  AppFormElement.TypeOfApp

var activeRow = s.getActiveCell().getRow();
var addCell = s.getRange(activeRow,2);

  if (type == "Roommate") {
  for(var i = 0; i < num; ++i){
    AddRoommate(activeRow,addCell,sname,s);
    }
  }else if (type == "Cosigner"){
  for(var i = 0; i < num; ++i){
    AddCosigner(activeRow,addCell,sname,s);
    }
  }
  s.setActiveRange(addCell.offset(1,1));
}

希望这对某人有所帮助!!!

【问题讨论】:

  • 不要使用Browser.msgbox()来测试函数是否运行,而是尝试使用Logger.log("it ran!");运行代码,然后在VIEW菜单中,查看LOGS。在您的浏览器中,按 f12 键,应显示浏览器控制台。如果 HTML 中有错误,则应该有错误消息。
  • 我按照您的建议更改了测试,当我运行该函数时,我确实看到了日志条目,但只有第一个 Logger.log(type) 没有显示任何内容。当我打开浏览器控制台时,当我隐藏提交时弹出此错误..Uncaught TypeError: Cannot read property "(class)@16f28274" from undefined. 我猜这是由以下代码行引起的:var type = AppForm.TypeOfApp.options[AppForm.TypeOfApp.selectedIndex].value;
  • 您应该将withFailureHandler 链接到google.script.run 代码。我怀疑未捕获的 TypeError 来自服务器端代码。我猜这是客户端代码、HTML 或 SCRIPT 标签错误。但是注释掉那一行,会告诉你这是否是错误的根源。
  • function formSubmit() {函数放入浏览器窗口对象:window.formSubmit = function() {。然后在顶部添加一个debugger; 行:window.formSubmit = function() { debugger; 在 chrome 中,代码应该在浏览器中停止在该行,然后您可以一次执行每一行并观察变量值。
  • 我注释掉了这些行,并将脚本的最后一行更改为以下内容是您所说的吗? ` google.script.run.withFailureHandler(google.script.host.close) .getValuesFromForm(document.forms[0]);` 这样做消除了错误,我仍然看到“成功”的日志条目,所以我取消了这些行的注释我注释掉,再次提交表单……表单消失了,但是执行记录有这行……Execution failed: TypeError: Cannot read property "(class)@1633227c" from undefined. (line 16, file "Add Application") [0.004 seconds total runtime]

标签: javascript html forms google-apps-script


【解决方案1】:

更改表单标签,并将this 添加到函数中:

onsubmit="formSubmit(this)"

然后修改你的函数:

function formSubmit(argTheFormElement) {

然后将变量argTheFormElement放入google.script.run.function(parameter);

google.script.run.getValuesFromForm(argTheFormElement);

这会将所有输入值传递给服务器。要取出值,您必须使用 name 标记的名称。

var type = AppForm.NumOfApp; //Get NumOfApp value

要关闭对话框,请使用:

google.script.host.close;

google.script.host.close

【讨论】:

  • 感谢您指出单斜线...我在原始脚本中没有,并将其添加到堆栈帖子中。至于关闭对话框,我是否在此行之后添加您建议的代码:google.script.run.getValuesFromForm(document.forms[0]);
  • 是的。将google.script.host.close 放在最后。
  • 感谢您的支持!!!除了表单保持打开状态之外,您建议的所有内容都有效......我在执行记录中没有看到任何错误,控制台中也没有错误,但表单仍然保持打开状态。这就是我对脚本部分的最后几行的内容google.script.run.withFailureHandler(myFailureFunction) .getValuesFromForm(argTheFormElement); google.script.host.close } function myFailureFunction(argError) {console.log("There was an error: " + argError.message);}; 这是正确的吗?如果是这样,为什么表单不会关闭?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 2023-01-30
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
相关资源
最近更新 更多