如何在GAS中找到传递给e.parameter的表单元素的数量?

时间:2014-08-16 01:33:43

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

在Google App Scripts(GAS)中,我希望能够将TextBox和TextArea元素添加到FlexTable中并将其删除(这些元素被用作表单),而不用担心它有多少。我已根据计数器命名文本元素,以简化此过程。

那么,有没有办法在提交表单后将输入数量(TextBox + TextArea)传递给e.parameter?

这里是FlexTable的相关代码:

function doGet() {
  var app = UiApp.createApplication();
  var flex = app.createFlexTable().setId('myFlex');

  var counter = 0;
  var row_counter = 0;

  ...

  var firstnameLabel = app.createLabel('Your FIRST Name');
  var firstnameTextBox = app.createTextBox().setWidth(sm_width).setName('input' + counter).setText(data[counter]);
  flex.setWidget(row_counter, 1, firstnameLabel);
  flex.setWidget(row_counter, 2, firstnameTextBox);
  row_counter++;
  counter++;

  var lastnameLabel = app.createLabel('Your LAST Name');
  var lastnameTextBox = app.createTextBox().setWidth(sm_width).setName('input' + counter).setText(data[counter]);
  flex.setWidget(row_counter, 1, lastnameLabel);
  flex.setWidget(row_counter, 2, lastnameTextBox);
  row_counter++;
  counter++;

  ...

  var submitButton = app.createButton('Submit Proposal');
  flex.setWidget(row_counter, 2, submitButton);

  var handler = app.createServerClickHandler('saveProposal');
  handler.addCallbackElement(flex);
  submitButton.addClickHandler(handler);

  var scroll = app.createScrollPanel().setSize('100%', '100%');
  scroll.add(flex);
  app.add(scroll);
  return app;
}

这里是ClickHandler的代码(注意我目前在FlexTable中有39个元素):

function saveProposal(e){
  var app = UiApp.getActiveApplication();
  var userData = [];
  var counter = 39;

   for(var i = 0; i < counter; i++) {
    var input_name = 'input' + i;
    userData[i] = e.parameter[input_name];
  }

那么,有没有办法获得元素的数量(在这种情况下为39)而无需手动计算它们并将此值赋给变量?

我对这些东西很陌生,我感谢你的帮助。

干杯!

3 个答案:

答案 0 :(得分:0)

您可以搜索基于arguments数组的对象。

function foo(x) {
     console.log(arguments.length); // This will print 7.
}
foo(1,2,3,4,5,6,7) // Sending 7 parameters to function.

答案 1 :(得分:0)

最简单的方法是在doGet()函数中添加hidden widget,其中包含counter值,如下所示:

var hidden = app.createHidden('counterValue',counter);// don't forget to add this widget as a callBackElement to your handler variable (handler.addCallBackElement(hidden)) 

然后在处理函数中使用

var counter = Number(e.parameter.counterValue);// because the returned value is actually a string, as almost any other widget...

如果你想在调试时看到这个值,你可以用textBox暂时替换它......

答案 2 :(得分:0)

您可以使用while循环。

var i = 0;
var userData = [];

while (e.parameter['input' + i] != undefined) {
  userData[i] = e.parameter['input' + i];
  i++;
};

OR:

var i = 0;
var userData = [];
var input_name = 'input0';

while (e.parameter[input_name] != undefined) {
  userData[i] = e.parameter[input_name];
  i++;
  input_name = 'input' + i;
};