Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is a snippet in the Google Apps Script for adding a menu in Google Docs/Forms/Sheets. As stated in the Menu#addItem method it calls the menuItem2 function, but the snippet didn't include a sample on how to call the addItem when you want to add parameters in function call, or is this not possible?

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Custom Menu')
    .addItem('First item', 'menuItem1')
    .addSeparator()
    .addSubMenu(ui.createMenu('Sub-menu')
      .addItem('Second item', 'menuItem2'))
    .addToUi();
}

function menuItem2() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
     .alert('You clicked the second menu item!');
}

function menuItem2(PARAMETER_HERE) {
  // codes    
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

You can't add parameters to functions called by a menu.

A simple workaround is to store parameters elsewhere (in scriptProperties for example) and read these parameters if parameter is undefined.

function menuItem2(PARAMETER) {
  // if PARAMETER is undefined then read default parameter in scriptProperties
  // codes    
}

In this configuration you can call the menuItem2 function from elsewhere in the script using a "normal" parameter and it will be handled as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...