TypeError:无法调用 null 的方法“getRange”。(第 9 行,文件“Code”)[重复]
2017-09-24
15378
我使用下面的过滤函数将数据从一张表复制到另一张表,进行过滤,以便仅在列 AE = TRUE 时复制数据。 当我使用此脚本时,它给出了错误:
TypeError: Cannot call method "getRange" of null. (line 9, file "Code")'
我不确定 getRange 部分是什么意思。
function myFunction() {
var sss =
SpreadsheetApp.openById('1V3Tl4h43AC92K4gQtvdZ07k01QJOnfwSD1BKKTziHJU');
var ss = sss.getSheetByName('<20 Filter');
var range = ss.getRange('A:AE');
var data = range.getValues();
var tss =
SpreadsheetApp.openById('1AKL81cN5QWTWSsSfuMGcFRED5QpeAK022k8VwrPl7sA');
var ts = tss.getSheetByName('Zones >20 Riders');
ts.getRange(ts.getLastRow()+1, 1, data.length,
data[0].length).setValues(data);
var range = ts.getRange(ts.getLastRow()+1, 1, data.length,
data[0].length)
var rawData = range.getValues() // get value from spreadsheet 2
var data = [] // Filtered Data will be stored in this array
for (var i = 0; i< rawData.length ; i++){
if(rawData[i][30] == "TRUE")
{
data.push(rawData[i])
}
}
}
或者我可以使用此脚本吗?
function myFunction() {
var sss = SpreadsheetApp.openById('1V3Tl4h43AC92K4gQtvdZ07k01QJOnfwSD1BKKTziHJU'); //replace with source ID
var ss = sss.getSheetByName('<20 Filter'); //replace with source Sheet tab name
var range = ss.getRange('A:AE'); //assign the range you want to copy
var rawData = range.getValues() // get value from spreadsheet 1
var data = [] // Filtered Data will be stored in this array
for (var i = 0; i< rawData.length ; i++){
if(rawData[i][30] == "TRUE")
{
data.push(rawData[i])
}
}
var tss = SpreadsheetApp.openById('1AKL81cN5QWTWSsSfuMGcFRED5QpeAK022k8VwrPl7sA'); //replace with destination ID
var ts = tss.getSheetByName('Zones <20 Riders'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
但它给出了错误:
TypeError: Cannot read property "length" from undefined. (line 15, file "Code")
2个回答
这意味着
tss.getSheetByName('Zones >20 Riders');
没有返回任何内容。您需要在尝试访问工作表之前检查是否确实找到了工作表。
以下是 Google Docs 提供的示例:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses");
if (sheet != null) {
// Do your thing
}
m.spyratos
2017-09-24
尝试按如下方式更改
var sss =
SpreadsheetApp.openById('1V3Tl4h43AC92K4gQtvdZ07k01QJOnfwSD1BKKTziHJU');
var ss = sss.getSheetByName('<20 Filter');
var sheet = ss.getSheets()[0];
var range = sheet.getRange('A:AE');
var data = range.getValues();
注意:
getSheets
返回此范围所属的工作表。
lpradhap
2017-09-24