简体   繁体   English

Google HTML服务:将表单中多项选择的值写入电子表格

[英]Google HTML Service: write values from multiple selects in form to spreadsheet

I've been banging my head against this for the past several days and have finally broken down and admitted defeat. 在过去的几天里,我一直在努力应对这一问题,最终失败并承认失败。 This is my first project utilizing Google HTML Service, and what I'm trying to do seems simple, but I can't get it to work. 这是我的第一个利用Google HTML Service的项目,我试图做的事情似乎很简单,但是我无法使其正常工作。 Here is what I want to happen... 这是我想发生的事情...

  1. User interacts with spreadsheet and needs to add additional rows with data 用户与电子表格进行交互,并需要添加其他数据行
  2. User selects an option from a custom menu item (got this working) 用户从自定义菜单项中选择一个选项(完成此工作)
  3. This selection launches an HTML service form (got this working) 此选择启动HTML服务表单(完成此工作)
  4. User selects the values from two drop down lists and clicks submit 用户从两个下拉列表中选择值,然后单击提交
  5. The selected options read read (working kind of...) and passed to the .js ( this is where I'm stuck ), which will create the rows and place the data. 选定的选项读为read(工作方式为...)并传递给.js( 这是我所卡住的位置 ),它将创建行并放置数据。

Below is my code: 下面是我的代码:

Function that launches the HTML Service 启动HTML服务的功能

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

index.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>

If you uncomment the lines that are commented out you will see that the values are read correctly. 如果取消注释已注释掉的行,您将看到正确读取了值。 Now, here is where it fails... I attempt to pass the form as an object to the function "getValuesFromFrom" using 现在,这是失败的地方...我尝试使用以下方式将表单作为对象传递给函数“ getValuesFromFrom”

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

Function getValuesFromFrom 函数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)

}

Nothing happens... the browser msgBox does not pop up. 没有任何反应...浏览器msgBox不会弹出。 What am I missing? 我想念什么? Also, how can I get the form to close automatically when the "Submit" button is pressed. 另外,当按下“提交”按钮时,如何使表格自动关闭。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

EDIT: After going back and forth with @Sandy Good I realized the "AppForm" variable in the getValuesFromForm function was undefined, which means that the form object was not being passed to the function from the html. 编辑:与@Sandy Good来回交流后,我意识到getValuesFromForm函数中的“ AppForm”变量是未定义的,这意味着表单对象没有从html传递给函数。 I tried another approach, and just attempted to pass a string variable to the function by altering the script portion of the html code like this 我尝试了另一种方法,只是尝试通过更改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);

This was successful, while this... 这是成功的,而这...

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);

was not! 不是!

So the question remains, what was I doing wrong previously? 所以问题仍然存在,我以前做错了什么?

EDIT: July 10th...Working code 编辑:7月10日...工作代码

Function that launches the HTML Service 启动HTML服务的功能

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

index.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 that receives the Form element 接收Form元素的函数

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));
}

Hope this helps someone out!!! 希望这可以帮助某人!!!

Change your form tag, and add this to the function: 改变你的表单标签,并添加this给该函数:

onsubmit="formSubmit(this)"

Then modify your function: 然后修改您的功能:

function formSubmit(argTheFormElement) {

Then put the variable argTheFormElement into the google.script.run.function(parameter); 然后将变量argTheFormElement放入google.script.run.function(parameter);

google.script.run.getValuesFromForm(argTheFormElement);

That will pass all input values to the server. 它将所有输入值传递到服务器。 The get the values out, you must use the name of the name tag. 要获取值,必须使用name标签的name

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

To make the dialog close, use: 要关闭对话框,请使用:

google.script.host.close;

google.script.host.close google.script.host.close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM