2012-07-11 39 views
0

當您單擊按鈕時,如何添加或減去數字。我想簡單地說按鈕單擊更改電子表格中的行

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

回答

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