无法访问事件发布值

时间:2012-07-12 09:24:16

标签: post google-apps-script

我正在编写一个小的谷歌应用程序脚本,允许在开发人员列表中选择几个名称。但是在我的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;
}

在例子中,列表是固定的,但在我的真实脚本中,我依靠电子表格创建列表。

由于

最高

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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”的状态,当它被选中时。您可以根据需要进行修改。