按钮单击时更改电子表格中的行

时间:2012-07-11 19:04:33

标签: google-apps-script

单击按钮时如何添加或减去数字。我想简单地说

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

如何使用Google Apps脚本按钮执行此类操作?对我来说,事情似乎不那么简单,因为我从未使用过点击处理程序。它可能是一些我想念的简单事物。谢谢你的帮助。

1 个答案:

答案 0 :(得分: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);