Top 30 Useful Google Apps Script Snippets

Google Developer Experts
3 min readOct 19, 2020


Here are my favorite 30 Google Apps Script snippets. These snippets will save your time.


Access to Spreadsheet ( standalone scripts )

const spreadsheet = SpreadsheetApp.openById("Spreadsheet ID")

Spreadsheet ID can be obtained from the spreadsheet URL[ Spreadsheet ID ]/edit

Access to Spreadsheet ( Container-bound scripts )

const spreadsheet = SpreadsheetApp.getActive()

Get values

const values = spreadsheet
.getSheetByName('sheet name')

Set values


Add custom menu

const onOpen = () => {
.addMenu('Menu name', [
{name: 'sub menu name 1', functionName: 'functionName1'},
{name: 'sub menu name 2', functionName: 'functionName2'},

onEdit trigger

const onEdit = e => {
const { range, oldValue, value } = e

onSelectionChange trigger

const onSelectionChange = e => {
const { range, user } = e

Google Calendar

Create event

CalendarApp.getDefaultCalendar().createEvent(title, start, end)

Get events

const events = CalendarApp
.getEvents(start, end)
.map(event => ({
title: event.getTitle(),
description: event.getDescription(),
start: event.getStartTime(),
end: event.getEndTime()

You can get other properties from CalendarEvent class

Access to not default calendar

const calendar = CalendarApp.getCalendarById('Calendar ID')


Send Email

GmailApp.sendEmail('', 'subject', 'body')

Search threads

If you’d like to get 10 latest threads,

const threads ='', 0, 10)

You can set search operators as the first argument.

Get messages

const messages ='', 0, 10).flatMap(thread => 
thread.getMessages().map(message => ({
subject: message.getSubject(),
body: message.getBody(),
date: message.getDate(),
from: message.getFrom(),
to: message.getTo(),

You can get other properties from GmailMessage class

Google Drive

Get folders and files in the specific folder

const folder = DriveApp.getFolderById('Folder ID')const files = folder.getFiles()
while (files.hasNext()) {
let file =
console.log('file: ', file.getName())
const folders = folder.getFolders()
while (folders.hasNext()) {
let folder =
console.log('folder: ', folder.getName())

Google Form

Get input values

const onSubmit = event => {
const answer = event.response
.map(itemResponse => ({
item: itemResponse.getItem().getTitle(),
response: itemResponse.getResponse()

You need to add form submit trigger.

Google Document

Access to Google Document ( standalone scripts )

const doc = DocumentApp.openById('Document ID')

Document ID can be obtained from the spreadsheet URL[ Document ID ]/edit

Access to Google Document ( Container-bound scripts )

const doc = DocumentApp.getActiveDocument()

Add custom menu

const onOpen = () => {
.createMenu('menu name')
.addItem('item name', 'functionName')


Format date

const date = Utilities.formatDate(new Date(), 'Asia/Tokyo', 'YYYY-MM-dd HH:mm:ss')


If you want the script to sleep for 1 second


Generate UUID

const uuid = Utilities.getUuid()


Execute a function after 1 minute

Google Apps Script has a limitation of script runtime. If you want the script to work more than the limit, you can schedule the next execution in advance.

const date = new Date()
date.setMinutes(date.getMinutes() + 1);// after 1 min


Get property

const value = PropertiesService.getScriptProperties().getProperty(key)

Set property

PropertiesService.getScriptProperties().setProperty(key, value)



const text = LanguageApp.translate('Hello World', 'en', 'ja')

Web Apps

Website ( no HTML template )

const doGet = e => {
const params = JSON.stringify(e.parameter)
return HtmlService.createHtmlOutput(params)

Website ( with HTML template )

const doGet = e => {
return HtmlService.createHtmlOutputFromFile('index')

You need to create index.html file.


You can get query string with accessing e.parameter.

const doGet = e => {
const params = JSON.stringify(e.parameter)
return ContentService


You can get request body with accessing e.postData.contents.

const doPost = e =>  {
const body = e.postData.contents
return ContentService


You can access other resources when using UrlFetchApp class.

GET API request

const content = UrlFetchApp.fetch(url).getContentText()

POST API request

const res = UrlFetchApp.fetch(url, {
method: 'POST',
headers: { "Content-Type": 'application/json' },
payload: JSON.stringify(data)

Thank you for reading this article!



Google Developer Experts

Vice President, RPG TEC. Google Developers Expert / Licensed Scrum Master / Website: