随机子串错误?< TypeError:在对象中找不到函数子字符串.... [英] Random substring error? "TypeError: Cannot find function substring in object..."
本文介绍了随机子串错误?< TypeError:在对象中找不到函数子字符串....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误是:
TypeError:在对象中找不到函数子字符串
代码循环遍历10个值,并检查第一个字符是否为数字.
The code loops through 10 values, checking for the first character being a number.
function TypeErrorMystery() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
//loop from cell 1-10
for (var i = 0; i < 10; i++) {
var range = s.getRange(i + 1, 1)
var substring1 = range.getValue().substring(0, 1);
//if first character in cell is #
if (substring1 === "#" ) {
//write over it with "success"
range.setValue("success");
}
};
}
确切的错误是:
TypeError:在对象3中找不到函数子字符串(第9行,文件代码"),
第9行是:
var substring1 = range.getValue().substring(0, 1);
推荐答案
单元格中的值为数字3. substring()
方法在应用于数字时会引发错误.您应该检查返回的值的类型.
The value in the cell is the number 3. The substring()
method throws an error when being applied to a number. You should check the type of value that is returned.
if (typeof thisCellValue === 'number') {continue;};//Skip over numbers
完整代码:
function TypeErrorMystery() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var range,
substring1,
thisCellValue;
//loop from cell 1-10
for (var i = 0; i < 10; i++) {
range = s.getRange(i + 1, 1)
thisCellValue = range.getValue();
Logger.log('typeof thisCellValue: ' + typeof thisCellValue);
if (typeof thisCellValue === 'number') {continue;};
substring1 = thisCellValue.substring(0, 1);
//if first character in cell is #
if (substring1 === "#" ) {
//write over it with "success"
range.setValue("success");
}
};
};
这篇关于随机子串错误?< TypeError:在对象中找不到函数子字符串....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文