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

[et_pb_section bb_built=”1″ admin_label=”section”][et_pb_row admin_label=”row” background_position=”top_left” background_repeat=”repeat” background_size=”initial”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” border_style=”solid” background_position=”top_left” background_repeat=”repeat” background_size=”initial” _builder_version=”3.0.50″ custom_margin=”-100px|||”]

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.

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row admin_label=”Row” background_position=”top_left” background_repeat=”repeat” background_size=”initial”][et_pb_column type=”4_4″][et_pb_text admin_label=”PostEnd” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid” saved_tabs=”all” global_module=”22296″ background_position=”top_left” background_repeat=”repeat” background_size=”initial”]


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

Baz Roberts (Google+Flipboard / Twitter)


[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]