Overview
This tutorial guides you through a Google Sheets script that integrates with Contextual AI. The script automates form filling by connecting to Contextual AI’s API, enabling dynamic, real-time population of form fields powered by a Contextual AI RAG agent.
Features
- Use Contextual AI RAG Agents directly from within Google Sheets
- Real-time API integration
Prerequisites
- Access to Google Sheets
- A Contextual AI API key
Tutorial Steps
To get started, make sure you have already set up your Agent. If note, you can follow these directions.
1. Configuring the Sheets Script
Ensure that the documents Vanguard BND Prospectus and iShares Government Bond Prospectus are uploaded to your Agent’s datastore.- Open the example Google Sheet with the script installed.
- Go to
File → Make a copy(so you have your own version). - Go to
Extensions → Apps Script. - Modify the
agent URLandAPI keywith your Agent ID and API key. - Add your form fields in Column A.
- Add your instructions in Column B (hidden by default).
2. Running the Script
- Custom menu buttons are already included to run the scripts.
- Select the three vertical dots on the buttons to modify executed scripts.
- Watch Column C for processing status updates.
- Results appear in real time as they’re fetched.
Code Structure
The script consists of two main functions:1. fetchDataAndPopulateSheet()
Coordinates the data fetching process, manages sheet range selection, and schedules API calls.
2. makeApiCall()
Handles individual API calls to Contextual AI Formats request payload Processes API responsesError Handling
The script includes basic error handling:- API connection issues
- Data formatting problems
- Rate limiting responses
- Invalid sheet ranges
Security Considerations
- Store API keys securely
- Don’t share sheets containing API credentials
- Regularly rotate API keys
- Limit script access to necessary users only