Google Apps Script is a powerful way to automate, customize, and supercharge your workflow in Google Docs. Whether you want to clean up references, format your document, or do advanced text processing, a simple script can save you hours. Here are 10 essential Google Apps Script snippets every Google Docs power user should know!
1. Remove All [cite: 1234] References
Description:
This script removes every instance of [cite: 1234] (where the number can be anything) from your document.
function removeCiteReferences() {
var body = DocumentApp.getActiveDocument().getBody();
var pattern = /\[cite:\s*\d+\]/g;
var text = body.getText();
var cleanedText = text.replace(pattern, ”);
body.setText(cleanedText);
}
2. Convert All “Exercise” Sections to Heading 2
Description:
Automatically converts every paragraph starting with “Exercise <number>:” to the Heading 2 style.
function convertExercisesToHeading2() {
var body = DocumentApp.getActiveDocument().getBody();
var totalParas = body.getNumChildren();
for (var i = 0; i < totalParas; i++) {
var element = body.getChild(i);
if (element.getType() == DocumentApp.ElementType.PARAGRAPH) {
var para = element.asParagraph();
var text = para.getText();
if (/^Exercise\s+\d+:/i.test(text)) {
para.setHeading(DocumentApp.ParagraphHeading.HEADING2);
}
}
}
}
3. Find and Highlight All TODOs
Description:
Highlights every “TODO” in yellow so you can spot unfinished work at a glance.
function highlightTODOs() {
var body = DocumentApp.getActiveDocument().getBody();
var found = body.findText(‘TODO’);
while (found) {
found.getElement().setBackgroundColor(‘yellow’);
found = body.findText(‘TODO’, found);
}
}
4. Replace All Instances of a Word
Description:
Find and replace every instance of a specific word or phrase.
function replaceWord(oldWord, newWord) {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(oldWord, newWord);
}
// Example usage: replaceWord(‘foo’, ‘bar’);
5. Add a Table of Contents at the Top
Description:
Inserts an up-to-date Table of Contents at the beginning of your document.
function insertTableOfContents() {
var body = DocumentApp.getActiveDocument().getBody();
body.insertParagraph(0, “Table of Contents”).setHeading(DocumentApp.ParagraphHeading.HEADING1);
body.insertTableOfContents(1, DocumentApp.TableOfContentsType.FLAT);
}
6. Remove Extra Blank Lines
Description:
Deletes consecutive blank paragraphs for a cleaner document.
function removeExtraBlankLines() {
var body = DocumentApp.getActiveDocument().getBody();
var numChildren = body.getNumChildren();
for (var i = numChildren – 1; i > 0; i–) {
var curr = body.getChild(i);
var prev = body.getChild(i – 1);
if (curr.getType() == DocumentApp.ElementType.PARAGRAPH &&
prev.getType() == DocumentApp.ElementType.PARAGRAPH &&
!curr.asParagraph().getText() && !prev.asParagraph().getText()) {
body.removeChild(curr);
}
}
}
7. List All Images with Their Index
Description:
Prints a log of all images in your document with their position number.
function listAllImages() {
var body = DocumentApp.getActiveDocument().getBody();
var imgs = body.getImages();
for (var i = 0; i < imgs.length; i++) {
Logger.log(“Image ” + (i + 1) + ” found.”);
}
}
8. Count Words in Your Document
Description:
Counts and logs the number of words in your document.
function countWords() {
var text = DocumentApp.getActiveDocument().getBody().getText();
var wordCount = text.match(/\b\S+\b/g)?.length || 0;
Logger.log(‘Total words: ‘ + wordCount);
}
9. Change All Headings to Title Case
Description:
Automatically updates all headings (H1, H2, H3, etc.) to Title Case.
function headingsToTitleCase() {
var body = DocumentApp.getActiveDocument().getBody();
var total = body.getNumChildren();
for (var i = 0; i < total; i++) {
var elem = body.getChild(i);
if (elem.getType() == DocumentApp.ElementType.PARAGRAPH) {
var para = elem.asParagraph();
var heading = para.getHeading();
if (heading != DocumentApp.ParagraphHeading.NORMAL) {
var txt = para.getText();
var newTxt = txt.replace(/\w\S*/g, w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase());
para.setText(newTxt);
}
}
}
}
10. Insert the Current Date Anywhere
Description:
Quickly insert today’s date wherever your cursor is in the doc.
function insertCurrentDate() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
var dateStr = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), “yyyy-MM-dd”);
cursor.insertText(dateStr);
} else {
DocumentApp.getUi().alert(‘Place your cursor in the document first.’);
}
}
How to Use These Snippets
- Open your Google Doc.
- Go to Extensions → Apps Script.
- Paste your chosen script(s) into the code editor.
- Save and run the function you want (authorize if prompted).
Tip: You can combine several of these scripts into one project for a custom workflow!
Supercharge Your Docs
Whether you’re managing a textbook, collaborating on technical docs, or just keeping things tidy, these scripts give you a huge productivity boost in Google Docs. Try them out, adapt them to your needs, and take control of your documents with automation!
