apps-script-samples Documentation

Apps Script samples for Google Workspace products.

RAW Doc

File: README.md

Google Apps Script Samples

Various sample code and projects for the Google Apps Script platform, a JavaScript platform in the cloud.

Learn more at developers.google.com.

Google APIs

AdminSDK

Advanced Services

Calendar

Classroom

Data Studio

Docs

Drive

Forms

Gmail

People

Sheets

Slides

Tasks

Templates

  • Build off a working framework for new Apps Script projects.

Triggers

  • Call an Apps Script function such as onOpen, onEdit, or onInstall in an add-on
  • Create a time-driven trigger

Codelabs

Codelab tutorials combine detailed explanation, coding exercises, and documented best practices to help engineers get up to speed with key Google technologies. Here's a list of Apps Script codelabs:

Clone using the `clasp` command-line tool

Learn how to clone, pull, and push Apps Script projects on the command-line
using clasp.

Lint

Run ESLint over this whole repository with:

shell
pnpm lint

This command will fix simple errors.

Type Checking

Run the TypeScript-based check over the repository with:

shell
pnpm check

This command validates .gs files by temporarily converting them to .js and running tsc. It checks for syntax errors and type issues using JSDoc annotations.


File: advanced/README.md

Advanced Services Samples

This directory contains samples for using Apps Script Advanced Services.

Note: These services must be enabled before running these samples.

Learn more at developers.google.com.


File: ai/standup-chat-app/README.md

Chat API - Stand up with AI

Project Description

Google Chat application that creates AI summaries of a consolidation Chat threads and posts them back within the top-level Chat message. Use case is using AI to streamline Stand up content within Google Chat.

Prerequisites

  • Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled

Set up your environment

  1. Create a Cloud Project
    1. Configure OAuth consent screen
    2. Enable the Admin SDK API
    3. Enable the Generative Language API
    4. Enable and configure the Google Chat API with the following values:
      1. App status: Live - available to users
      2. App name: โ€œStandupโ€
      3. Avatar URL: โ€œhttps://www.gstatic.com/images/branding/productlogos/chat_2020q4/v8/web-24dp/logo_chat_2020q4_color_2x_web_24dp.pngโ€
      4. Description: โ€œStandup Appโ€
      5. Enable Interactive features: Disabled
  2. Create a Google Gemini API Key
    1. Navigate to https://aistudio.google.com/app/apikey
    2. Create API key for existing project from step 1
    3. Copy the generated key
  3. Create and open a standalone Apps Script project
    1. From Project Settings, change project to GCP project number of Cloud Project from step 1
    2. Add the following script properties:
      1. Set API_KEY with the API key previously generated as the value.
      2. Set SPREADSHEET_ID with the file ID of a blank spreadsheet.
      3. Set SPACE_NAME to the resource name of a Chat space (e.g. spaces/AAAXYZ)
    3. Enable the Google Chat advanced service
    4. Enable the AdminDirectory advanced service
  4. Add the project code to Apps Script
  5. Enable triggers:
    1. Add Time-driven to run function standup at the desired interval frequency (e.g. Week timer)
    2. Add Time-driven to run function summarize at the desired interval frequency (e.g. Hour timer)

File: ai/gmail-sentiment-analysis/README.md

Gmail sentiment analysis with Vertex AI

Project Description

Google Workspace Add-on that extends Gmail and adds sentiment analysis capabilities.

Prerequisites

  • Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled

Set up your environment

  1. Create a Cloud Project
    1. Enable the Vertex AI API
    2. Create a Service Account and grant the role Vertex AI User
    3. Create a private key with type JSON. This will download the JSON file for use in the next section.
  2. Open an Apps Script Project bound to a Google Sheets Spreadsheet
    1. From Project Settings, change project to GCP project number of Cloud Project from step 1
    2. Add a Script Property. Enter service_account_key as the property name and paste the JSON key from the service account as the value.
  3. Add OAuth2 v43 Apps Script Library using the ID 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF.
  4. Add the project code to Apps Script

Usage

  1. Create a label in Gmail with this exact text and emojy (case sensitive!): UPSET TONE ๐Ÿ˜ก
  2. In Gmail, click on the Productivity toolbox icon (icon of a spy) in the sidepanel.
  3. The sidepanel will open up. Grant the Add-on autorization to run.
  4. The Add-on will load. Click on the blue button "Identify angry customers."
  5. Close the Add-on by clicking on the X in the top right corner.
  6. It can take a couple of minutes until the label is applied to the messages that have a negative tone.
  7. If you don't want to wait until the labels are added, you can refresh the browser.

File: ai/drive-rename/README.md

Google Workspace Add-on Drive - Name with Intelligence

Project Description

Google Workspace Add-on for Google Drive, which uses AI to recommend new names for the selected Doc in Google Drive by passing the body of the document within the AI prompt for context.

Prerequisites

  • Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled

