How to use script editor in Google docs

Google Apps Script is a quick application development platform for creating business applications that integrate well with G Suite apps. Scripts are simply add-ons, that are purely written in JavaScript to give additional features to Google Docs. For example, you can convert your Google Docs file into a PDF with added script in Google Docs. You just have to write code in modern JavaScript and you also have access to built-in libraries for Google Docs that makes your job much easier. You don’t have to install anything. You can just open a code editor right in your browser, and your scripts run on Google’s servers. Script editor provides easy ways to automate your Google Docs tasks. Let’s see how to use script editor in Google Docs.

Script editor interaction with Google Docs

The script editor is designed to programmatically create Google Docs and also allows you to modify Google Docs. You can also customize the user interface of your Google Docs with new menus, dialog boxes, and sidebars.

Script editor can interact with Docs in two different ways.

  • You can create or modify any script of a Google Docs document only if you are the owner or have appropriate permissions for the document.
  • The script editor is usually bound to a document that gives the script exceptional abilities to alter the user interface.

Check out

How to use Script editor in Google Docs?

You can create a container-bound script withing Google Docs. All you have to is

  • Select Tools > Script editor from the top menu bar. A separate window for the script editor will be opened in your browser.
Script editor in Google Docs
  • You can also visit script.google.com to open the script editor. Before that make sure you have signed in to your Google account. If you are visiting for the very first time, you’ll be redirected to a page that gives basic intro about Apps Script. 
  • You can Click “Start Scripting” to proceed to the script editor.

You can easily create a new document using the below script.

function createDoc() {
 var doc = DocumentApp.create('Sample Document');
 var body = doc.getBody();
 var rowsData = [['Series', 'Movies'], ['GOT', 'After'], ['F.R.I.E.N.D.S', 'Divergent'], ['Starwars', 'Four']];
 body.insertParagraph(0, doc.getName())
     .setHeading(DocumentApp.ParagraphHeading.HEADING1);
 table = body.appendTable(rowsData);
 table.getRow(0).editAsText().setBold(true);
}

The above script is a simple example to create a new Google Docs document in your Google Drive, which includes

  • A paragraph that contains the same text as the document’s name
  • Styles that paragraph as a heading
  • A table based on the provided values in a two-dimensional array.

You can easily make these changes to an existing Google Docs document by replacing DocumentApp.create() with DocumentApp.openById() or openByUrl(). 

You can also make changes to the scripts created inside a Google Docs document (container-bound) by using DocumentApp.getActiveDocument().

How to use script editor to create Google Docs and link for that?

Let’s see how to build and run a simple standalone script that creates a Google Docs file and sends an email to the current user with a link to the Google Docs file.

function createAndSendDocument() {
 // To create a sample new Google Docs named ‘Script editor’
 var doc = DocumentApp.create(‘Script editor’);
// To access the body of the Google Docs document,and then add a paragraph.
 doc.getBody().appendParagraph(‘This document was to demonstrate how to use script editor in Google Docs’);
// To get the URL of the Google Docs document.
 var url = doc.getUrl();
// To get your email address
 var email = Session.getActiveUser().getEmail();
// To get the name of the GoogleDocs document to use as the email subject line.
 var subject = doc.getName();
// To append a new string to the “URL” variable and  use it as the email body.
 var body = ‘Link to your doc: ‘ + url;
// To send yourself the email with a link to the Google Docs document.
 GmailApp.sendEmail(email, subject, body);
}
  • Once you are done with your script in the script editor, save it.
  • Head over to the menu bar and click File > Save. Name your new script in the script editor and click OK.

How to execute the script editor in Google Docs?

  • To execute the script, either Run > Run function > <function name>
How to execute the script editor in Google Docs
  • A dialog box will appear saying “Authorization required”.The Script editor needs your permission to access your data on Google.
  •  Allow to authorize and the script running will be finished in some time.
  • You can now check your Gmail inbox for the new email, and then click the link to open the Google Docs file you have created.
  • You can also view both your script and the Google Docs document saved in Google drive.

The script editor will consider Google Docs as an HTML structured document. To say precisely, Google Docs is composed of various elements like Paragraph Table and more. To use or modify a Google Docs, script editor usually begin with a call to getBody(). This is because that the Body is the master element containing all other elements except for the header section, footer section, and any Footnotes.

How to use script editor as Google Docs add-on in Google Docs?

You can deploy your script saved in script editor as an add-on in docs. To do this, first, create your script and execute it in the script editor. Now follow the below steps.

  • Go the menu bar at the top of the script editor.
  • Click Publish > Deploy as Google add-on
How to use script editor as Google Docs add-on in Google Docs

You can now see your script created in the script editor in the “Add-on” menu of Docs.

How to use script editor in Google Docs to replace text?

You can use the script editor to replace a bulk text in Docs. For example, if you have a spreadsheet full of client information and you want to generate a personalized Docs for each client, then script editor is your one-stop solution.

Even though there are many ways to replace text, the simplest one is the “replaceText()” method. 

Add some placeholders using the script editor in Google Docs?

You can add several lines of placeholder text to your Google Docs very quick and simple. This will make your job so easy that you would be more likely to type the placeholders into the Google Docs document yourself.

function createPlaceholders() {
 var body = DocumentApp.getActiveDocument().getBody();
 body.appendParagraph('{name}');
 body.appendParagraph('{address}');
 body.appendParagraph('{city} {state} {zip}');
}

Replace some placeholders using the script editor in Google Docs

You can also replace the placeholders with properties defined in the client object.

function searchAndReplace() {
 var body = DocumentApp.getActiveDocument()
     .getBody();
 var client = {
   name: 'Joe Script-Guru',
   address: '100 Script Rd',
   city: 'Scriptville',
   state: 'GA',
   zip: 94043
 };

 body.replaceText('{name}', client.name);
 body.replaceText('{address}', client.address);
 body.replaceText('{city}', client.city);
 body.replaceText('{state}', client.state);
 body.replaceText('{zip}', client.zip);
}

You can use the getActiveDocument() method in a stand-alone script only. For scripts not created inside a Google Docs, use can useDocumentApp.create(), openById(), or openByUrl() instead.

Google Docs Script Template

Google Docs Script templates contain three special tags, called scriptlets. You can write codes inside a scriptlet. Google Docs script template call functions defined in other code files, reference global variables, or use any of the Apps Script APIs. 

Uses of script editor in Google Docs

Script editor can do a cluster of things in Docs such as

  • You can add custom menus, dialogs, and sidebars to Google Docs
  • You can write custom functions on your own for Google Docs
  • You can publish web apps solely or embedded in Google Sites.
  • You can interact with other Google services such as Google Analytics, Calendar, Drive, Gmail, and Maps.
  • You can build add-ons to extend Google Docs and publish them to the Add-on store.
  • You can convert an Android app into an Android add-on to exchange data with a user’s Google Doc on a mobile device.
  • You can build a personalized Google Docs that manages responses for Google Forms.
  • You can create Google Docs script templates

You can now make the maximum of your Google Docs by customizing your own script in the script editor and using script editor in Google Docs.

Leave a Comment