开发者

TypeError: Cannot call method "getRange" of null. (line 0) [duplicate]

开发者 https://www.devze.com 2023-01-26 04:30 出处:网络
This question already has an answer here: How to fix 'TypeError: Cannot call method "getRange" of null' in Google Apps Script
This question already has an answer here: How to fix 'TypeError: Cannot call method "getRange" of null' in Google Apps Script (1 answer) Closed 4 months ago.

I keep getting this error when I try and run this script. It has run just fine for several days. It is in a Google Docs Spreadsheet.

TypeError: Cannot call method "getRange" of null. (line 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().insertRo开发者_JS百科wsBefore(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"));
  }
}

Is there anything you can see that would cause this error? ​


Have you checked the return value of ss.getSheetByName("PO Form"). It is most likely null. You also have a class level variable namedss and method variables named ss as well. This sort of shadowing can lead to problems if you're not very careful.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号