10 Apps Scripts For Docs
10 Apps Scripts For Docs
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:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
1
● 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.
function createDocumentFromTemplate() {
var templateId = 'TEMPLATE_DOCUMENT_ID_HERE';
var documentId = DriveApp.getFileById(templateId).makeCopy().getId();
var doc = DocumentApp.openById(documentId);
var body = doc.getBody();
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
2
body.replaceText('{{Name}}', 'John Doe');
body.replaceText('{{Date}}', new Date().toDateString());
doc.saveAndClose();
}
function formatDocument() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
paragraphs.forEach(function(paragraph) {
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING1);
});
}
3. Mail Merge
function sendMailMerge() {
var docTemplate = "TEMPLATE_DOCUMENT_ID_HERE";
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
3
var docName = "Custom Doc for ";
var recipients = [["email@example.com", "John Doe"],
["email2@example.com", "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]);
copyDoc.saveAndClose();
DriveApp.getFileById(copyId).setTrashed(true);
});
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
4
function requestApproval() {
var doc = DocumentApp.getActiveDocument();
var approvers = ['approver@example.com'];
approvers.forEach(function(approver) {
DocumentApp.getActiveDocument().addEditor(approver);
MailApp.sendEmail(approver, "Document Approval Request", "Please
review and approve the document: " + doc.getUrl());
});
}
function insertDynamicContent() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
6. Collaboration Enhancements
function shareDocumentWithGroup() {
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
5
var doc = DocumentApp.getActiveDocument();
var emailAddresses = ['user1@example.com', 'user2@example.com'];
emailAddresses.forEach(function(email) {
doc.addEditor(email);
});
}
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);
}
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
6
function insertExternalData() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
function renameAndMoveDocuments() {
var files = DriveApp.getFilesByName("Old Name");
while (files.hasNext()) {
var file = files.next();
file.setName("New Name");
var folder = DriveApp.getFolderById("TARGET_FOLDER_ID");
folder.createFile(file);
}
}
function onOpen() {
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
7
var ui = DocumentApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Show Dialog', 'showDialog')
.addToUi();
}
function showDialog() {
var html = HtmlService.createHtmlOutput('<p>Hello, world!</p>')
.setWidth(250)
.setHeight(300);
DocumentApp.getUi().showModalDialog(html, 'A Custom Dialog');
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
8