I'm new to Google Script and decided to try out the documentation code here located at the bottom. The code is located below:
function createAndSendDocument() {
// Create a new Google Doc named 'Hello, world!'
var doc = DocumentApp.create('Hello, world!');
// Access the body of the document, then add a paragraph.
doc.getBody().appendParagraph('This document was created by Google Apps Script.');
// Get the URL of the document.
var url = doc.getUrl();
// Get the email address of the active user - that's you.
var email = Session.getActiveUser().getEmail();
// Get the name of the document to use as an email subject line.
var subject = doc.getName();
// Append a new string to the "url" variable to use as an email body.
var body = 'Link to your doc: ' + url;
// Send yourself an email with a link to the document.
GmailApp.sendEmail(email, subject, body);
}
The code works just fine, but since I did not want it to create a new document every time I run this, I edited the code to:
function sendDocument() {
// Get the name of the document to use as an email subject line.
var doc = DocumentApp.getActiveDocument();
// Get the email address of the active user - that's you.
var email = Session.getActiveUser().getEmail();
// Append a new string to use as an email body.
var body = 'Link to your doc: ' + doc.getUrl() + '. Happy editing! :D';
// Send yourself an email with a link to the document.
GmailApp.sendEmail(email, doc, body);
}
However, I could not run the above code and an error message popped up saying:
TypeError: Cannot call method "getUrl" of null. (line 9, file "")
Here's my execution transcript:
[15-10-03 10:13:26:875 PDT] Starting execution
[15-10-03 10:13:26:881 PDT] DocumentApp.getActiveDocument() [0 seconds]
[15-10-03 10:13:26:883 PDT] Session.getActiveUser() [0 seconds]
[15-10-03 10:13:26:883 PDT] User.getEmail() [0 seconds]
[15-10-03 10:13:26:886 PDT] Execution failed: TypeError: Cannot call method "getUrl" of null. (line 9, file "Code") [0.003 seconds total runtime]
Can anyone please help me with this problem? Thanks in advance! :)