0

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?

14
  • 1
    if you log LogDoc, what type of object is it? Commented Mar 9, 2018 at 21:21
  • 1
    Just run Logger.log(logDoc) Commented Mar 9, 2018 at 21:37
  • 1
    Have you tried to cast it? looks like LogDoc is a file and you want a document, try calling ((Document)LogDoc).getBody() Commented Mar 9, 2018 at 21:41
  • 1
    what error does it give you? Commented Mar 9, 2018 at 21:48
  • 2
    The cast doesn't seem to work, humm. Try var doc = DocumentApp.openById(LogDoc.getId()); doc.getBody()? Commented Mar 9, 2018 at 21:53

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.