0

我正在编写一个小谷歌应用程序脚本,它允许在开发人员列表中选择多个名称。但是在我的 doPost(e) 方法中,我无法访问发布值的数组(未定义),如果我选中所有复选框...

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // add the new menu
  var menuEntries = [];
  menuEntries.push({name: "Edit page", functionName: "start"})
  ss.addMenu("My tools", menuEntries);
}


function start() {

  var app  = UiApp.createApplication();
  app.setTitle("Edit page");
  var formPanel = app.createFormPanel();
  var mainPanel = app.createVerticalPanel();
  var gridPanel = app.createGrid(1, 2);

  // developpers
  var developperPanel = app.createVerticalPanel()
  var developpers = [];
  developpers.push( "John", "Peter", "Max", "Johnny" );
  for (var i = 1; i < developpers.length; ++i) {
    var checkbox = app.createCheckBox(developpers[i]).setName("dev"+i);
    developperPanel.add(checkbox);
  }
  gridPanel.setWidget(0,0,app.createLabel("Developpers : "));
  gridPanel.setWidget(0,1,developperPanel);

  // submit button
  mainPanel.add(gridPanel);
  mainPanel.add(app.createSubmitButton().setText("OK"));
  formPanel.add(mainPanel);
  app.add(formPanel);

  var ss = SpreadsheetApp.getActive();
  ss.show(app);
}

function doPost(e) {
  var app = UiApp.getActiveApplication();
  app.add(app.createLabel(e.values[0])); // Here is the error.       
  return app;
}

在示例中,列表是固定的,但在我的真实脚本中,我通过电子表格创建了列表。

谢谢

最大限度

4

2 回答 2

1

查看复选框值的正确方法是使用e.parameter[name],就像您在评论中所说的那样。这是一些代码示例:

function start() {
  //...
  var developpers = ["John", "Peter", "Max", "Johnny"];
  //you were skiping the first developer
  for (var i = 0; i < developpers.length; ++i) { //array starts on zero, not one
    var checkbox = app.createCheckBox(developpers[i]).setName("dev"+i);
    developperPanel.add(checkbox);
  }
  //...
}

function doPost(e) {
  var app = UiApp.getActiveApplication();
  var developpers = ["John", "Peter", "Max", "Johnny"]; //repeat it here or make it global, if it's static like this
  var checked = [];
  for( var i in developpers )
    if( e.parameter['dev'+i] == 'true' )
      checked.push(developpers[i]);
  app.add(app.createLabel(checked));
  return app;
}
于 2012-07-12T13:11:39.550 回答
0

你应该使用e.parameter.yourCheckBoxName

例如:

function doPost(e) {
  var app = UiApp.getActiveApplication();
  app.add(app.createLabel(e.parameter.dev1); // Access checkbox by its name
  return app;
}

这将显示复选框“Peter”的状态,当它被选中时。您可以根据需要进行修改。

于 2012-07-12T13:23:25.930 回答