Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-formulacustom-function

Trying to create custom function on google sheets to check if something is paid and add it to a total


So i'm trying to create a custom function on google sheets that will see a value, check to see if the next cell says 'yes' (which means that value has already been paid), and if 'yes' is there it should take that value and add it to a total and return that total, but I keep getting '0' as a return value. Here's my code:

This is what I tried:

function totalStatus(input) {
  let total= 0;

  for (let i = 0; i < input.length; i++) {

    for(let y = 0; y < input[i].length; y++) {
      if (input[i][y] !== '' && input[i][y + 1] === 'yes') {
        input[i][y] += total;
      }
    }
  }
  return total;
}

and kept getting '0' as a return.


Solution

  • You are not modifying total, you should have total += input[i][y]. What you are doing is adding total to input[i][y], which in turn does nothing, because total = 0.