Random substring error? "TypeError: Cannot find function substring in object..."
Random substring error? "TypeError: Cannot find function substring in object..."
错误是:
TypeError: 在对象中找不到函数子字符串
代码循环遍历 10 个值,检查第一个字符是否为数字。
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 行,文件 "Code"),
第 9 行是:
var substring1 = range.getValue().substring(0, 1);
单元格中的值为数字 3。substring()
方法在应用于数字时会引发错误。您应该检查返回值的类型。
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: 在对象中找不到函数子字符串
代码循环遍历 10 个值,检查第一个字符是否为数字。
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 行,文件 "Code"),
第 9 行是:
var substring1 = range.getValue().substring(0, 1);
单元格中的值为数字 3。substring()
方法在应用于数字时会引发错误。您应该检查返回值的类型。
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");
}
};
};