Imagine you have content in a Google Doc that looks like this:
Exercise 1: Introduction to Functions Learning Objective: Understand how functions work in JavaScript.
Exercise 2: Variables and Scope Learning Objective: Learn about variable declaration and scope in JavaScript.
You want to:
✅ Set each “Exercise” heading to Heading 3.
✅ Move “Learning Objective:” and the following text to a new line as normal text.
💡 The Solution: Google Apps Script
With Google Apps Script, we can automate this task efficiently. The script below loops through all the paragraphs, identifies lines starting with “Exercise”, updates their heading style, and separates any “Learning Objective:” text into a new paragraph.
function updateExerciseAndSplitLearningObjective() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
for (var i = paragraphs.length - 1; i >= 0; i--) {
var paragraph = paragraphs[i];
var text = paragraph.getText();
// Check if the paragraph starts with "Exercise"
if (text.startsWith("Exercise")) {
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
var splitIndex = text.indexOf("Learning Objective:");
if (splitIndex !== -1) {
var beforeText = text.substring(0, splitIndex).trim(); // Exercise part
var afterText = text.substring(splitIndex).trim(); // Learning Objective part
// Update the original paragraph with just the Exercise part
paragraph.setText(beforeText);
// Insert a new paragraph after the current one with the Learning Objective text
var newParagraph = body.insertParagraph(i + 1, afterText);
newParagraph.setHeading(DocumentApp.ParagraphHeading.NORMAL);
}
}
}
Logger.log("Updated all 'Exercise' headings and split 'Learning Objective:' into a new line.");
}
🔍 How the Script Works
1️⃣ Retrieves the active document using DocumentApp.getActiveDocument()
.
2️⃣ Loops through all paragraphs to find lines starting with "Exercise"
.
3️⃣ Updates the paragraph’s heading to Heading 3.
4️⃣ Checks if the paragraph contains "Learning Objective:"
.
5️⃣ Splits the text at "Learning Objective:"
, keeping the first part under Heading 3 and moving the rest into a new paragraph with normal text.
📌 Example Output
Before Running the Script
Exercise 1: Introduction to Functions Learning Objective: Understand how functions work in JavaScript.
Exercise 2: Variables and Scope Learning Objective: Learn about variable declaration and scope in JavaScript.
After Running the Script
Exercise 1: Introduction to Functions (Heading 3)
Learning Objective: Understand how functions work in JavaScript. (Normal Text)
Exercise 2: Variables and Scope (Heading 3)
Learning Objective: Learn about variable declaration and scope in JavaScript. (Normal Text)
🚀 How to Use This Script
1️⃣ Open Google Docs.
2️⃣ Click Extensions > Apps Script.
3️⃣ Delete any existing code and paste the script into the editor.
4️⃣ Click Run ▶️ to execute the script.
🎯 Why Use This Script?
✅ Saves Time – No need to manually format each exercise.
✅ Consistency – Ensures all exercises have the same heading style.
✅ Automation – Works across entire documents instantly.
This script is a great example of how Google Apps Script can be used to streamline document formatting, making your workflow more efficient. Try it out and enhance your productivity with Google Docs automation! 🚀
