类型错误:无法读取空 Google 脚本的属性'getRow'



我得到一个TypeError运行这个。我不明白为什么……

function iceHorsepowerInjector() {  
let z = 0;
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('blah blah blah');
let sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('blah blah blah')
let sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('blah blah blah')
for (let i = 2; i <= 1312; i++) { 
let handle = sheet.getRange("A"+[i]).getValue(); 
if (handle == null){
continue;
} else {                                                     
var index = sheet1.createTextFinder(handle).matchEntireCell(true).findNext().getRow(); // <---- this is where I am getting the error
}
if (handle == null){
continue;
}
index = parseInt(index);
let horsepower = sheet1.getRange("EE"+[index]).getValue();
if (horsepower == "NULL"){
continue;
}                                            
let id = sheet.getRange("CQ"+[i]).getValue();
if (id == "#N/A"){
continue;
}                                                        
var commands = ("UPDATE fossil_fuel_vehicle_trims SET horsepower" + "=" + "'"+horsepower+ "' WHERE id =" + handle +";")
sheet2.getRange(1+z,1).setValue(commands);   //set to last row used in sheet3
z = z + 1
}   
}

这是错误…


TypeError: Cannot read property 'getRow' of null
iceHorsepowerInjector   @ Code.gs:11

如何解决这个问题?我觉得我就在附近,但我似乎不明白为什么会发生这种情况。

由于sheet1.createTextFinder(handle).matchEntireCell(true).findNext()返回null而发生错误。

这个错误的一个简单修复方法是替换

var index = sheet1.createTextFinder(handle).matchEntireCell(true).findNext().getRow(); // <---- this is where I am getting the error
}
if (handle == null){
continue;
}

var found = sheet1.createTextFinder(handle).matchEntireCell(true).findNext();
}
if (found == null){
continue;
}
var index = found.getRow();

最新更新