I'm trying to teach myself to use Google Apps Script, but somehow every function I try returns this error immediately. And since I'm a huge beginner, I don't know what I'm doing wrong.
Here's a simple example of a code I tried to run:
function myFunction(){
//application
//file
var ad = DocumentApp.getActiveDocument();
var docBody = ad.getBody () ;
var paragraphs = docBody.getParagraphs();
//paragraphs[0]. setText ("MY NEW TEXT"):
//var attr = paragraphs[0].getAttributes() ;
//Logger.log(attr);
paragraphs[0].setAttributes({FONT_SIZE:40});
}
Yet no matter what I'm running really, I get this:
TypeError: Cannot read property 'getBody' of null myFunction @ Code.gs:5
What am I doing wrong?
I have an open Google Doc, I've allowed permissions to run the script project. What else should I try? Thanks.
CodePudding user response:
In order to get getActiveDocument()
working you need to use it in a script that is container-bound:
- Create a new Google Document
- Go to
Extensions > Apps Script
- Run your function inside of it.
If your script is not container-bound
, you need to use the openById(id)
or openByUrl(url)
methods, in order to retrieve a Document
CodePudding user response:
Adblock software was preventing the running somehow. Disabled it and runs fine now. Disregard!