【问题标题】:Google Apps Script - TypeError: Cannot read property 'getRange' of nullGoogle Apps 脚本 - TypeError:无法读取 null 的属性“getRange”
【发布时间】:2021-09-15 07:39:29
【问题描述】:

已经很晚了,我已经研究了一段时间,我有一个版本在另一个谷歌表格上工作,但在那个版本中,我一次只尝试使用一张工作表。这一次,我试图从多张纸上调用范围,我认为我做错了......我很感激任何帮助,但无论我如何尝试安排这个,我都会不断收到“无法读取属性'getRange' null 就像我什么都没有打电话一样。:\

编辑(adding doc link)

function onTrigger() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Routed Options')
      .addItem('Show alert', 'showAlert')
      .addToUi();
}

function AddRoutedOptions() {
  var ui = SpreadsheetApp.getUi(); // Same variations.
  var result = ui.alert(
      'Add all Routed tasks to the Project Tracker?',
      'Are you sure you want to add all Routed tasks to the Project Tracker?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
  var gs1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Getting Started Checklist');
  var mp1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Mid-Point Checklist'); 
  var pg1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pre-Go Live Checklist');
  var vc1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Video Course Checklist');
  gs1.getRange('B29:B30').copyTo(gs1.getRange('E20:E21'), {contentsOnly:true});
  gs1.getRange('B31').copyTo(gs1.getRange('H18'), {contentsOnly:true});
  mp1.getRange('B28:B31').copyTo(mp1.getRange('B21:B24'), {contentsOnly:true});
  mp1.getRange('B32:B33').copyTo(mp1.getRange('E12:E13'), {contentsOnly:true});
  mp1.getRange('B34').copyTo(mp1.getRange('H18'), {contentsOnly:true});
  pg1.getRange('B32:B35').copyTo(pg1.getRange('E25:E28'), {contentsOnly:true});
  vc1.getRange('B32:C37').copyTo(vc1.getRange('G10:H15'), {contentsOnly:true});
} else {
    // User clicked "No" or X in the title bar.
    ui.alert('No tasks were added.');
  }
}

function RemoveRoutedOptions() {
  var ui = SpreadsheetApp.getUi(); // Same variations.
  var result = ui.alert(
      'Remove all Routed tasks from the Project Tracker?',
      'Are you sure you want to remove all Routed tasks from the Project Tracker?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
  var gs1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Getting Started Checklist')
  var mp1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Mid-Point Checklist')
  var pg1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pre-Go Live Checklist')
  var vc1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Video Course Checklist')
  gs1.getRange('E20:E21').clearContent()
  mp1.getRange('B21:B24').clearContent()
  pg1.getRange('E25:E28').clearContent()
  vc1.getRange('G10:H15').clearContent()
} else {
    // User clicked "No" or X in the title bar.
    ui.alert('No tasks were removed.')
  }
}

【问题讨论】:

  • 我已经查看了文档here: 并尝试通过将 SpreadsheetApp.openByID 与我的其余代码一起使用来更具体,但这也不起作用。 :(
  • 在 AddRoutedOptions() 中,有几个对 getRange() 的调用。你有没有弄清楚是哪个调用导致了错误?一般来说,我认为这是由错误的工作表名称引起的。工作表名称是否真的存在于活动电子表格中?
  • 如果您尝试创建一个小于当前代码的minimal reproducible example,您可能能够解决您自己的问题。但至少那时你可能有更好的机会获得更多帮助。目前,这个问题涉及的范围太多,我们大多数人都无法考虑为自己设置。我可能会为自己的问题做这件事,但为别人做这件事的可能性不大。
  • @IncluCat 是的,我确保工作表名称存在并且名称末尾没有空格。我现在在我的原始问题中添加了一个测试文档,这可能有助于了解上下文和我遇到的问题。感谢您的帮助和回应。
  • @Cooper 对此感到抱歉,这点很有效。我已经编辑了我的问题并添加了一个测试文档,该文档应该完全复制我正在尝试做的事情。所有代码都在 SecondaryCode.gs 中。本质上,选择删除选项时,应在我使用的.getRange中删除所有选定的数据。当我使用 Add 选项时,它应该使用 .getRange 从工作表的一部分提取数据,然后使用 .copyTo 概述的特定单元格。我意识到在我完成之前我不擅长在自己的代码中评论事物,所以我想给出一个细分。提前感谢您的帮助和反馈

标签: google-apps-script typeerror


【解决方案1】:

好消息,我解决了我的问题并让它工作了,但我不知道我明白为什么,所以如果有人仍然可以帮助我指出这一点,我将不胜感激。它归结为将我的“var”之后的部分更改为 sheet1、sheet2、sheet3、sheet4 等,而不是 gs1、mp1、pg1、vc1。我不知道为什么后者不起作用,这就是我所做的全部更改,并且效果很好......

“var”之后的第一件事必须是工作表吗?它不能是我们自己的价值,还是我说错了?抱歉,对应用脚本很陌生,还在学习很多东西!

function AddRoutedOptions() {
  var ui = SpreadsheetApp.getUi(); // Same variations.
  var result = ui.alert(
      'Add all Routed tasks to the Project Tracker?',
      'Are you sure you want to add all Routed tasks to the Project Tracker?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Getting Started Checklist');
  var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Mid-Point Checklist');
  var sheet3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pre-Go Live Checklist');
  var sheet4 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Video Course Checklist');
    sheet1.getRange('B29:B30').copyTo(sheet1.getRange('E20:E21'), {contentsOnly:true});
    sheet1.getRange('B31').copyTo(sheet1.getRange('H18'), {contentsOnly:true});
    sheet2.getRange('B28:B31').copyTo(sheet2.getRange('B21:B24'), {contentsOnly:true});
    sheet2.getRange('B32:B33').copyTo(sheet2.getRange('E12:E13'), {contentsOnly:true});
    sheet2.getRange('B34').copyTo(sheet2.getRange('H18'), {contentsOnly:true});
    sheet3.getRange('B32:B35').copyTo(sheet3.getRange('E25:E28'), {contentsOnly:true});
    sheet4.getRange('B32:C37').copyTo(sheet4.getRange('G10:H15'), {contentsOnly:true});
} else {
    // User clicked "No" or X in the title bar.
    ui.alert('No tasks were added.');
  }
}

【讨论】:

  • 您正在尝试将 StackOverflow 用作类似论坛的社区。 Stackoverflow 不能以这种方式工作 - 它是“问题->答案”对的常见问题解答样式数据库。我敢打赌,没有人会在答案中寻找您提出的新问题,因此他们将保持未回答状态。建议你去take a tourcheck help center熟悉一下Stackoverflow是什么以及如何使用它
【解决方案2】:

感谢您提供更多信息。
我在调试模式下运行了您的脚本,发现vc1 变为空。
在您的电子表格中,有一个名为 Video Course List 的工作表,但您将 Video Course Checklist 传递给 getSheetByName()。
请检查名称。

【讨论】:

    猜你喜欢
    • 2019-03-26
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多