Javascript Google 电子表格脚本:toString() 不返回字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42518152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:13:13  来源:igfitidea点击:

Google spreadsheet script: toString() not returning a string?

javascriptgoogle-sheetsgoogle-spreadsheet-api

提问by MikeyS

I'm writing a very simple google spreadsheets script and need to compare strings. For some reason, when I call toString() on the content of a cell, I get a type error: "TypeError: Cannot find function includes in object Semester Long Clinics. (line 6)", where in this case "Semester Long Clinics" is the actual content of the cell. Here's the code:

我正在编写一个非常简单的谷歌电子表格脚本,需要比较字符串。出于某种原因,当我对单元格的内容调用 toString() 时,我收到一个类型错误:“TypeError:找不到对象中包含的函数 Semester Long Clinics。(第 6 行)”,在本例中为“Semester Long Clinics” " 是单元格的实际内容。这是代码:

function getStudents(input, clinicName, columnNumber) { 
  var toPrint = []
  var i = 0; 
  for(i; i < 43; i++){
  var toCheck = input[i][columnNumber - 1].toString()
   if(toCheck.includes(clinicName)){
      toPrint.push(input[i][0].toString() + ", " + input[i][1].toString() +     ", " + input[i][2].toString())
     }
  }
  return toPrint
}

The only explanation I can think of is that the input array contains instances of some sort of object that resists the standard toString() method, but I'm not sure what the advantages of that would be. Any help is much appreciated!

我能想到的唯一解释是输入数组包含某种抵抗标准 toString() 方法的对象的实例,但我不确定这样做的优点是什么。任何帮助深表感谢!

回答by Hyman Brown

I don't think the error is due to the toString Function rather with this function

我不认为错误是由于 toString 函数而不是这个函数引起的

toCheck.includes(clinicName)

Since your error is on line 6 and it says cannot find the function includes in the object/string "Semester Long Clinics", which is the content of that array/cell.

由于您的错误在第 6 行,并且它说找不到包含在对象/字符串“Semester Long Clinics”中的函数,这是该数组/单元格的内容。

You can try this instead

你可以试试这个

if( toCheck.indexOf(clinicName) != -1)

Just might be that the function "includes" is not supported by Apps script.

只是可能是 Apps 脚本不支持“包含”功能。