Google Spreadsheets:按钮将B添加到A然后清除B.

时间:2012-10-28 01:15:08

标签: button google-apps-script google-sheets clear

Helpful answer, but not exactly what I'm looking for

上面的答案给出了我正在寻找的部分脚本。我想添加另一步(或两步),但不知道如何。

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列中的单元格将变为空白。

2 个答案:

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

答案 1 :(得分:0)

您还可以在电子表格菜单中添加一个按钮,使用此代码执行上面的AdmaL代码示例

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