AI Apps Script Code Reviewer Issue #24

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.