【问题标题】:TypeError: Cannot read property 'getRange' of null (line 14, file "Code")TypeError:无法读取 null 的属性“getRange”(第 14 行,文件“代码”)
【发布时间】:2020-07-14 12:34:58
【问题描述】:

我在尝试在 Google App 脚本中运行电子邮件发送脚本时遇到此错误。我对 Javascript 没有任何了解(我打算在学习 Python 之后再学习),我从 Google App 脚本提供的示例代码中得到了这段代码。

Google 电子表格的行数和列数:https://prnt.sc/tha2o1

下面是代码。

// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';

/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */
function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 6; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 7);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var message = row[1]; // Second column
    var emailSent = row[3]; // Third column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      var subject = 'Sending emails from a Spreadsheet';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

【问题讨论】:

标签: javascript google-apps-script google-sheets


【解决方案1】:

我正在运行一个独立的脚本,但它不起作用。如果您希望使用 Google Sheets/Docs/Forms/Sites,您应该运行容器绑定脚本。这是运行容器绑定脚本的方法。 https://developers.google.com/apps-script/guides/bound

【讨论】:

  • 如果您完全按照教程进行操作,在第 3 步中会显示从 google 表格打开脚本编辑器。那应该已经创建了一个容器绑定脚本。
  • 后来@Tanaike 询问时发现了。
猜你喜欢
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多