简体   繁体   English

Google表格/ Javascript:无法使用indexOf()在数组中搜索特定字符串

[英]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. 1)在包含关键字“名字”的数组中找到对象-不太准确-我可以从driveapp内的文件列表中进行操作,但似乎在SpreadSheetApp内不起作用。

2) then get the index of that object in my spreadsheet 2)然后在我的电子表格中获取该对象的索引
--Error-- "TypeError: Cannot find function hasNext in object First Name,Last Name,[...] (line 9, file "Test get keyword from array")" --Error--“ TypeError:在对象的名字,姓氏,[...]中找不到函数hasNext(第9行,文件“从数组中获取关键字”)

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. 3)将那个索引(实际上是列号)分配给一个变量,例如firstNameCol,这样我就可以访问它下面的列中的所有信息。
--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? 那么,会说流利的javascript / googlescript的人会这么友善地指出我可能在第二项上犯了一个错误吗?有什么方法可以完成第三项?

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. 从第3行开始,到第2列,获得一行值和23列值。

I guess that you are trying to inspect all the column titles, and find the column with the title, "First Name"? 我猜您正在尝试检查所有列标题,并找到标题为“ First Name”的列? And that's why you are only getting one row of data? 这就是为什么您仅获得一行数据的原因?

The getSheetValues method returns a two dimensional array. getSheetValues方法返回一个二维数组。 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);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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