0

当你点击一个按钮时,你如何添加或减去一个数字。我想简单地说

if (button.click){
    num++;
}

我怎样才能用谷歌应用脚​​本按钮做这样的事情?对我来说,事情似乎不那么简单,因为我从未使用过点击处理程序。它可能是我缺少的一些简单的东西。谢谢您的帮助。

4

1 回答 1

1

请看一下这个演示代码:

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

编辑 :(在您的第二个问题之后)要获得一个显示单元格内容的标签,您可以在处理程序函数中使用此代码:

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

在 Ui 定义中,您必须像这样创建标签:

   var label = app.createLabel("test Label with text that will be modified on click").setId('label');
   panel.add(label);
于 2012-07-11T19:36:37.243 回答