2
votes

I'm trying to edit Google's premade app script that sends emails from a spreadsheet.

I added a new variable currentp to grab include the URL of the spreadsheet in the emails sent by the script. When I try to run this script I receive the following error:

TypeError: Cannot find function getUrl in object Sheet. (line 17, file "Code")

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  
  var numRows = 2;   
  var dataRange = sheet.getRange(startRow, 1, numRows, 3)

  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0]; 
    var message = row[1];      
    var currentp =  sheet.getUrl()  //current spreadsheet page
    var emailSent = row[2];     
    if (emailSent != EMAIL_SENT) {  
      var subject = "Sending emails from a Spreadsheet";
      MailApp.sendEmail(emailAddress, subject, message,currentp); // I would like the URL to be included in the email.
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);

      SpreadsheetApp.flush();
    }
  }
}
1

1 Answers

2
votes

GetActiveSheet() returns a Sheet object, which is a "tab" within a Spreadsheet file. The getUrl() method exists only on the Spreadsheet File object, so you need to get the Spreadsheet separately from the Sheet.

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();

then to get the URL:

var currentp = spreadsheet.getUrl();

See: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet https://developers.google.com/apps-script/reference/spreadsheet/sheet