Top 30 Useful Google Apps Script Snippets

Oct 19, 2020 · 3 min read

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

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')



Generate UUID

const uuid = Utilities.getUuid()


Execute a function after 1 minute

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.


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


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


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 Developers Experts

Experts on various Google products talking tech.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store