https://github.com/lsvekis/Google-Apps-Script-Gemini-Projects
๐ Apps Script + Gemini Mastery โ Issue #24
AI Apps Script Code Reviewer
Automatically review, explain, optimize, and improve Google Apps Script projects using Gemini.
This project acts like an AI code reviewer that helps developers find bugs, improve performance, strengthen security, and learn best practices.
โญ What You Will Build
In this issue, you’ll build an AI Code Reviewer that can:
โ Review Apps Script code
๐ Identify bugs and potential issues
โก Suggest performance improvements
๐ Highlight security concerns
๐ Explain unfamiliar code
โจ Recommend best practices
๐ Generate an optimization report
๐ง Why This Project Matters
Every developer benefits from a second set of eyes.
Instead of wondering:
“Is this code any good?”
You can ask AI.
Example:
Review this Apps Script project and suggest improvements.
Within seconds you’ll receive:
- code quality feedback
- optimization ideas
- bug detection
- readability improvements
- security recommendations
๐งฉ Architecture
Apps Script code
โ
Apps Script reads project files
โ
Gemini reviews code
โ
Apps Script creates a Google Doc report
๐งฑ Step 1 โ Menu
Code.gs
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("AI Tools")
.addItem("AI Code Reviewer","showReviewerSidebar")
.addToUi()
}
function showReviewerSidebar(){
SpreadsheetApp.getUi().showSidebar(
HtmlService.createHtmlOutputFromFile("Sidebar")
.setTitle("AI Code Reviewer")
)
}
๐งฑ Step 2 โ Sidebar
Sidebar.html
<div style="font-family:Arial;padding:14px;">
<h2>AI Apps Script Code Reviewer</h2>
<label><b>Paste Apps Script code</b></label>
<textarea
id="code"
style="width:100%;height:220px;">
Paste your Apps Script code here...
</textarea>
<button onclick="review()">
Review Code
</button>
<pre id="output"></pre>
<script>
function review(){
document.getElementById("output").textContent =
"Reviewing..."
google.script.run
.withSuccessHandler(res=>{
document.getElementById("output").textContent =
res
})
.reviewCode(
document.getElementById("code").value
)
}
</script>
</div>
๐งฑ Step 3 โ Ask Gemini
CodeReviewer.gs
function reviewCode(code){
if(!code) return "Paste some Apps Script."
const prompt = `
You are a senior Google Apps Script reviewer.
Review this code.
Return JSON:
{
"summary":"",
"strengths":[...],
"bugs":[...],
"performance":[...],
"security":[...],
"bestPractices":[...],
"refactoredCode":""
}
Code:
${code}
`
let result
try{
result = callGemini(prompt,"")
}catch(e){
return "Gemini error: "+e
}
result = result
.replace(/```json/g,"")
.replace(/```/g,"")
const review = JSON.parse(result)
return createReviewDoc_(review)
}
๐งฑ Step 4 โ Create Report
ReviewWriter.gs
function createReviewDoc_(review){
const doc =
DocumentApp.create(
"Apps Script Code Review"
)
const body = doc.getBody()
body.appendParagraph("Summary")
.setHeading(
DocumentApp.ParagraphHeading.HEADING1
)
body.appendParagraph(review.summary)
body.appendParagraph("Strengths")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
)
review.strengths.forEach(item=>{
body.appendListItem(item)
})
body.appendParagraph("Potential Bugs")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
)
review.bugs.forEach(item=>{
body.appendListItem(item)
})
body.appendParagraph("Performance")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
)
review.performance.forEach(item=>{
body.appendListItem(item)
})
body.appendParagraph("Security")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
)
review.security.forEach(item=>{
body.appendListItem(item)
})
body.appendParagraph("Best Practices")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
)
review.bestPractices.forEach(item=>{
body.appendListItem(item)
})
body.appendParagraph("Suggested Refactor")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
)
body.appendParagraph(
review.refactoredCode
)
return doc.getUrl()
}
๐งฑ Step 5 โ Gemini Helper
Reuse your existing helper.
const GEMINI_API_KEY =
"YOUR_API_KEY"
const GEMINI_MODEL =
"gemini-2.5-flash"
๐งช Example Reviews
Review:
function test(){
SpreadsheetApp.getActive()
}
or
function onEdit(e){
if(e.range.getColumn()==3){
MailApp.sendEmail(...)
}
}
๐ Example Report
Summary
Overall code quality is good but several improvements are recommended.
Strengths
โข Simple structure
โข Good naming
โข Easy to follow
Potential Bugs
โข Missing null checks
โข Event object not validated
Performance
โข Cache Spreadsheet object
โข Reduce API calls
Security
โข Avoid hardcoded IDs
โข Validate user input
Best Practices
โข Use constants
โข Split functions
โข Add comments
Suggested Refactor
(Improved Apps Script implementation)
๐ฅ Advanced Exercises
โ Review multiple project files
โ Compare two versions
โ Score overall code quality
โ Generate documentation automatically
โ Explain code for beginners
๐ Next Issue (#25)
AI Apps Script Documentation Generator
Automatically generate:
- README files
- inline documentation
- function descriptions
- installation guides
- API documentation
from existing Apps Script projects.
This will be an excellent addition for developers who want to improve maintainability, onboarding, and GitHub-ready documentation.