Home > Back-end >  How to add the onOpen trigger to a Google Sheet with Google Apps Script?
How to add the onOpen trigger to a Google Sheet with Google Apps Script?

Time:10-10

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.

code:

/**
 * The event handler triggered when opening the spreadsheet.
 * @param {Event} e The onOpen event.
 * @see https://developers.google.com/apps-script/guides/triggers#onopene
 */
function onOpen(e) {
  // Add a custom menu to the spreadsheet.
  SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
      .createMenu('Custom Menu')
      .addItem('First item', 'menuItem1')
      .addToUi();
}

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?

CodePudding user response:

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

Please note that there is an orange / yellow dot next to the filename, Code.gs. 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.

Related

  • Related