Some common apps script for docs 10 Apps Scripts Coding Examples

Google Docs, Google Apps Script is commonly used to automate and extend its capabilities, making document creation, collaboration, and management more efficient. Here are the top 10 common Google Apps Script uses specifically tailored for Google Docs:

  • Automated Document Generation: Scripts to create documents from templates, automatically filling in specific details based on user input or external data sources. This is often used for generating reports, invoices, or personalized letters.
  • Content Manipulation and Formatting: Scripts that automate the editing process, such as applying specific formatting styles, inserting or replacing text based on certain criteria, or reorganizing document structures.
  • Mail Merge: Scripts that combine Google Docs with Gmail for sending personalized email messages or letters to multiple recipients, where each document is customized with information specific to each recipient.
  • Document Approval Workflows: Custom workflows that manage document review and approval processes, including sending documents to approvers, tracking status, and managing revisions.
  • Dynamic Content Insertion: Inserting dynamic content into a document, such as images, tables, or charts from external sources, and updating them automatically.
  • Collaboration Enhancements: Scripts designed to enhance collaboration features, such as automatically sharing documents with a group of users, managing permissions, or notifying collaborators about changes or comments.
  • Automated Comment Management: Scripts to automatically add, remove, or respond to comments within a document, useful for editorial processes or collaborative feedback.
  • Integration with External Data and Services: Connecting Google Docs to external APIs to pull in data from web services, databases, or other platforms, allowing for the automatic insertion of up-to-date information into documents.
  • Document Organization and Management: Scripts that help organize and manage documents within Drive, including renaming, moving, or converting documents in bulk, based on specific criteria or schedules.
  • Custom Menus and Dialogs: Adding custom menus, sidebars, or modal dialogs to Google Docs to provide users with easy access to custom scripts, additional document functions, or integration with other services.

1. Automated Document Generation

function createDocumentFromTemplate() {

 var templateId = ‘TEMPLATE_DOCUMENT_ID_HERE’;

 var documentId = DriveApp.getFileById(templateId).makeCopy().getId();

 var doc = DocumentApp.openById(documentId);

 var body = doc.getBody();

 body.replaceText(‘{{Name}}’, ‘John Doe’);

 body.replaceText(‘{{Date}}’, new Date().toDateString());



2. Content Manipulation and Formatting

function formatDocument() {

 var doc = DocumentApp.getActiveDocument();

 var body = doc.getBody();

 body.replaceText(‘Old Text’, ‘New Text’);

 var paragraphs = body.getParagraphs();

 paragraphs.forEach(function(paragraph) {




3. Mail Merge

function sendMailMerge() {

 var docTemplate = “TEMPLATE_DOCUMENT_ID_HERE”;

 var docName = “Custom Doc for “;

 var recipients = [[“”, “John Doe”], [“”, “Jane Doe”]];

 recipients.forEach(function(recipient) {

 var docCopy = DriveApp.getFileById(docTemplate).makeCopy(docName + recipient[1]);

 var copyId = docCopy.getId();

 var copyDoc = DocumentApp.openById(copyId);

 var copyBody = copyDoc.getBody();

 copyBody.replaceText(‘{{Name}}’, recipient[1]);


 MailApp.sendEmail(recipient[0], “Your Custom Doc”, “Please find attached”, {

 attachments: [docCopy.getAs(MimeType.PDF)],

 name: ‘Automated Emailer Script’





4. Document Approval Workflows

function requestApproval() {

 var doc = DocumentApp.getActiveDocument();

 var approvers = [‘’];

 approvers.forEach(function(approver) {


 MailApp.sendEmail(approver, “Document Approval Request”, “Please review and approve the document: ” + doc.getUrl());



5. Dynamic Content Insertion

function insertDynamicContent() {

 var doc = DocumentApp.getActiveDocument();

 var body = doc.getBody();

 var imageUrl = ‘’;

 var image = UrlFetchApp.fetch(imageUrl).getBlob();



6. Collaboration Enhancements

function shareDocumentWithGroup() {

 var doc = DocumentApp.getActiveDocument();

 var emailAddresses = [‘’, ‘’];

 emailAddresses.forEach(function(email) {




7. Automated Comment Management

function addCommentToText() {

 var doc = DocumentApp.getActiveDocument();

 var body = doc.getBody();

 var searchText = body.findText(“specific text”);

 if (searchText) {

 var startPosition = searchText.getStartOffset();

 var endPosition = searchText.getEndOffsetInclusive();

 var textRange = doc.newRange().addRange(searchText.getElement(), startPosition, searchText.getElement(), endPosition).build();

 doc.addComment(“Please review this section.”, textRange);



8. Integration with External Data and Services

function insertExternalData() {

 var doc = DocumentApp.getActiveDocument();

 var body = doc.getBody();

 var response = UrlFetchApp.fetch(“”);

 var data = JSON.parse(response.getContentText());

 body.appendParagraph(“External Data: ” + data.someField);


9. Document Organization and Management

function renameAndMoveDocuments() {

 var files = DriveApp.getFilesByName(“Old Name”);

 while (files.hasNext()) {

 var file =;

 file.setName(“New Name”);

 var folder = DriveApp.getFolderById(“TARGET_FOLDER_ID”);




10. Custom Menus and Dialogs

function onOpen() {

 var ui = DocumentApp.getUi();

 ui.createMenu(‘Custom Menu’)

 .addItem(‘Show Dialog’, ‘showDialog’)



function showDialog() {

 var html = HtmlService.createHtmlOutput(‘<p>Hello, world!</p>’)



 DocumentApp.getUi().showModalDialog(html, ‘A Custom Dialog’);