Apps Script Basics (2) – First Docs and Forms Scripts

Following on from my last post on creating your first script, here we’re going to edit a Google Doc and also a Google Form. This will show you how you can access Docs and Forms, not just Sheets with Apps Script and it will introduce you to how it’s possible to add and edit the text in those documents.


First Google Docs Script

In this little script, we’re going to write some text in a Google Doc.

To open the Script editor in Docs, click on “Tools” from the menu and then “Script editor”:

First Scripts - 3

We leave the function that is already written in there and in between the curly brackets, we type DocumentApp. Like the SpreadsheetApp in the previous post, this is telling the computer which class to access, in other words, whether it is a Google Doc, Google Sheet, etc.

Then add a dot and various possible options will appear in the menu.

First Scripts - 33

First, we want to get the current active document. So, scroll down and you’ll see the method called getActiveDocument(). Click on it to add it.

First Scripts - 34

Add a dot again and a new menu will appear.

First Scripts - 35

Now we have the document we want to get the document’s body. So, scroll down until you see the method getBody() then select it.

First Scripts - 36

Add a dot and another menu will appear.

First Scripts - 37

Now, we want to add some text into the body of the document. To do that, we need the method setText().

First Scripts - 38

As you can see, it’s asking you to add some text in between the brackets.

First Scripts - 39

Type in the text you want, remembering to write it between quote marks. End the line with a semi-colon.

First Scripts - 40

So, the code should look like this now:

First Scripts - 41

So, to summarise, we’re getting the Document app, getting the active document, then getting the body of that document and then adding some text to it.

Press the play button on the toolbar to run the program.

First Scripts - 49

The first time you run it, you’ll have to review the permissions as always. Click “Review permissions”.

First Scripts - 12

Then click “Allow”.

First Scripts - 13

As we can see, it’s added the text to the document.

Docs and Forms Scripts - 25


First Google Forms Script

Now, let’s look at how we can edit text in a Google Form. Here, we’re going to change the title of the form.

Create a new form. At the moment, as it’s a new one, the title will just say “Untitled form”.

First Scripts - 42

To open the Script editor in Forms, click on the 3 dots menu and then “Script editor”:

First Scripts - 4

This time we want to edit a form, so we use the FormApp. Type that in and add a dot to bring up the menu.

First Scripts - 43

Now, we want to get this active form, so we scroll down and click on the getActiveForm() method.

First Scripts - 44

Add a dot and you’ve guessed it, another menu will appear.

First Scripts - 45

This time we want to change the title of the form, so we scroll down and select the setTitle() method.

First Scripts - 46

As you can see, like the Doc example above, it’s prompting you to add some text in between the brackets.

First Scripts - 47

Here, I’ve added “Questionnaire”. Then I end the line with a semi-colon.

First Scripts - 48

Press the play button to run the program.

First Scripts - 49

The first time this will prompt you to review the permissions. Click on “Review permissions”.

First Scripts - 14

Then click “Allow”.

First Scripts - 15

As you can see, this changed the title from “Untitled form” to “Questionnaire”.

First Scripts - 16

Hopefully, you can start to see how with a little bit of Apps Script knowledge you will be able to edit Docs, Forms, and Sheets allowing you to do this automatically rather than manually.

Here’s a copy of the Google Doc, which includes the code.

Here’s a copy of the Google Form, which also includes the code.


 eBooks available on Drive, Forms, Sheets, Docs, Slides, and Sheet Functions:

Baz Roberts (Google+Flipboard / Twitter)


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s