Reputation: 178
I know some JS and I have a fair amount of experience with VBA scripts for MSO, but I'm just starting to learn how to script google docs, and I'm having difficulty with the google scripting environment.
I created a doc on my google drive, and I am trying to log outputs in the code, but I get an error when I get the error TypeError: Cannot find function getBody in object Excel Download Macro Log File. (line 56, file "Code") when I try to run this code:
function logDataFromCode() {
var LogDoc = DriveApp.getFilesByName('Excel Download Macro Log File').next();
// Access the body of LogDoc, then add a paragraph with relevant data from script:
LogDoc.getBody()
.appendPageBreak()
.appendParagraph("[put variables to log here]");
}
Can someone please explain why I am getting this error?
Upvotes: 0
Views: 523
Reputation: 9862
You are getting that error because the class returned by getFilesByName("name").next()
is of type File
, not Document
.
The solution is to use the File
from DriveApp
to explicitly open a Document
via DocumentApp
:
var LogDoc, files = DriveApp.getFilesByName("the file name");
if(files.hasNext()) {
LogDoc = DocumentApp.openById(files.next().getId());
} else {
// No files matched the name supplied to DriveApp
return;
}
Upvotes: 3
Reputation: 1283
Your variable LogDoc
is a File
, but you want it to be a Document
The best way to cast it is to get it's id and then accesing it using the DocumentApp
to open it.
Like this:
var LogFile = DriveApp.getFilesByName('Excel Download Macro Log File').next();
var LogDoc = DocumentApp.openById(LogFile.getId());
// Access the body of LogDoc, then add a paragraph with relevant data from script:
LogDoc.getBody()
Upvotes: 2