[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”:
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, 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.
Add a dot again and a new menu will appear.
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.
Add a dot and another menu will appear.
Now, we want to add some text into the body of the document. To do that, we need the method setText().
As you can see, it’s asking you to add some text in between the brackets.
Type in the text you want, remembering to write it between quote marks. End the line with a semi-colon.
So, the code should look like this now:
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.
The first time you run it, you’ll have to review the permissions as always. Click “Review permissions”.
Then click “Allow”.
As we can see, it’s added the text to the document.
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”.
To open the Script editor in Forms, click on the 3 dots menu and then “Script editor”:
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.
Now, we want to get this active form, so we scroll down and click on the getActiveForm() method.
Add a dot and you’ve guessed it, another menu will appear.
This time we want to change the title of the form, so we scroll down and select the setTitle() method.
As you can see, like the Doc example above, it’s prompting you to add some text in between the brackets.
Here, I’ve added “Questionnaire”. Then I end the line with a semi-colon.
Press the play button to run the program.
The first time this will prompt you to review the permissions. Click on “Review permissions”.
Then click “Allow”.
As you can see, this changed the title from “Untitled form” to “Questionnaire”.
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:
- “Beginner’s Guide to Google Drive” – iBooks store / Kindle store
- “Beginner’s Guide to Google Forms” – iBooks store / Kindle store
- “Beginner’s Guide to Google Sheets” – iBooks store / Kindle store
- “Beginner’s Guide to Google Docs” – iBooks Store / Kindle store
- “Beginner’s Guide to Google Slides” – iBooks Store / Kindle store
- “Google Sheet Functions – A step-by-step guide” – iBooks Store / Kindle Store
Baz Roberts (Google+ / Flipboard / Twitter)
[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]