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.