2012-10-28 28 views
1

Helpful answer, but not exactly what I'm looking for谷歌電子表格:按鈕添加B到A,然後清空b

以上回答給我找的腳本的一部分。我想添加另一個步驟(或兩個),但不知道如何。

Here's what I'm trying to accomplish: 

我有一列數字,並希望從另一列添加數字。我無法弄清楚的部分是如何在數字添加到第一列後清除單元格。

Like this: 

A <---B 
1 <---2 
2 <---5 
3 <---24 

點擊一個按鈕,我想從B中的數字被添加到A然後B被清除。 A列將變爲3,7,27,並且B列中的單元格將變爲空白。

回答

0
function addRange() { 
    //replace 'Sheet1' with your actual sheet name 
    var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1'); 
    var tRange = sheet.getRange('A1:A3'); 
    var tValues = tRange.getValues(); 
    var sRange = sheet.getRange('B1:B3'); 
    var sValues = sRange.getValues(); 
    for (var i = 0; i < tValues.length; i++) { 
    tValues[i][0] += sValues[i][0]; 
    } 
    tRange.setValues(tValues); 
    sRange.clearContent(); 
} 
+0

非常感謝AdmaL的代碼示例!這工作完美。 – Jenny

0

您還可以添加一個按鈕到您的電子表格菜單執行上面使用此代碼

function onOpen() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var menubutton = [ {name: "Add Range", functionName: "addRange"}]; //"addRange" is the name of the function submitted by AdamL Above //This would also need to be included as a function in your script as the function to call when you click the "Custom" menu button 
    ss.addMenu("Custom", menubutton); 
} 
相關問題