As developers, we are constantly looking for ways to improve our workflow and the quality of our code. The recent rise of powerful AI assistants, like Google’s Gemini, has opened up new frontiers for productivity, particularly in the realm of code reviews. These tools can offer instant feedback, suggest optimisations, and spot potential bugs we might have missed.
However, there’s often a practical hurdle: providing the AI with the complete context of our project. For Google Apps Script developers, this can mean the tedious process of copying and pasting code from multiple .gs
and .html
files, not to mention the crucial appsscript.json
manifest.
To solve this, I’ve developed a simple tool built, fittingly, with Apps Script itself. It’s a Google Sheet that lets you export an entire Apps Script project into a single, neatly formatted JSON file, ready to be added directly to the Gemini App or a Gemini Gem.
Why This Approach?
- Holistic Reviews: By packaging every file—including the manifest with its scopes and dependencies—you give the AI the bigger picture leading to more accurate analysis.
- Boosted Efficiency: Forget manual copy-pasting. A couple of clicks are all it takes to get a complete project export, saving you time and preventing errors.
How It Works & How to Prompt Gemini
To get the most out of your exported project file, it helps to understand its structure and how to tell your AI assistant what it’s looking at.
The JSON Structure
The tool uses the official Google Apps Script API to fetch your project’s content. The resulting .json
file contains a list (an array) of File
objects. Based on the official documentation, each object in the list represents one file from your project and looks something like this:
{ "name": "Code", "type": "SERVER_JS", "source": "function myFunction() {\\n Logger.log('Hello, world!');\\n}" }
name
: The name of the file (e.g., “Code”, “Index”, “appsscript”).type
: The kind of file it is. This will beSERVER_JS
for.gs
files,HTML
for.html
files, andJSON
for theappsscript.json
manifest.source
: The actual code or content of the file as a string.
Prompting Gemini for the Best Results
When you upload this file, you can give Gemini a simple instruction to ensure it understands the context. Here is a sample prompt you can adapt:
“Please review the attached Google Apps Script project. The JSON file contains an array of file objects, where each object has a ‘name’, ‘type’, and ‘source’. Please analyse all the files together as a single project to provide a comprehensive review.”
This prompt tells Gemini exactly how to interpret the file, ensuring it sees the appsscript.json
manifest for its scopes, the server-side logic in the .gs
files, and any client-side .html
files as a complete, interconnected project.
Tip: For scripts bound to a Google Sheets, Docs, or Slides, to get the most accurate review, add these documents to your Gemini conversation alongside the exported JSON file to give Gemini the complete context.
Take It to the Next Level: Create a Custom Gem
To save even more time, you can embed these instructions into a custom Gem in the Gemini App. This creates a reusable ‘persona’ for your code reviews. When creating your Gem, you can provide it with more detailed instructions to focus its analysis.
Here is a more advanced set of instructions you could use for your “Apps Script Code Reviewer” Gem:
You are an expert Google Apps Script developer who specialises in writing clean, efficient, and secure code. When I provide a JSON file, it will represent a complete Apps Script project. The file contains an array of file objects, each with a 'name', 'type', and 'source'. Your task is to perform a thorough code review of the entire project. Please analyse all files together. Specifically, focus on: 1. **Best Practices:** Check if the code follows the official Google Apps Script style guide and modern JavaScript (ES6+) conventions. 2. **Performance:** Identify any potential bottlenecks, inefficient loops, or excessive API calls. 3. **Security:** Look for any potential security vulnerabilities, especially concerning data handling and web app permissions. 4. **Clarity and Readability:** Assess the code for clarity, and check for adequate comments and meaningful variable names. Please provide your feedback in a structured format, starting with a high-level summary and then a detailed list of suggestions, grouped by file. If you are suggesting code revisions, provide the fully revised file.
Getting Started
Here’s how to set up your own Apps Script project exporter.
Step 1: Prerequisites – Enabling the Apps Script API
The tool relies on the Apps Script API to fetch project files. To use it, you first need to link your script project to a Google Cloud Platform (GCP) project where the API is enabled.
- Create a GCP Project: If you don’t already have one, create a standard GCP project by visiting the Google Cloud Console.
- Enable the Apps Script API: Within your GCP project, navigate to the “APIs & Services” dashboard, click “+ ENABLE APIS AND SERVICES”, search for “Google Apps Script API”, and enable it.
- Configure the OAuth Consent Screen: Before the script can be authorised, you must configure the consent screen.
- In the GCP Console, navigate to “APIs & Services” > “OAuth consent screen”.
- Choose Internal for the User Type and click Create.
- Fill in the required fields (App name, User support email, and Developer contact information) and click Save and Continue. No other configuration is needed. As this is for internal use, you do not need to submit the app for verification.
- Link to Your Apps Script Project:
- Open your Apps Script project (the one containing the exporter code).
- Go to “Project Settings” (the cog icon ⚙️).
- Under “Google Cloud Platform (GCP) Project”, click “Change project” and enter your GCP project number.
Step 2: Setting Up the Exporter Sheet
- Make a Copy: Click this link to make your own copy of the pre-configured Google Sheet Template.
- Configure the Destination Folder:
- In your new sheet, go to
Extensions > Apps Script
. - In the
Code.gs
file, find the lineconst DEST_FOLDER_ID = '...';
- Replace the placeholder ID with the ID of the Google Drive folder where you want your JSON exports to be saved. You can get this from the folder’s URL (it’s the string of characters after
folders/
). Tip: Your copy of this template can be shared with your colleagues. Also share the folder with them so that the script can write the export file.
- Save and Authorise: Save the script project. Return to your sheet and reload the page. A new “Script Exporter” menu will appear. The first time you run it, you will be prompted to authorise the script’s required permissions.
Step 3: Exporting Your First Project
Using the tool is the easiest part!
- Find the Script ID: First, you need the ID of the project you wish to review. The easiest way to find this is by copying it from the script editor’s URL.
- For standalone scripts: The URL will look like
https://script.google.com/d/THIS_IS_THE_ID/edit
. The ID is the long string of characters between/d/
and/edit
. - For container-bound scripts (in a Sheet, Doc, etc.): From your Google Sheet or Doc, open the script editor via
Extensions > Apps Script
. The URL in this new browser tab will have the same format. Copy the ID from there.
- Run the Exporter: In your exporter sheet, click
Script Exporter > Export Script Project
. - Paste the ID: When the dialog box appears, paste in the Script ID you just copied and click OK.
- Check the Output: The script will fetch the project files, create a JSON file in your designated Drive folder, and confirm with a success message. A log of the export, including a link to the file, will be automatically added to the “Execution Log” sheet.
You can now head over to the Gemini App (gemini.google.com), add the JSON file from Google Drive, and start your comprehensive code review!
I hope you find this tool as useful, it’s a small optimisation, but one that hopefully makes integrating Gemini for Workspace into our daily Apps Script development practices that much smoother. I’d love to hear if you use this or similar approaches and any other tips you have for Gemini assisted Google Workspace development. Happy scripting!