google-apps-script - 类型错误 : Cannot find function getCell in object Sheet

标签 google-apps-script

我对于编写与 Google Apps 脚本和 JavaScript 相关的任何内容来说都是全新的。

我已经根据我的需要调整了脚本,但运行它时出现以下错误:

TypeError: Cannot find function getCell in object Sheet

本质上,我试图获取单元格 D4 (4,4) 中的值并将该值传递给变量 emailTo。显然我做得不对。脚本的其余部分应该可以正常工作。任何指导表示赞赏。

// Sends PDF receipt
// Based on script by ixhd at https://gist.github.com/ixhd/3660885

// Load a menu item called "Receipt" with a submenu item called "E-mail Receipt"
// Running this, sends the currently open sheet, as a PDF attachment
function onOpen() {
  var submenu = [{name:"E-mail Receipt", functionName:"exportSomeSheets"}];
  SpreadsheetApp.getActiveSpreadsheet().addMenu('Receipt', submenu);  
}

function exportSomeSheets() {
  // Set the Active Spreadsheet so we don't forget
  var originalSpreadsheet = SpreadsheetApp.getActive();

  // Set the message to attach to the email.
  var message = "Thank you for attending !  Please find your receipt attached.";

  // Construct the Subject Line
  var subject = "Receipt";

  // THIS IS WHERE THE PROBLEM IS
  // Pull e-mail address from D4 to send receipt to
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var emailTo = sheet.getCell(4, 4).getValue();

  // Create a new Spreadsheet and copy the current sheet into it.
  var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
  var projectname = SpreadsheetApp.getActiveSpreadsheet();
  sheet = originalSpreadsheet.getActiveSheet();
  sheet.copyTo(newSpreadsheet);

  // Find and delete the default "Sheet 1"
  newSpreadsheet.getSheetByName('Sheet1').activate();
  newSpreadsheet.deleteActiveSheet();

  // Make the PDF called "Receipt.pdf"
  var pdf = DocsList.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
  var attach = {fileName:'Receipt.pdf',content:pdf, mimeType:'application/pdf'};

  // Send the  constructed email 
  MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});

  // Delete the wasted sheet
  DocsList.getFileById(newSpreadsheet.getId()).setTrashed(true);  
}

最佳答案

问题是 getCell()Range 的方法,而不是 Sheet 的方法。从 Sheet 获取 Range,然后在 Range 对象上使用 getCell()

关于google-apps-script - 类型错误 : Cannot find function getCell in object Sheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14784448/

相关文章:

google-apps-script - 在谷歌脚本中将谷歌电子表格转换为xls

javascript - 自定义函数停止工作

google-apps-script - 根据电子表格中的值自动完成 Google 表单

javascript - 无法读取代码中的属性

google-maps-api-3 - 如何使用谷歌文档中的数据在谷歌地图上显示标记?

google-apps-script - getActiveRange 不返回当前选择

google-apps-script - 删除 Google Apps 脚本文档服务中的内容

google-apps-script - DriveApp : Get File ID with Parent Folder ID/Name and File Name

javascript - 在 Java 脚本中访问已解析的 JSON 响应中的数组

google-apps-script - 谷歌脚本: multiple doPost in one project