Max
Max

Reputation: 1830

Can't access to event posted values

i'm writing a little google apps script which allow to select several names among a list of developpers. But in my doPost(e) method, i can't access to the array of posted values (it's undefinded), event if i check all the checkboxes...

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

In the exemple, the list is fixed but in my real script, i create the list thanks to the Spreadsheet.

Thanks

Max

Upvotes: 0

Views: 194

Answers (2)

Gunasekaran R
Gunasekaran R

Reputation: 66

You should use e.parameter.yourCheckBoxName

for example:

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

This would show status of check box "Peter", when it is checked. You can modify based on your need.

Upvotes: 0

Henrique G. Abreu
Henrique G. Abreu

Reputation: 17752

The correct way to see the checkboxes values is using e.parameter[name], like you said yourself on a comment. Here is some code example:

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

Upvotes: 1

Related Questions