Apps Script and Gemini AI Apps Script Debugging Assistant

πŸš€ 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.