Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  119] [ 1]  / answers: 1 / hits: 19400  / 9 Years ago, mon, september 14, 2015, 12:00:00

Hello I am having difficulties working with arrays from a Google Spreadsheet, so I have the following code:



var statusCell = rows.getCell(i, statusColumn);
var ownerCell = rows.getCell(i, ownerColumn);

var owners = new Array (Mark,Chris,Will,Amy);

if(ownerCell.getValue() === One of the values in the array(owners))
{statusCell.setValue('Claimed')};


All my cells have been specified and so on, and I have no problem, getting cells in specific locations and or setting them. But my problem here is that I would ideally have a range of some sort in a different sheet with names, but for now they can be listed in an array, like shown in the script.



Then I want to make an if statement, and check wether the ownerCell one of the values that are listed in the array. So ex. if the ownerCell = Chris then the if statement would continue, because it is one of the names listed in the array and then set the statusCell to be Claimed. As seen in the script.



Maybe you can do something directly so you wouldn't have to compare, I don't know. Some advice would really help me out. Thanks alot :)


More From » arrays

 Answers
22

Use indexOf to test whether the value is found in the array. It returns -1 when the value isn't found, the index if it is.



if (owners.indexOf(ownerCell.getValue()) != -1) {
statusCell.setValue('Claimed');
}

[#65070] Friday, September 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sabrina

Total Points: 92
Total Questions: 92
Total Answers: 85

Location: Palestine
Member since Thu, Feb 2, 2023
1 Year ago
;