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

问题描述:

当您单击按钮时,如何添加或减去数字。我想简单地说按钮单击更改电子表格中的行

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

我该如何用google apps脚本按钮做类似的事情?事情似乎没那么直截了当,因为我从来没有使用过点击处理程序。它可能是我错过的一些简单的事情。谢谢您的帮助。

+0

好吧,它不像你想要的那样简单,但它不是很难做...看到下面的答案 – 2012-07-11 19:47:32

+0

感谢您的答复。还有一件事,要显示来自活动单元格的信息,我会简单地这样做:label.setText(sh.getRange().getActiveRange()。getRow(),sh.getActiveRange()。getColumn())。getValue()) ; – user1518769 2012-07-11 20:43:16

+0

看看我刚​​才回答的下一篇文章,我添加了一个Label并解释了如何修改其值。另外,如果您认为我回答了您的第一个问题,请将其标记为已回答。 thx – 2012-07-11 20:47:54

请看看这个演示代码:

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