TypeError: Cannot find function getCell in object

2019-08-02 07:49发布

问题:

I am completely new to coding anything related to Google Apps Script and JavaScript in general.

I've adapted a script to my needs, but I am getting the following error when I run it:

TypeError: Cannot find function getCell in object Sheet

Essentially, I am trying to get the value in cell D4 (4,4) and pass that value to the variable emailTo. I'm obviously not doing it correctly. The rest of the script should work fine. Any guidance is appreciated.

// 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);  
}

回答1:

The issue is that getCell() is a method of Range, not Sheet. Get a Range from the Sheet, then use getCell() on the Range object