4

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 :)

4
  • 1
    Array.prototype.indexOf() Commented Sep 14, 2015 at 17:04
  • And it'd be better to write in var owners = ["Mark","Chris","Will","Amy"]; Commented Sep 14, 2015 at 17:07
  • But as far as I can see here: w3schools.com/jsref/jsref_indexof.asp the indexOf only returns the index? So for "chris" that would be "1". I need to check if the value is there? Or am I misunderstanding something. Commented Sep 14, 2015 at 17:20
  • @WilliamLarsenBang indexOf returns -1 if the value isn't found. So just check whether the result is not -1. Commented Sep 14, 2015 at 17:29

1 Answer 1

10

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');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.