My custom addition function in Google Sheets concatenates numbers as strings, returning ‘12345’ instead of 15. How can I ensure number retention? Is enforced strict typing available?
computeAggregate(dataRange.getValues());
function computeAggregate(arr) {
let sumValue = 0;
for (let k = 0; k < arr.length; k++) {
sumValue += Number(arr[k]);
}
return sumValue;
}
hey, try using parseFloat instead of Number() and ensure you’re checking device cell values. i solved it by cleaning up my inputs and that worked fine.
I had a similar issue when developing a script for processing sizeable amounts of data in a financial report. In my case, type conversion errors led to unexpected concatenation of values. I ended up creating an intermediary function that checked if the cell value was actually numeric before attempting any operations on it. This extra validation step was instrumental in catching potential errors early and ensuring accurate computations. While using Number() generally works, the additional type check proved crucial in maintaining numeric integrity throughout the process.