Dav*_*vid 2 google-sheets google-apps-script
尝试运行该脚本时,我一直收到此错误。它已经运行了好几天。它在Google文档电子表格中。
TypeError:无法调用null的方法“ getRange”。(第0行)
ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "New PO", functionName: "NewPO"}];
ss.addMenu("New PO", menuEntries);
}
function NewPO() {
SpreadsheetApp.getActiveSheet().insertRowsBefore(1,6);
// Adjust this range accordingly, these are the cells that will be
// copied. Format is getRange(startRow, startCol, numRows, numCols)
ss.getSheetByName("PO Form").getRange(1, 1, 6, 8)
.copyTo(SpreadsheetApp.getActiveSheet().getRange(1, 1, 6, 8));
}
function onEdit(e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
// 1 is A, 2 is B, ... 8 is H
if (r.getColumn() == 8 && r.getValue() == "x") {
r.setValue(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));
}
}
Run Code Online (Sandbox Code Playgroud)
有什么可以看到的错误信息吗??
您是否检查的返回值ss.getSheetByName("PO Form")。这很有可能null。您还具有一个名为的类级别变量ss和一个名为的方法变量ss。如果您不太谨慎,这种阴影可能会导致问题。