5. Code Your Alert Box to Open Automatically

Playback Speed:

Hi, I’m Julia.

In the previous videos, you wrote a program to make an alert box pop up when someone opens your document.

Then you added HTML formatting to make that message more eye-catching.

Apps Script has changed since these videos were first recorded, so I’m going to show you how to make this program run automatically when you open the document.

Right now, the only way to see the alert is to run the code manually.

A reader would have to open your document, then open the script editor and run the code to show the welcome message.

In this video, you will add code to make the alert box appear automatically, every time someone opens your document.

To do that, you will make an event, also called a trigger.

In computer science, an event is an action that tells code to run.

In this case, you will set up an event that tells the program to run every time your “Research Notes” document is opened.

To create an event, open the Triggers menu.

Add a trigger.

The form shows different options for the event.

Choose to run the function My function.

A function is a routine built into the code that tells the computer to perform a specific task.

The Event source is the document.

And the event type is On open.

This means when the document is opened, the trigger will run My function.

Click Save.

When you add something new, the program may ask for authorization to run.

This makes sure the person opening the document knows that code will run.

Then, return to the document tab, and click Refresh to test your code.

It works. The welcome message appears automatically. Great work.

If your alert doesn’t open, don’t worry.

An important part of writing code is testing what you’ve built, then figuring out how to fix it if it doesn’t work.

Stick with it.

If you test your code a few times and can’t get it to work, ask your neighbor for help.

Once you’ve programmed your alert, share your document with a classmate, so they can see the message you created.

Then, check your Google Drive for a different version of “Research Notes” that your classmate shared with you.

See what kinds of welcome messages your fellow computer scientists created. Nice work.


  1. Create an onOpen event for the “Opening Message” project.
  2. Test the code by refreshing the document.
  3. Share the document with a classmate.