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.