📘 Lesson 7 — Gemini-Powered Google Docs (Summaries, Rewrites & AI Writing Tools)
Blog Post #7 in the Apps Script + APIs + Gemini Series
Google Sheets is about data.
Google Docs is about thinking, writing, and meaning.
In this lesson, you’ll bring Gemini directly into Google Docs, allowing users to summarize documents, rewrite sections, and generate outlines — all without leaving the document.
This is the same architecture used for:
- AI writing assistants
- Learning & tutoring tools
- Policy summarizers
- Content editors
- Research helpers
🎯 What You’ll Build
By the end of this lesson, you’ll have:
✅ A custom Docs menu
✅ The ability to read document text
✅ Gemini-powered summaries and rewrites
✅ AI output inserted directly into the Doc
✅ A reusable pattern for AI writing tools
🧠 Key Concepts Introduced
DocumentAppbasics- Custom menus in Google Docs
- Reading document content safely
- Inserting AI-generated content
- UX patterns for AI writing tools
📍 Step 1 — Add a Custom Menu to Google Docs
This makes Gemini feel like a native feature.
/**
* Adds an AI menu to Google Docs.
*/
function onOpen() {
DocumentApp.getUi()
.createMenu('AI Tools')
.addItem('Summarize Document with Gemini', 'summarizeDocWithGemini')
.addItem('Rewrite Selected Text', 'rewriteSelectionWithGemini')
.addToUi();
}
📌 Reload the Doc to see AI Tools in the menu bar.
📍 Step 2 — Read the Document Content
/**
* Gets the full text of the active document.
*/
function getDocumentText() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
return body.getText();
}
🔍 Why This Matters
Gemini works with text, not document structure.
We extract readable content and pass it as context.
📍 Step 3 — Summarize the Entire Document
/**
* Summarizes the document using Gemini.
*/
function summarizeDocWithGemini() {
try {
var docText = getDocumentText();
if (!docText || docText.trim().length < 20) {
throw new Error('Document is too short to summarize.');
}
var prompt =
'Summarize the following document in clear bullet points:\n\n' +
docText;
var summary = callGemini(prompt);
insertSummaryAtTop(summary);
} catch (err) {
DocumentApp.getUi().alert(err.message);
}
}
📍 Step 4 — Insert the Summary into the Document
/**
* Inserts the summary at the top of the document.
*/
function insertSummaryAtTop(text) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.insertParagraph(0, 'Gemini Summary:')
.setHeading(DocumentApp.ParagraphHeading.HEADING2);
body.insertParagraph(1, text);
}
📍 Step 5 — Rewrite Selected Text with Gemini
This is a power feature.
/**
* Rewrites selected text using Gemini.
*/
function rewriteSelectionWithGemini() {
try {
var selection = DocumentApp.getActiveDocument().getSelection();
if (!selection) {
throw new Error('Please select text to rewrite.');
}
var elements = selection.getRangeElements();
var selectedText = '';
elements.forEach(function(el) {
if (el.getElement().editAsText) {
selectedText += el.getElement().editAsText().getText();
}
});
if (!selectedText.trim()) {
throw new Error('No readable text selected.');
}
var prompt =
'Rewrite the following text to be clearer and more concise:\n\n' +
selectedText;
var rewritten = callGemini(prompt);
insertRewriteBelowSelection(rewritten);
} catch (err) {
DocumentApp.getUi().alert(err.message);
}
}
📍 Step 6 — Insert Rewritten Text Safely
/**
* Inserts rewritten text below the selection.
*/
function insertRewriteBelowSelection(text) {
var doc = DocumentApp.getActiveDocument();
var cursor = doc.getCursor();
if (!cursor) {
throw new Error('Could not determine cursor position.');
}
cursor.insertText('\n\nGemini Rewrite:\n' + text);
}
🧪 Try It Out
- Open a Google Doc with real content
- Reload the document
- Use AI Tools → Summarize Document with Gemini
- Select a paragraph and try Rewrite Selected Text
🎉 You now have an AI writing assistant inside Docs.
🧪 Exercises (Highly Recommended)
Exercise 1 — Tone Control
Change the rewrite prompt to:
- “Rewrite in a professional tone”
- “Rewrite for a beginner audience”
- “Rewrite as bullet points”
Exercise 2 — Outline Generator
Create a new menu item:
“Generate Document Outline”
Prompt Gemini to return headings only.
Exercise 3 — Append AI Output Instead
Instead of inserting at the top:
- Append summaries at the end
- Or insert into a new section
💡 Pro Tips for AI in Docs
Tip 1 — Never Overwrite User Content
Always insert AI output separately.
Tip 2 — Make AI Output Clearly Labeled
Users must know what was generated by AI.
Tip 3 — This Scales to Add-ons
This architecture works for:
- Workspace add-ons
- Shared tools
- Internal documentation systems
🔮 What’s Next?
In Lesson 8, you’ll put everything together:
Lesson 8 — Building a Gemini-Powered AI Dashboard Web App
You’ll build:
- A web UI
- Prompt input
- AI responses
- History logging
- A complete AI application using Apps Script