Skip to content

Basics

Run your first Apps Script function

Open Extensions > Apps Script from a Google Sheet or Doc to reach the code editor. Replace the starter function myFunction() { }, save with Ctrl+S, press Run, and read what Logger.log printed.

1 of 7

Where the editor is (Extensions > Apps Script)

Apps Script is a place to write small programs that work with your Google files. There are two doors into it.

From a spreadsheet, open the Extensions menu and choose Apps Script. A new browser tab opens with an editor and a file called Code.gs. The same menu item exists in Google Docs and Slides. In Google Forms, open More (the three-dot menu) and choose Script editor.

You can also go to script.google.com and click New project. That gives you an editor that is not attached to any file. Both doors lead to the same editor.

  • The panel on the left lists Editor, Triggers, Executions, and Project Settings.
  • A file that ends in .gs holds your code. Code.gs is created for you.
  • The name in the top left is the project name. Click it to rename the project.

2 of 7

The starter function myFunction()

A new project already contains this text: function myFunction() { }. That is a function, which means a named block of instructions. The name is myFunction and the curly braces hold the instructions, so right now it does nothing.

You are free to rename it. Change the word myFunction to a name that says what the code does, keep the parentheses and the braces, and add lines between the braces. A name has no spaces and starts with a letter.

Select the starter line and paste this over it. It reads the clock and prints one line for you to see.

Replace myFunction with this
function logGreeting() {
  const now = new Date();
  const readableDate = Utilities.formatDate(now, Session.getScriptTimeZone(), 'yyyy-MM-dd HH:mm');
  Logger.log('Hello from Apps Script. The time is ' + readableDate + '.');
}

3 of 7

Save before you run

Apps Script does not save your typing on its own. Press Ctrl+S, or Cmd+S on a Mac, or click the floppy disk icon in the toolbar above the code.

The editor runs the last saved version of your code, not the text on your screen. If you change a line and press Run without saving, the old version runs and you get a result that does not match what you are reading.

When there are unsaved changes the project name area shows an asterisk and the disk icon is active. After a save, both settle down.

4 of 7

Choose the function, then press Run

Above the code there is a dropdown that lists every function in the file. Apps Script runs the one shown there, so pick logGreeting.

Click Run. If the project has more than one function and the wrong name is showing, you will run the wrong code, so check the dropdown every time.

Debug sits next to Run. It runs the same function but pauses at breakpoints you set. You do not need it yet.

  • Helper functions you only call from other code are listed here too, so read the name before you press Run.
  • Nothing outside the editor changes until your code tells a Google service to change something.

5 of 7

The first time: an authorization dialog

The first run of a project that touches your Google data opens a window that says "Authorization required". Google is asking whether this script may act on your account. You pick your account, read the list of access it wants, and click Allow. Along the way you may see a screen that says "Google hasn't verified this app", because your own new project has not been through Google review. The way through is Advanced, then "Go to <project name> (unsafe)". The permissions basics page walks through every screen and explains how to take the access back later.

6 of 7

Read the Execution log

When you press Run, a panel called Execution log opens at the bottom of the editor. It shows a line saying the function started, then every line your code printed with Logger.log, then a line saying it finished.

For logGreeting you should see one line with the greeting and the current date and time. The date uses the time zone set in Project Settings, so it may differ from your own clock if that setting is wrong.

The log clears when you run again. To see older runs, click Executions in the left sidebar, which keeps a history of every run.

7 of 7

When the log says "Exception"

A line starting with "Exception" means your code stopped part way through. The rest of that line says why, and the part in brackets says where, for example "at logGreeting(Code:3:3)", which is file Code.gs, line 3.

Read the message literally. "TypeError: Cannot read properties of null" means something you asked for was not there. "ReferenceError: Logger2 is not defined" means a name is misspelled. "Exception: You do not have permission to call ..." means the run needs access you have not approved yet.

Fix one thing, save, and run again.

  • Nothing partial is undone. If the code changed a sheet before it failed, that change stays.
  • Copy the message into a search engine with the words "apps script" for a quick explanation.
  • A run that takes longer than six minutes, at the time of writing, is stopped by Google and logged as an exceeded maximum execution time error.

Remember

The short version

  • Extensions > Apps Script opens the editor from Sheets, Docs, and Slides; in Forms, use More (the three-dot menu) > Script editor; script.google.com opens it on its own.
  • Save with Ctrl+S before every run, because the editor runs the last saved version.
  • Check the function name in the toolbar dropdown before pressing Run.
  • Logger.log prints to the Execution log panel, which is where you find out what happened.
  • A line starting with Exception names the reason and the line number, so read it before changing anything.
LoggerUtilitiesSession

Keep going

Put it to work

All basics