Join Scott Simpson for an in-depth discussion in this video Creating and editing a document, part of Learning Google Apps Script.
- Let's take a look at working with documents using the DocumentApp class. I have the developer reference open here to the Documents surface, and I encourage you to explore it to find out all the great stuff you can do with Docs. There's much more here than I can show you in this up and running course, but I'll go through some of the basics. The first thing I want to do is create a document. And, if I click on DocumentApp here and scroll down to the methods, I can see there's a Create class. I'll switch over to the IDE, and I'll declare a variable called doc.
I'll write var doc. Then I'll use the DocumentApp class. Alright, DocumentApp.create, and I can see this takes a String value as the name of the document, so I'll add that. I'll run the script, which automatically saves, and if I need to I'll authorize the script. I'll switch over to My Drive, I'll refresh and sure enough here's My Document.
I'll go back to the IDE and I'll press Run a couple more times. Back in My Drive, I see that I have a couple documents named My Document. How does Google tell these documents apart? Each doc has a unique identifier, even if they have the same name. I'll open up a few of these documents. And if we look up here in the URL bar, between d/ and /edit you can find the unique identifier.
But there's an easier way to get it in our script. I'll close out of these, and I'll delete all of my duplicate documents. On a new line I'll call the doc object, and I'll press period to bring out the auto-complete suggestions. I'll scroll through here and sure enough here's a method called getId. I'll write that to the log so we can see what it is. I'll write Logger.log and wrap up doc.getId in the parentheses.
I'll run the script and then I'll go into my logs. And here's the ID for this brand new document, I'll copy that using the keyboard shortcut. Now, I'll use it to tell my script to just work with this one document every time I run the script, rather then creating a new document every time. I'll erase create("My Document");, and I'll replace it with openById, and then paste in the unique ID. I can get rid of the Logger too.
Now let's add some content to the stock. You can think of a document as being made up of different elements. It's not just a bunch of text inside a container. There's a header section, a footer section, and most importantly for us here, a body section. That body section can contain other elements like lists, tables and paragraphs, so we need to drill down from the document level to the body and then to a paragraph before we can really start working with text. Over here in the reference guide, you can see the full documentation about the document, body and paragraph classes among others.
Back in the IDE, let's first get to the document body. I'll write var body = doc.getBody(); and then we'll get the first or zeroth element in the body to work with. I'll write var para1, for paragraph one, = body.getChild, and then I'll put in an interger, in this case 0. Now we can work with the text.
To do that I'll write para1.setText, and I'll write "I'm a document". This method, setText, resets the contents of a paragraph element to be whatever String is passed into it. You can also append text or stick it on the end of whatever is already there with the appendText method. To do that I'll write para1.appendText and I'll add a new line and write "\nDocuments are fun!".
Now I'll run it and we'll take a look at what that document looks like. Okay, but it's a little boring. Let's switch back to the IDE, and make the text a little more interesting. We can style this text in all kinds of different ways. Once again, check out the documentation for all the options. Here I'll change the paragraph text to a heading, and set the size and color to something other than default. Here in the documentation for the paragraph class I found the setHeading method.
I'll click on that to take me to the definition. I see that the parameter is a ParagraphHeading type, so I'll click that, and I'll find that this is an Enum or Enumeration. Enumerations are values which are predefined in the system. I'll scroll down to see them. I'll need to pass one of these values in to set the heading type with the setHeading method. Let's go with number five. I'll switch to the IDE, add a new line, and write para1.setHeading, and then DocumentApp .ParagraphHeading.HEADING5, and I'll choose that from this handy list.
Then I'll set the color of the paragraph. For that I'll write para1.setForgroundColor, which will change the color of the text, and I'll give it an HTML color value. Now let's say white. Then I'll set the background color of the text with para1.setBackgroundColor, and let's go with a blue for that. Finally, I'll set the font size to 22.
I'll write para1.setFontSize(22); Let's run that and see what it looks like. I'll switch to My Document. Cool, that's a pretty clear change, and if I click on the text, I can see it's tagged as Heading 5, which is what we set with the Enum earlier. Now, obviously it's easier to work with documents directly in the editor, rather than using code for most purposes, but being able to change docs programmatically can be really useful if you want to put together a mail merge from a spreadsheet or something like that.
Apps Script is great in that it lets you mix and match data and output methods to build all kinds of custom solutions as we'll see later on.
First, learn how to use simple scripts to edit documents and spreadsheets, work with files in Drive, and find and send messages in Gmail. Then find out how to add custom functionality to your spreadsheets, and build user interface elements for your scripts, using menus, alerts, and dialogs. Finally, Scott shows how to share your work with others by publishing scripts as web apps.
- What is Google Apps Script?
- Understanding script types
- Logging and debugging your scripts
- Scripting common tasks
- Building custom spreadsheet functions
- Creating add-on functionality such as dialogs and sidebars
- Sharing your script as a web app