Reputation: 460
I'm struggling to handle a multiple selection listbox in a handler function. My listbox entries contain commas, ideally i want to get an array of the selected items in the handler but it seems to be a single string.
Does anyone know how I could do this?
thanks
function doGet() {
...
var completedReportsList = app.createListBox(true)
.setId('list')
.addItem("Bloggs, Joe")
.addItem("Doe, Jane");
var generatePDFButton = app.createButton("Generate PDFs")
.setId("generatePDFButton");
var generatePDFButtonHandler = app.createServerClickHandler('generatePDF')
.addCallbackElement(completedReportsList)
generatePDFButton.addClickHandler(generatePDFButtonHandler);
...
}
function generatePDF(e) {
var app = UiApp.getActiveApplication();
Logger.log(e.parameter.list.length); // undefined
Logger.log(e.parameter.list); // <- how to parse multiple selected items?
return app;
}
Upvotes: 0
Views: 165
Reputation: 46802
ListBox accepts 2 parameters, a "shown" value and a "returned" value. The second parameter is optional and if not present the returned value becomes the shown value.
Could it be possible for you to use this second field with a value that does not contain commas, for example using a | separator so that you could easily rebuild your original value with a replace in each array element?
Code would be as follows :
(you also forgot to give a name to your list, that's why you got length=undefined for e.parameter.list)
function doGet() {
var app = UiApp.createApplication().setTitle('test');
var completedReportsList = app.createListBox(true)
.setId('list').setName('list')
.addItem("Bloggs, Joe","Bloggs| Joe")
.addItem("Doe, Jane","Doe| Jane");// this could of course be obtained in the script with a .replace string method.
var generatePDFButton = app.createButton("Generate PDFs").setId("generatePDFButton");
var generatePDFButtonHandler = app.createServerClickHandler('generatePDF').addCallbackElement(completedReportsList);
generatePDFButton.addClickHandler(generatePDFButtonHandler);
app.add(completedReportsList).add(generatePDFButton)
return app
}
function generatePDF(e) {
var app = UiApp.getActiveApplication();
Logger.log(e.parameter.list.split(',').length); // undefined
var listValues = e.parameter.list.split(','); // an array of values
for(var n in listValues){
Logger.log(listValues[n].replace('|',','));
}
return app;
}
And here is a modified version that creates the alternate fields automatically in the doGet function :
function doGet() {
var app = UiApp.createApplication().setTitle('test');
var completedReportsList = app.createListBox(true).setId('list').setName('list');
var items = ["Bloggs, Joe","Doe, Jane"];
for(var n in items){
item = items[n];
modItem = items[n].replace(',','|');
completedReportsList.addItem(item,modItem);
}
var generatePDFButton = app.createButton("Generate PDFs").setId("generatePDFButton");
var generatePDFButtonHandler = app.createServerClickHandler('generatePDF').addCallbackElement(completedReportsList);
generatePDFButton.addClickHandler(generatePDFButtonHandler);
app.add(completedReportsList).add(generatePDFButton)
return app
}
Upvotes: 1