1

有用的答案,但不完全是我想要的

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

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

4

2 回答 2

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();
}
于 2012-10-28T21:16:53.480 回答
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);
}
于 2012-10-29T10:09:22.597 回答