← Google Apps Script

Basic · B1

Meet Apps Script

Apps Script lets you write small programs that control Google Sheets, Docs, Gmail, Drive and more. In this chapter you meet the editor, run your first function and learn why Google asks for permission.

What is Apps Script?

Google Apps Script is JavaScript that runs on Google's servers. It comes with built-in objects for each Google app, so one line can write a cell, send an email or make a calendar event. You don't install anything: the editor is in your browser and the code runs in Google's cloud.

📊 Sheets

Read and write cells, clean data, add menus and your own formulas.

📄 Docs

Make reports, fill templates and find and replace text.

✉️ Gmail

Send personal emails to a whole class from a list.

📁 Drive

Create folders, copy files and list what is in a folder.

📅 Calendar

Add events from a sheet of dates.

🌐 Web apps

Publish a page or a small API with its own URL.

Two kinds of project

KindHow you make itUse it when
Bound Open a Sheet, Doc, Form or Slides file → Extensions → Apps Script The script belongs to one file: a menu in that Sheet, a button in that Doc.
Standalone Go to script.google.com → New project The script works with many files, or is a web app.

A bound script can use getActiveSpreadsheet() or getActiveDocument() to mean "the file I live in". A standalone script has no such file, so it opens files by ID or creates new ones.

Which account?

Use a Google account where you are allowed to run scripts. Some school accounts block Gmail sending or public web apps. If a later chapter fails with "access denied", try a personal account.

Next: the editor →

A tour of the editor

Open a new Google Sheet, then choose Extensions → Apps Script. A new tab opens with:

PartWhat it does
Project name (top left)Click "Untitled project" to rename it. Give every project a real name.
Files listCode.gs holds your script. The + adds more script files or HTML files.
Libraries and ServicesAdd other people's code, or advanced Google APIs. You need these later.
Toolbar💾 Save, ▶ Run, Debug, and the function dropdown that chooses which function runs.
Execution logOpens under the code when you run. Your console.log messages and errors appear here.
Left rail iconsOverview, Editor, Triggers (⏰), Executions (history of runs) and Project settings (⚙).

Code lives in functions

A function is a named block of code. The editor only runs functions, never loose lines, so every script you write in this course is a function. You choose which one runs from the dropdown.

Code.gs
function greet() {
  const name = 'Ploy';
  console.log('Hello, ' + name);
}

function today() {
  const now = new Date();
  console.log('Today is ' + now.toDateString());
}

Execution log (greet selected)

Hello, Ploy

Choose today in the dropdown and press Run again, and you get today's date instead.

Keyboard shortcuts worth knowing

  • Ctrl + S save · Ctrl + R run the selected function
  • Ctrl + / comment or uncomment a line
  • Ctrl + Space autocomplete: type SpreadsheetApp. and wait for the list
  • Shift + Alt + F format the whole file (on a Mac, Cmd instead of Ctrl)

Next: your first script →

Step 1: print a message

  1. Delete the empty myFunction that the editor gives you.
  2. Paste the code below and press Ctrl + S.
  3. Check the dropdown shows hello, then press ▶ Run.
Code.gs
function hello() {
  console.log('Hello, Apps Script!');
}

Execution log

10:15:02 AM   Notice   Execution started
10:15:02 AM   Info     Hello, Apps Script!
10:15:03 AM   Notice   Execution completed

console.log writes a line into the log. You will use it all the time to check what your code is doing. The older Logger.log also works, but it prints numbers as 5.0, so this course uses console.log.

Step 2: write into the sheet

Now the script changes the Sheet it is bound to. Add this function under hello, save, select writeHello and run.

Code.gs
function writeHello() {
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange('A1').setValue('Hello from Apps Script');
  sheet.getRange('A2').setValue(new Date());
}

Back in the Sheet

A1  Hello from Apps Script
A2  (today's date and time)

Read it from left to right, the way the computer does:

  • SpreadsheetApp is the Sheets service.
  • .getActiveSheet() picks the tab that is open.
  • .getRange('A1') picks one cell, and .setValue(…) puts something in it.

Each dot asks the thing on its left for something new. This "chain" style is how all of Apps Script reads.

Step 3: make a whole new file

Code.gs
function makeDoc() {
  const doc = DocumentApp.create('My first scripted doc');
  doc.getBody().appendParagraph('This document was made by a script.');
  console.log(doc.getUrl());
}

Execution log

https://docs.google.com/document/d/1AbC…/edit

Open the link from the log. The new document is in your Drive with the sentence already typed in. A script made it in about a second.

Next: the permission screen →

"Authorization required"

The first time a script touches your data, Google stops and asks. A script runs as you, with your access to your files and email, so you have to agree first. You see this once per project, and again if you add code that needs something new (Gmail, for example).

  1. Click Review permissions and choose your account.
  2. For your own script you may see "Google hasn't verified this app". Click Advanced, then Go to (your project name) (unsafe). "Unsafe" only means Google hasn't reviewed it. You wrote it, so you know what it does.
  3. Read the list of what the script can do, then click Allow. The script runs.

Only allow code you understand

Never authorize a script someone sent you unless you have read it. A script that can "send email as you" or "see, edit, create and delete all your Google Drive files" really can. That is the power you are learning to use, and it is also how scams work.

Scopes: what the list means

Each line on the screen is a scope, a permission. Apps Script works them out by reading your code:

Your code usesGoogle asks to…
SpreadsheetAppSee, edit, create and delete your spreadsheets
DocumentAppSee, edit, create and delete your documents
GmailAppRead, compose, send and delete your email
MailAppSend email as you (a smaller permission than GmailApp)
UrlFetchAppConnect to an external service

To remove a script's access later, go to your Google Account → Security → Your connections to third-party apps & services.

Next: practice →

Practice

Do these in a Sheet-bound project. Try each one before you open the solution.

1. Three lines about you Easy

Write a function aboutMe that prints three lines to the log: your name, your class and your favourite app.

Show a solution
Code.gs
function aboutMe() {
  console.log('My name is Ploy');
  console.log('I am in M.5');
  console.log('My favourite app is Google Sheets');
}

2. Two cells Easy

Write writeName so that it puts your name in B2 and your class in C2.

Show a solution
Code.gs
function writeName() {
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange('B2').setValue('Ploy');
  sheet.getRange('C2').setValue('M.5/3');
}

3. A pop-up message Explore

Spreadsheets have a toast method that shows a small message in the corner of the Sheet. Use autocomplete on SpreadsheetApp.getActiveSpreadsheet(). to find it, and show "Hi from my script!" for 5 seconds.

Show a solution
Code.gs
function sayHi() {
  SpreadsheetApp.getActiveSpreadsheet().toast('Hi from my script!', 'Apps Script', 5);
}

4. Break it on purpose Think

In writeHello, change getRange to getrange (small r) and run it. Read the error in the log. Which line does it name? Fix it again. Reading errors calmly is half of programming.

Next: quiz →

Check yourself

1. Where does Apps Script code run?
2. Which language is Apps Script based on?
3. You open a Sheet and choose Extensions → Apps Script. What kind of project is that?
4. Your project has three functions. How does the editor know which one to run?
5. Why does Google show "Authorization required" the first time?
6. Where do console.log messages appear?