I'm new to JavaScript and Google Apps Script so this is by all means a 'beginner' question I'm sure.
I'm using Google Apps Script in a spreadsheet to format single cells using an onEdit trigger. The format of the cell is based on user input from a custom UI app containing 6 buttons. The problem I'm having is the onClickHandler function doesn't see the same active cell that triggered the onEdit trigger initially; rather, the getActiveCell method called in the Handler is returning the cell just below the cell that was edited that became active as a result of the user pressing 'Enter' and the active range moving down one cell.
How do I get the handler to focus on the same cell that initiated the onEdit trigger? I'm not sure if I should somehow be passing the active cell from the event that triggered the onEdit code to the Handler via button parameters or somehow getting a value returned to the onEdit code to make the changes to the edited cell there. Whichever way is correct, I'm not sure how to go about it.
function onEdit(event)
{
var sheet = event.source.getActiveSheet();
var cell = sheet.getActiveCell();
var cellR = cell.getRow();
var cellC = cell.getColumn();
var cellValue = cell.getValue();
var active_spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
if ((cellR >= 14 && cellR <= 21 && cellC >= 1 && cellC <= 7) ||
(cellR >= 34 && cellR <= 41 && cellC >= 1 && cellC <= 7)) {
displayUserOptions();
}
}
function displayUserOptions()
{
var app = UiApp.createApplication().setTitle('Choose Equipment Class').setHeight(40).setWidth(350);
var handler = app.createServerHandler('onClickHandler');
var button1 = app.createButton('War').setTabIndex(1).setId('war').addClickHandler(handler);
var button2 = app.createButton('Nature').setTabIndex(2).setId('nature').addClickHandler(handler);
var button3 = app.createButton('Balance').setTabIndex(3).setId('balance').addClickHandler(handler);
var button4 = app.createButton('Fortune').setTabIndex(4).setId('fortune').addClickHandler(handler);
var button5 = app.createButton('Chaos').setTabIndex(5).setId('chaos').addClickHandler(handler);
var button6 = app.createButton('Generic').setTabIndex(6).setId('generic').addClickHandler(handler);
var mypanel = app.createHorizontalPanel();
mypanel.add(button1); mypanel.add(button2); mypanel.add(button3);
mypanel.add(button4); mypanel.add(button5); mypanel.add(button6);
app.add(mypanel);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
}
function onClickHandler(e) {
var app = UiApp.getActiveApplication();
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
if (e.parameter.source == 'war') {
Logger.log("War button pressed");
Browser.msgBox(cell.getValue());
cell.setValue("War!"); // *This string is committing to the wrong cell.
}
app.close();
return app;
}