How to add the onOpen trigger to a Google Sheet with Google Apps Script?


I’ve run a few stand-alone "hello world type scripts" but cannot get this to run:

cannot run

The run button is greyed out.


 * The event handler triggered when opening the spreadsheet.
 * @param {Event} e The onOpen event.
 * @see
function onOpen(e) {
  // Add a custom menu to the spreadsheet.
  SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
      .createMenu('Custom Menu')
      .addItem('First item', 'menuItem1')

This was created from within Google Sheets, so that it’s bound to a spreadsheet.

While I’ve run a few "hello world" stand-alone scripts, how is this bound script saved, deployed, and executed?


The run button is greyed out there is no function selected in the function selector dropdown. This other toolbar element is greyed out because was not saved having a function on it.

Please note that there is an orange / yellow dot next to the filename, That means that the last changes done to the file were not saved yet.

Once you save the file, the function selector dropdown and the run button will be enabled and you should be able to run the onOpen function.


Answered By – Rubén

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published