GSP1384
Overview
In this lab, you learn how to securely connect Google Sheets to Agent Platform API. You use Google Apps Script as a bridge, allowing your spreadsheet to communicate directly with Google Cloud services.
Rather than relying on hardcoded API keys, this integration uses user-based OAuth 2.0 authentication. This secures your workflow by running queries using the credentials of the active user. By the end of this lab, you create a custom spreadsheet function to query Gemini models directly from cells. This approach is ideal for:
- Enriching sheets with AI-generated text
- Summarizing datasets
- Performing sentiment analysis
This approach requires no complex backend servers or pipelines. It is a great low-code solution for prototyping AI workflows in Google Workspace.
Objectives
By completing this lab, you will learn how to:
- Enable Google Cloud APIs for a project.
- Connect a Google Apps Script project to a Google Cloud project.
- Authenticate to Google Cloud services from Apps Script using OAuth 2.0.
- Call the Agent Platform API using UrlFetchApp and an access token.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
- Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
-
Click Activate Cloud Shell
at the top of the Google Cloud console.
-
Click through the following windows:
- Continue through the Cloud Shell information window.
- Authorize Cloud Shell to use your credentials to make Google Cloud API calls.
When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:
Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
gcloud auth list
- Click Authorize.
Output:
ACTIVE: *
ACCOUNT: {{{user_0.username | "ACCOUNT"}}}
To set the active account, run:
$ gcloud config set account `ACCOUNT`
- (Optional) You can list the project ID with this command:
gcloud config list project
Output:
[core]
project = {{{project_0.project_id | "PROJECT_ID"}}}
Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
Real-world scenario
Imagine you are a customer support manager at Cymbal Retail. You have a Google Sheet containing hundreds of customer feedback entries. Manually reading and summarizing each entry to identify urgent issues is time-consuming. By connecting Google Sheets to Agent Platform using Apps Script, you can create a custom formula. This formula automatically summarizes feedback and extracts key sentiments. This saves hours of manual work and helps your team respond to critical issues faster.
Task 1. Enable the Agent Platform API
To start building the feedback analysis workflow for Cymbal Retail, you enable the Agent Platform API. This allows your Google Cloud project to access Gemini models.
- Right-click the enable the Agent Platform API link and choose Open Link in Incognito Window.
- Accept terms and conditions. Click Agree and Continue.
- Click Select a project.
- Select the project matching the provided Project ID.
- Click Next to confirm project
- Click Enable
Task 2. Create a Google Sheet and Apps Script
To hold the customer feedback data, you create a new Google Sheet. You then open its bound Apps Script editor to build the AI connection.
- Open Google Sheets, click Blank spreadsheet.
- Name the spreadsheet Agent Platform Caller.
- Select the menu item Extensions > Apps Script.
- Name the Apps Script project Agent Platform Caller.
- Click Project Settings (gear icon ⚙️) in the navigation pane.
- Check the box that says Show “appsscript.json” manifest file in editor.
- In the left sidebar, click Editor.
- Next to Services, click + (Add a service).
- Scroll down and select Agent Platform API.
- Set Identifier to AgentPlatform.
Task 3. Call the Agent Platform API
To automate the feedback analysis, you configure the Apps Script manifest with the required OAuth scopes. You then add the script code to send customer comments to the Agent Platform API and retrieve summaries.
- Return to the Editor ( <> icon).
- In the file
appsscript.json, replace with the following JSON content to add the required oauthScopes.
{
"timeZone": "America/New_York",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "AgentPlatform",
"serviceId": "aiplatform",
"version": "v1"
}
]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets.currentonly",
"https://www.googleapis.com/auth/cloud-platform"
]
}
The cloud-platform scope allows your script to generate an access token for Google Cloud services like Agent Platform.
- In the file
Code.gs, replace the default myFunction with the following code.
// --- Configuration ---
const PROJECT_ID = 'YOUR_GCP_PROJECT_ID'; // ⚠️ IMPORTANT: Replace with your GCP Project ID
const LOCATION = 'global';
const MODEL_ID = 'gemini-2.5-flash';
/**
* Creates a custom menu in Google Sheets when the spreadsheet opens.
*/
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Agent Platform')
.addItem('Summarize Selected Feedback', 'summarizeSelectedFeedback')
.addToUi();
}
/**
* Reads prompt from active cell, calls Vertex AI via Advanced Service,
* and writes the result to the adjacent cell to the right.
*/
function summarizeSelectedFeedback() {
const sheet = SpreadsheetApp.getActiveSheet();
const activeCell = sheet.getActiveCell();
const prompt = activeCell.getValue();
if (!prompt || prompt.toString().trim() === "") {
SpreadsheetApp.getUi().alert('Please select a cell with text first.');
return;
}
if (PROJECT_ID === 'YOUR_GCP_PROJECT_ID' || !PROJECT_ID) {
SpreadsheetApp.getUi().alert('Please update the PROJECT_ID constant in the Apps Script editor with your Google Cloud Project ID.');
return;
}
const resourceName = `projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}`;
const requestBody = {
contents: [
{
role: "user",
parts: [{ text: `Summarize the following customer feedback in 1 sentence: ${prompt}` }]
}
],
generationConfig: {
temperature: 0.7
}
};
try {
const response = AgentPlatform.Projects.Locations.Publishers.Models.generateContent(
requestBody,
resourceName
);
const generatedText = response.candidates?.[0]?.content?.parts?.[0]?.text || "No response generated.";
// Output the generated summary to the cell immediately to the right (Column B)
sheet.getRange(activeCell.getRow(), activeCell.getColumn() + 1).setValue(generatedText);
} catch (error) {
console.error("Agent Platform Error:", error);
SpreadsheetApp.getUi().alert(`Error: ${error.message || error}`);
}
}
- In the script, find the line
const PROJECT_ID = 'YOUR_GCP_PROJECT_ID'; and replace YOUR_GCP_PROJECT_ID with the actual Project ID from your lab.
- Click Save project to Drive project (disk icon 💾).
- In the function dropdown, verify onOpen is selected.
- Click Run.
- Authorization Required: The first time you run this, Apps Script asks for permission to connect to an external service.
- Click Review permissions.
- Choose your student account.
- Click Select all.
- Click Continue.
Task 4. Test the Custom Function
To verify the setup, you test the custom function on a sample prompt in your Google Sheets. This simulates how you automatically summarize Cymbal Retail customer feedback.
- Switch back to your Agent Platform Caller Google Sheet.
- Refresh the browser tab. Notice the new Agent Platform menu appears in the top menu bar.
- In cell A1, type a prompt, for example:
The delivery arrived two days late and the packaging was damaged, but the product itself worked well.
- Click on cell A1 to select it.
- In the top menu, click Agent Platform > Summarize Selected Feedback.
- Cell B1 will be populated with the summary generated by Gemini on Agent Platform.
Congratulations!
You've successfully completed the Agent Platform lab using user-based authentication.
You've successfully integrated Agent Platform with Google Sheets using Apps Script. This enables you to build custom, AI-powered workflows directly within your spreadsheets for tasks like summarization and sentiment analysis.
Next steps / Learn more
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated: August 10, 2026
Lab Last Tested: August 10, 2026
Copyright 2026 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.