Google Sheets – Add Cell Contents to Another Cell and Delete Original

google sheetsgoogle-appsgoogle-apps-script

I have a spreadsheet to keep track of points in a class. I want to have a cell for each class that I can type a number of points into, and that number will be added into a corresponding cell in the row below it. Then I want the first cell to empty itself so that I can add more points in and repeat the process whenever I need to. The second cell (the one in the row below) should keep a running total of all the numbers entered into the original cell.

Best Answer

This requires a script, for example:

function onEdit(e) {
  if (e.range.rowStart == 2) {
    var below = e.range.offset(1, 0);
    below.setValue(Number(e.value) + Number(below.getValue()));
    e.range.clear();
  }
}

Here the watched range is the 2nd row (the condition e.range.rowStart == 2). If a number is entered there, then it's added to the content of the cell below, and the cell itself is cleared.

Using Number to avoid string concatenation 2 + 2 = 22 instead of addition.


Same for rows 2 and 6: the notation || means OR:

function onEdit(e) {
  if (e.range.rowStart == 2 || e.range.rowStart == 6) {
    var below = e.range.offset(1, 0);
    below.setValue(Number(e.value) + Number(below.getValue()));
    e.range.clear();
  }
}