Been trying this the whole day but to no avail. I need to be able to return each value in an array to google sheet repeatedly, (n-1) number of times, with n as the array.length. This array.length is expected to be inconsistent, depending on user input via google form.
User is expected to enter some code names in a text field in google form, separating each code name with symbol '||'. Each code name must be unique but still there would be a validation check to remove duplicates before other process. Then return output in rows in the google sheet repeatedly.
Eg. Input of 5 code names in array = ['Marvell','Xtortion','Ambiguous','Exhale','Xtortion']
My code as below:-
//by Bjorn | https://stackoverflow.com/questions/881085/count-the-number-of-occurrences-of-a-character-in-a-string-in-javascript
var cname=sheet.getRange(2,1).getValue();
var code=cname.split("||");
var codename=[...new Set(code)]; //get unique values first
var count=cname.split("||").length - 1; // to get freq to repeat output of same value.
// ['Marvell','Xtortion','Ambiguous','Exhale']
// array.length is 4, output each value being returned to google sheet start from row 3 in col A should be 3 times (n-1)
var lrow=sheet.getLastRow();
for(var i=1;i<=count;i++){
for(var j=0;j<=count;j++){
sheet.getRange(lrow+1,1,i,1).setValue([j]);
var lrow=sheet.getLastRow();
}
}
}
Expected Output :
Marvell
Marvell
Marvell
Xtortion
Xtortion
Xtortion
Ambiguous
Ambiguous
Ambiguous
Exhale
Exhale
Exhale
But my code produced as below :
Marvell
Xtortion
Ambiguous
Exhale
Marvell
Marvell
Xtortion
Xtortion
Ambiguous
Ambiguous
Exhale
Exhale
Marvell
Marvell
Marvell
Xtortion
Xtortion
Xtortion
Ambiguous
Ambiguous
Ambiguous
Exhale
Exhale
Exhale
Please help to correct my code. Thank you in advance.