mardi 17 février 2015

Google Sheets / Javascript : Unable to search for a specific string in an array using indexOf()


I am attempting to:


1) find the object in the array that contains the keyword "First Name"

--check-- I can do that.



2) then get the index of that object in my spreadsheet

--Error-- "TypeError: Cannot find function hasNext in object First Name,Last Name,[...] (line 9, file "Test get keyword from array")"



3) assign that index (which is in fact the column number) to a variable ,say, firstNameCol so that I can access all of the information in the column below it.

--Error-- I seem to be incorrectly assigning the value into the variable and do not have the slightest notion how to go about it.


I am learning this as I go. So would someone more fluent in javascript / googlescript be so kind to point out where I might be making a mistake with the second item, and what methods are available to accomplish the third item?


Thanks



function testSheet()
{
var keyword = "First Name"
var ssId="1kRdKGDQJXxCW2q1HPclWcpsOpI1BJPvAlMjvLSX6JvY";
var ss = SpreadsheetApp.openById(ssId);
var sheet = ss.getSheetByName("Sheet1");
var values = sheet.getSheetValues(3, 2, 1, 23);

for(;values.hasNext(); values.next())
{
if(values.indexOf(keyword) > -1 )

/* I would like to assign the index to a variable should the if
condition return true : var keywordIndex values.getIndex();*/

logger.log(values);
}
}




Aucun commentaire:

Enregistrer un commentaire