1

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.

0

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();
  }
}
  • Perfect! How can I make it so that Row 6 works the same as Row 2 here? This may sound dumb, but I copied the code twice and changed the number, but then only 6, not 2, functions right. – Josh Kelver Jul 22 '17 at 20:14
  • You are so great, Alex, thanks. I'd love if you could look at another of my questions. It was flagged as a duplicate, but I don't know how to tailor the duplicate's answer to my own needs. The question is here: webapps.stackexchange.com/questions/107910/… – Josh Kelver Jul 22 '17 at 20:28
  • I see now that I'd like to use the script above in just columns B-F. How can I amend the script for that purpose? – Josh Kelver Jul 24 '17 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.