AI Google Docs Content Refiner Apps Script 18

🚀 Apps Script + Gemini Mastery — Issue #18

https://github.com/lsvekis/Google-Apps-Script-Gemini-Projects

AI Google Docs Content Refiner

Rewrite, summarize, improve, and transform documents automatically using Google Apps Script + Gemini.


⭐ What You Will Build

In this issue, you’ll build an AI Content Refiner for Google Docs that can:

✍️ Rewrite content in different tones
📄 Summarize long documents
🧠 Simplify complex text
💼 Make writing more professional
🎯 Adapt content for different audiences

All directly inside Google Docs.


🧠 Why This Project Matters

Writing is everywhere:

• emails
• reports
• documentation
• proposals
• content creation

But editing and refining takes time.

This tool allows users to simply say:

“Rewrite this to sound more professional”

or

“Summarize this document in 5 bullet points”

And instantly get improved content.


🧩 Architecture

Google Doc content

Apps Script reads text

Gemini transforms content

Apps Script writes output back to the document


🧱 Step 1 — Add the Menu

Code.gs

function onOpen() {
DocumentApp.getUi()
.createMenu("AI Tools")
.addItem("AI Content Refiner", "showRefinerSidebar")
.addToUi();
}function showRefinerSidebar() {
DocumentApp.getUi().showSidebar(
HtmlService.createHtmlOutputFromFile("Sidebar")
.setTitle("AI Content Refiner")
);
}

🧱 Step 2 — Sidebar UI

Sidebar.html

<div style="font-family:Arial;padding:14px;"><h2>AI Content Refiner</h2><label><b>What would you like to do?</b></label><textarea id="prompt" style="width:100%;height:80px;">
Example: Rewrite this to sound more professional
</textarea><button onclick="run()">Refine Document</button><pre id="output" style="white-space:pre-wrap;margin-top:12px;"></pre><script>function run(){document.getElementById("output").textContent="Processing document..."google.script.run
.withSuccessHandler(res=>{
document.getElementById("output").textContent=res
})
.refineDocument(
document.getElementById("prompt").value
)}</script></div>

🧱 Step 3 — Read Document Content

DocReader.gs

function getDocumentText_(){const doc = DocumentApp.getActiveDocument()
const body = doc.getBody()return body.getText()}

🧱 Step 4 — Send to Gemini

Refiner.gs

function refineDocument(userPrompt){if(!userPrompt) return "Enter instructions."const text = getDocumentText_()const prompt = `
You are a writing assistant.Document:
${text.substring(0,12000)}Instruction:
${userPrompt}Return only the transformed content.
`let resulttry{result = callGemini(prompt,"")}catch(e){return "Gemini error: "+e}return writeRefinedContent_(result)}

🧱 Step 5 — Write Output Back

DocWriter.gs

function writeRefinedContent_(content){const doc = DocumentApp.getActiveDocument()
const body = doc.getBody()body.appendParagraph("\n--- AI Refined Content ---\n")body.appendParagraph(content)return "Refined content added to document."}

🧱 Step 6 — Gemini Helper

Reuse from previous issues:

const GEMINI_API_KEY = "YOUR_API_KEY"
const GEMINI_MODEL = "gemini-2.5-flash"

🧪 Example Prompts

Users can request:

Rewrite this to sound more professional
Summarize this document in bullet points
Simplify this for a beginner audience
Turn this into a LinkedIn post
Improve clarity and structure

🔥 Advanced Exercises

Readers can extend this by adding:

✅ tone selection dropdown
✅ summarize vs rewrite toggle
✅ highlight changes
✅ version comparison
✅ export to new document


🔜 Next Issue (#19)

AI Email Generator for Gmail

Generate full emails from prompts and context automatically.

Example:

“Write a follow-up email after a meeting”