简体   繁体   中英

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" --not quite-- I can do it from a list of files inside of driveapp, but it doesn't seem to work inside of the SpreadSheetApp.

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);
    }
   }    

You are getting one row of values:

var values = sheet.getSheetValues(3, 2, 1, 23);

That starts in row 3, and column 2, gets one row of values and 23 columns of values.

I guess that you are trying to inspect all the column titles, and find the column with the title, "First Name"? And that's why you are only getting one row of data?

The getSheetValues method returns a two dimensional array. You need to get the second dimension out of the main array.

Here are the changes I've made to the code:

function testSheet() {
   var keyword = "First Name"
   var ssId="Your SS ID";
   var ss = SpreadsheetApp.openById(ssId);
   var sheet = ss.getSheetByName("Form Responses 1");
   var values = sheet.getSheetValues(3, 2, 1, 23);

   var howManyColumns = values[0].length;
   var indexOfString = 0;
   var thisValue = "";

    for(var i = 0;i < howManyColumns; i++) {
      Logger.log('i: ' + i);

      thisValue = values[0][i];
      Logger.log('thisValue: ' + thisValue);

      indexOfString = thisValue.indexOf(keyword);
      Logger.log('indexOfString: ' + indexOfString);

      if(indexOfString > -1 ) {

     /* I would like to assign the index to a variable should the if   
     condition return true : var keywordIndex values.getIndex();*/
      var thisIndexNumber = i;
      Logger.log('this index: ' + thisIndexNumber);
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM