Reputation: 3
How do you add or subtract from a number when you click on a button. I want to simply say
if (button.click){
num++;
}
How can I do something like that with the google apps script buttons? Things seem a bit less straightforward to me as I've never used a click handler. Its probably some simple thing that I'm missing. Thanks for the help.
Upvotes: 0
Views: 1197
Reputation: 46792
please have a look at this demo code :
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
//
function move() {
var app = UiApp.createApplication().setTitle("move test")
.setHeight(100).setWidth(400).setStyleAttribute("background-color","beige");
var panel = app.createVerticalPanel();
var next = app.createButton('next').setWidth('180');
var chkmode = app.createCheckBox("moving mode (checked = up/dwn, unchecked=L/R)").setValue(false).setName('chkmode');
panel.add(next).add(chkmode);
var handler = app.createServerHandler('click').addCallbackElement(panel);
next.addClickHandler(handler);
app.add(panel);
ss.show(app);
}
//
function click(e) {
var app = UiApp.getActiveApplication();
var activeline = sh.getActiveRange().getRow();// get the row number of the selected cell/range
var activecol = sh.getActiveRange().getColumn();// get the row number of the selected cell/range
var chkmode=e.parameter.chkmode;// this returns a string, that's why true is written "true" just below...
if(chkmode=="true"){
++activeline
}else{
++activecol} // the ++ can be before or after the var name, your choice ;-)
var sel=sh.getRange(activeline,activecol);
sh.setActiveSelection(sel);// make the next row or column active
return app;
}
EDIT : (following your secondary question) to get a Label showing the cell's content you can use this code in the handler function :
function click(e) {
var app = UiApp.getActiveApplication();
var activeline = sh.getActiveRange().getRow();// get the row number of the selected cell/range
var activecol = sh.getActiveRange().getColumn();// get the row number of the selected cell/range
var cellvaluestring = sh.getActiveRange().getValue().toString();
var label = app.getElementById('label');
label.setText(cellvaluestring);
var chkmode=e.parameter.chkmode;
if(chkmode=="true"){
activeline++
}else{
activecol++}
var sel=sh.getRange(activeline,activecol);
sh.setActiveSelection(sel);// make the next row active
return app;
}
and in the Ui definition you have to create the label like this :
var label = app.createLabel("test Label with text that will be modified on click").setId('label');
panel.add(label);
Upvotes: 1