π Apps Script + Gemini Mastery β Issue #29
https://github.com/lsvekis/Apps-Script-Code-Snippets
AI Apps Script Debugging Assistant
Debug Google Apps Script projects faster by combining Gemini AI with automated code analysis, error interpretation, and intelligent fix recommendations.
Instead of spending hours searching Stack Overflow or reading logs, let AI analyze your Apps Script code, explain the root cause, and suggest practical fixes.
β What You Will Build
In this issue, you’ll build an AI Debugging Assistant that can:
π Analyze Apps Script errors
π Detect common coding mistakes
π‘ Explain why an error occurred
π Suggest code fixes
π§ͺ Recommend additional tests
π Generate a debugging report in Google Docs
β‘ Highlight potential improvements before deployment
π§ Why This Project Matters
Debugging is often the most time-consuming part of development.
Instead of asking:
“Why doesn’t this work?”
You can ask:
“What’s causing this error, and how do I fix it?”
AI becomes your debugging partner, helping you understand the problemβnot just patch it.
π§© Architecture
Apps Script Error
β
βΌ
Paste Code + Error Message
β
βΌ
Gemini Analysis
β
βΌ
Structured JSON Response
β
βΌ
Apps Script Generates
β’ Root Cause
β’ Suggested Fixes
β’ Corrected Code
β’ Best Practices
β’ Testing Recommendations
π§± Step 1 β Menu
Code.gs
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("AI Tools")
.addItem(
"AI Debugging Assistant",
"showDebuggerSidebar"
)
.addToUi();
}
function showDebuggerSidebar(){
SpreadsheetApp.getUi().showSidebar(
HtmlService
.createHtmlOutputFromFile("Sidebar")
.setTitle("AI Debugging Assistant")
);
}
π§± Step 2 β Sidebar
Sidebar.html
<div style="font-family:Arial;padding:15px;">
<h2>AI Debugging Assistant</h2>
<label>Error Message</label>
<textarea
id="error"
style="width:100%;height:80px;">
TypeError: Cannot read properties of undefined
</textarea>
<label>Apps Script Code</label>
<textarea
id="code"
style="width:100%;height:220px;">
</textarea>
<button onclick="analyze()">
Analyze Error
</button>
<pre id="output"></pre>
<script>
function analyze(){
output.textContent="Analyzing...";
google.script.run
.withSuccessHandler(r=>{
output.textContent=r;
})
.analyzeError(
error.value,
code.value
);
}
</script>
</div>
π§± Step 3 β Analyze with Gemini
Debugger.gs
function analyzeError(error, code){
const prompt=`
You are a senior Google Apps Script engineer.
Analyze this Apps Script error.
Return JSON only.
{
"summary":"",
"rootCause":"",
"likelyLocation":"",
"fixes":[...],
"bestPractices":[...],
"correctedCode":"",
"testing":[...]
}
Error:
${error}
Code:
${code}
`;
let result=
callGemini(prompt,"");
result=result
.replace(/```json/g,"")
.replace(/```/g,"");
const analysis=
JSON.parse(result);
return createDebugReport_(analysis);
}
π§± Step 4 β Generate Report
DebugReport.gs
function createDebugReport_(analysis){
const doc=
DocumentApp.create(
"Apps Script Debug Report"
);
const body=doc.getBody();
body.appendParagraph("Summary")
.setHeading(
DocumentApp.ParagraphHeading.HEADING1
);
body.appendParagraph(
analysis.summary
);
body.appendParagraph("Root Cause")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
);
body.appendParagraph(
analysis.rootCause
);
body.appendParagraph("Suggested Fixes")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
);
analysis.fixes.forEach(item=>{
body.appendListItem(item);
});
body.appendParagraph("Best Practices")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
);
analysis.bestPractices.forEach(item=>{
body.appendListItem(item);
});
body.appendParagraph("Testing")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
);
analysis.testing.forEach(item=>{
body.appendListItem(item);
});
body.appendParagraph("Corrected Code")
.setHeading(
DocumentApp.ParagraphHeading.HEADING2
);
body.appendParagraph(
analysis.correctedCode
);
return doc.getUrl();
}
π§ͺ Example Input
Error:
TypeError:
Cannot read properties of undefined
Code:
function myFunction(){
const value =
SpreadsheetApp
.getActiveSheet()
.getRange("A1")
.getValue();
Logger.log(
value.name
);
}
π€ Example AI Analysis
Root Cause
The value returned from cell A1 is a primitive value, not an object. Accessing value.name causes the error.
Suggested Fixes
- Check the data type before accessing properties
- Validate that objects exist
- Add defensive programming checks
Corrected Code
if(value){
Logger.log(value);
}
π₯ Advanced Features
Add support for:
β Apps Script execution logs
β Stack trace analysis
β Automatic bug categorization
β Performance bottleneck detection
β Security issue detection
β Suggested refactoring
β Explain errors for beginners
β Generate GitHub issue reports
π What You’ll Learn
By completing this project you’ll learn how to:
- Build AI-assisted debugging tools
- Interpret Apps Script runtime errors
- Create structured AI prompts
- Generate developer-friendly reports
- Improve code quality through automated analysis
This is a practical tool you’ll likely reuse in many future Apps Script projects.
π Next Issue (#30)
AI Apps Script Prompt Builder
Design better prompts for Gemini directly inside Google Workspace.
You’ll build a tool that helps developers:
- generate structured prompts
- optimize prompts for Apps Script tasks
- save reusable prompt templates
- test prompts against sample data
- compare prompt variations
- build a personal prompt library
Instead of writing prompts from scratch every time, you’ll create a reusable AI prompt engineering toolkit for Google Apps Script development.