Add a custom menu

Why?

Scripts you write can be run from within the script Editor, but that isn’t very user-friendly. Instead, you’re going to want to add your own menu.

How?

Include an onOpen() function in your script. This will be run automatically when the container Sheet is opened by the user.

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Classroom')
    .addItem('List classes', 'listClasses')
    .addToUi();
}

Notes

This utilises the getUI method of the SpreadsheetApp class, which is the parent class of the Spreadsheet service. The example above uses method chaining to first create a menu (called “Classroom”), then to add items to it (e.g. “List classes”). You can add as many .addItem() lines as you want. You can also .addSubmenu() and .addSeparator().

The second parameter is the name of the function in your script that you would like to execute when the menu item is clicked. Unfortunately, you can’t pass any parameters to this function, meaning you might need to use a work around, e.g.

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Classroom')
    .addItem('Sync Year 7 students', 'sync7')
    .addItem('Sync Year 8 students', 'sync8')
    .addItem('Sync Year 9 students', 'sync9')
    .addItem('Sync Year 10 students', 'sync10')
    .addItem('Sync Year 11 students', 'sync11')
    .addItem('Sync all staff', 'syncStaff')
    .addItem('List students without guardians', 'listNoGuardians')
    .addToUi();
}
function sync7()  { syncStudents(7); }
function sync8()  { syncStudents(8); }
function sync9()  { syncStudents(9); }
function sync10() { syncStudents(10); }
function sync11() { syncStudents(11); }

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x