I have a function that has two passed in parameters--one is a single number from a single cell, and the other is a range, but all numbers. The function calculates the percent change between each of the passed in numbers and finds the average. It's not all one range because of where they reside on the Google Sheet. Anyway, I have tested it with internal variables and it works perfectly. I have tested the passed in parameters with TYPEOF for specific indexes in the range and with the single number and it always returns "number". I have tested the TYPEOF on the returned variable, and it is a number. But, when I run the function from a cell I get a #NUM! error that says "Result is not a number." What am I missing?
function avgROC(prc, prcs){
//This function calculates the percent differences between values in a range of cells and returns the average
var avg = [];
var tot = 0;
var m = 0;
var n = 0;
//Get all the percentages between all the values in the range
for(var i = 0; i<prcs.length; i++){
if(prcs[i]==0){continue;}
if(prcs[i+1]/prcs[i]-1==-1){continue;}
avg[i] = prcs[i+1]/prcs[i]-1;
m = m + 1;
}
//Find the final percentage between the last number in the range and the isolated number
avg[avg.length]= prc/prcs[m]-1;
//Get the average of the percentages
for(var j=0; j<avg.length; j++){
tot = avg[j]+tot;
n = n + 1;
}
var r = tot/n;
return r;
}
In the cell I am typing "=avgROC(A1, A5:M5)", where A1 is something like 5.64 and the range is all similar values. I've also tried passing the range as a string and using the
var prices = SpreadsheetApp.getActiveSpreadsheet().getRange(prcs);
approach, but that gave the same results. Any thoughts?