Set up your environment

  1. Create a Cloud Project
    1. Enable the Vertex AI API
    2. Enable Google Drive API
    3. Configure OAuth consent screen
    4. Create a Service Account and grant the role Service Vertex AI User role
    5. Create a private key with type JSON. This will download the JSON file for use in the next section.
  2. Open a standalone Apps Script project.
    1. From Project Settings, change project to GCP project number of Cloud Project from step 1
    2. Add a Script Property. Enter model_id as the property name and gemini-pro as the value.
    3. Add a Script Property. Enter project_location as the property name and us-central1 as the value.
    4. Add a Script Property. Enter service_account_key as the property name and paste the JSON key from the service account as the value.
  3. Add Google Drive API v3 advanced service.
  4. Add OAuth2 v43 Apps Script Library using the ID 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF.
  5. Add the project code to Apps Script

Project Description

A Google Workspace Add-on that creates custom link previews for pages on the Google developer documentation site. The link preview uses AI to generate page summaries.

Prerequisites

  • Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled

Set up your environment

  1. Create a Cloud Project
    1. Enable the Vertex AI API
    2. Create a Service Account and grant the role Vertex AI User
    3. Create a private key with type JSON. This will download the JSON file for use in the next section.
  2. Open a stand alone Apps Script Project
    1. From Project Settings, change project to GCP project number of Cloud Project from step 1
    2. Add a Script Property. Enter service_account_key as the property name and paste the JSON key from the service account as the value.
  3. Add OAuth2 v43 Apps Script Library using the ID 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF.
  4. Add the project code to Apps Script

File: ai/custom-func-ai-studio/README.md

Google Sheets Custom Function with AI Studio

Project Description

Google Sheets Custom Function to be used as a bound Apps Script project with a Google Sheets Spreadsheet

Prerequisites

  • Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled

Set up your environment

  1. Create a Cloud Project
    1. Enable Generative Language API - (may skip as is automatically done in step 2)
  2. Create a Google Gemini API Key
    1. Navigate to https://aistudio.google.com/app/apikey
    2. Create API key for existing project from step 1
    3. Copy the generated key for use in the next step.
  3. Open an Apps Script Project bound to a Google Sheets Spreadsheet
    1. From Project Settings, change project to GCP project number of Cloud Project from step 1
    2. Add a Script Property. Enter api_key as the property name and use the Gemini API Key as the value
  4. Add the project code to Apps Script

Usage

Insert a custom function in Google Sheets, passing a range and a prompt as parameters

Example:

text
=gemini(A1:A10,"Extract colors from the product description")

File: ai/custom_func_vertex/README.md

Google Sheets Custom Function with AI Studio

Project Description

Google Sheets Custom Function to be used as a bound Apps Script project with a Google Sheets Spreadsheet.

Prerequisites

  • Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled

Set up your environment

  1. Create a Cloud Project
    1. Enable the Vertex AI API
    2. Create a Service Account and grant the role Vertex AI User
    3. Create a private key with type JSON. This will download the JSON file for use in the next section.
  2. Open an Apps Script Project bound to a Google Sheets Spreadsheet
    1. From Project Settings, change project to GCP project number of Cloud Project from step 1
    2. Add a Script Property. Enter model_id as the property name and gemini-pro as the value.
    3. Add a Script Property. Enter project_location as the property name and us-central1 as the value.
    4. Add a Script Property. Enter service_account_key as the property name and paste the JSON key from the service account as the value.
  3. Add OAuth2 v43 Apps Script Library using the ID 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF.
  4. Add the project code to Apps Script

Usage

Insert a custom function in Google Sheets, passing a range and a prompt as parameters

Example:

text
=gemini(A1:A10,"Extract colors from the product description")

File: ai/autosummarize/README.md

Editor Add-on: Sheets - AutoSummarize AI

Project Description

Google Workspace Editor Add-on for Google Sheets that uses AI to create AI summaries in bulk for a listing of Google Docs and Slides files.

Prerequisites

  • Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled

Set up your environment

  1. Create a Cloud Project
    1. Enable the Vertex AI API
    2. Create a Service Account and grant the role Service Account Token Creator Role
    3. Create a private key with type JSON. This will download the JSON file for use in the next section.
  2. Open an Apps Script Project bound to a Google Sheets Spreadsheet.
    1. Rename the script to Autosummarize AI.
    2. From Project Settings, change project to GCP project number of Cloud Project from step 1
    3. Add a Script Property. Enter model_id as the property name and gemini-pro-vision as the value.
    4. Add a Script Property. Enter project_location as the property name and us-central1 as the value.
    5. Add a Script Property. Enter service_account_key as the property name and paste the JSON key from the service account as the value.
  3. Add OAuth2 v43 Apps Script Library using the ID 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF.
  4. Enable the Drive v3 advanced service.
  5. Add the project code to Apps Script

Usage

  1. Insert one or more links to any Google Doc or Slides files in a column.
  2. Select one or more of the links in the sheet.
  3. From the Sheets menu, select Extensions > AutoSummarize AI > Open AutoSummarize AI
  4. Click Get summaries button.

File: chat/quickstart/README.md

Google Chat Apps Script Quickstart

Complete the steps described in the quickstart instructions,
and in about five minutes you'll have a simple Apps Script application
that makes requests to the Google Chat API.

Run

After following the quickstart setup instructions, execute the function listSpaces
from the Apps Script console.