'Google Apps Script function called from sheet - sometimes blank results (returned empty value)
My simple (as I initially thought) Google Apps script function is not working as expected as it yields different results based on the input given = it is to return generated numbers (separated by comma) from START to END - being called in [column J] as per attached picture.
When the custom function is called with two params (start number and end number) from the sheet (cell content) it yields different results if:
Scenario #1: params are static in the formula - for instance: =FillNums(5,12)
Scenario #2: params are dynamic in the formula (for instance pointing to cell A2 and B2: =FillNums(A2,B2))
- In first scenario all the calculations are OK, as expected
- In the SECOND scenario however it yields sometimes BLANK result.
ONLY in SCENARO #2, depending on the input parameters (in cells), I'm getting different output:
5,21 => blank output
1,21 => 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
2,21 => 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
3,21 => blank output
4,19 => blank output
5,19 => blank output
6,19 => blank output
7,19 => blank output
8,19 => blank output
9,19 => blank output
5,9 => 5,6,7,8,9
10,21 => 10,11,12,13,14,15,16,17,18,19,20,21
Screenshot of the mentioned issue / Google Sheet
Debugging is not working, useless, spent hours trying to resolve it and found out that it is not possible when function being called from the sheet, the same goes for Logger = unusable.
Function:
function FillNums(start,end) {
var result = ""
var a = start
var b = end
for (var i = a; i <= b; i++) {
if (i == b) {
result = result + i
} else {
result = result + i + ","
}
}
return result
}
Any solutions are mostly welcome to resolve this issue. That function should yield ALL THE TIME row of numbers from start to end.
I do script from time to time, but I'm just a beginner here and cant understand why such simple thing is yielding empty result sometimes :(
Link to a example sheet containing the script function: Google Sheet
Solution 1:[1]
When I saw your script and your sample Spreadsheet, I thought that in your situation, the values of start,end of FillNums(start,end) might be the string values. I thought that this might be the reason of your issue. When this is reflected in your script, it becomes as follows.
Modified script 1:
function FillNums(start, konec) {
var result = ""
var a = Number(start); // Modified
var b = Number(konec); // Modified
var i = 0
for (i = a; i <= b; i++) {
if (i == b) {
result = result + i
} else {
result = result + i + ","
}
}
return result
}
Modified script 2:
As other sample, when an array is used, it becomes as follows.
function FillNums(start, konec) {
var a = Number(start);
var b = Number(konec);
var result = [];
for (var i = a; i <= b; i++) {
result.push(i);
}
return result.join(",");
}
Modified script 3:
As other sample, when an array is used, it becomes as follows. In this case, you can use this script like =FillNums2(A2:B10).
function FillNums2(values) {
return values.map(([start, konec]) => {
if (!start && !konec) return ["no data"];
var a = Number(start);
var b = Number(konec);
var temp = [];
for (var i = a; i <= b; i++) {
temp.push(i);
}
return [temp.join(",")];
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tanaike |
