0

I have some information that I would like to push from a primary google sheet to other sheets which I need to select, I would like to turn this into a menu item so I've added:

  ui.createMenu('Push to Sheet').addItem('Select Sheet', 'myFunction').addToUi();

I have a complicated function that starts with:

function myFunction(PARAM1,  PARAM2) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  .......

I'm not clear How to pass parameters to the function using this technique. How can I get this working?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 1
    Related: https://stackoverflow.com/questions/20872214/google-apps-menu-function-with-parameter https://stackoverflow.com/questions/46529007/can-a-google-sheets-custom-menu-pass-a-variable-to-function https://stackoverflow.com/questions/23823558/google-apps-script-calling-a-function-from-menu-with-a-spreadsheet-range-as-the https://stackoverflow.com/questions/50946493/how-to-know-which-menu-item-was-clicked (and so on) – tehhowch Aug 13 '18 at 14:52

2 Answers2

4

There is no way to pass a parameter to the function associated with a menu item. Not sure why you would. Unless you have more than one menu item that uses the same function and you are trying to determine which menu item was selected. In that case you would need a wrapper function something like myFunction1(), myFunction2(), etc. linked to each menu item individually. Then within myFunction1() you call myFunction(1).

TheWizEd
  • 7,517
  • 2
  • 11
  • 19
  • Yes , your example is my use case. You give an example in code? I have a follow up at https://stackoverflow.com/questions/51826107/script-function-not-found-entries – user1592380 Aug 13 '18 at 15:47
  • 1
    Heres is example of how to do it: https://stackoverflow.com/questions/51827523/capture-the-name-of-the-selected-ui-menu-option – user1592380 Aug 13 '18 at 19:03
0

While there is no way to pass arguments to your newly created function, you can however pass in simple global variables, but they have to be defined at the root level and they cannot be altered by any means from any subsequent functions.

i.e. if you defined the following in the root script

var folderToSearch = ['folderIDString']

You could pick that up in your

newCreatedMenuFunction () {
Logger.log("folderToSearch: " + folderToSearch);
}

Output 'folderIDString'

Not that it does you much good, but some parameters are possible. You might have to rethink what it is you are trying to achieve.

sandalwoodsh
  • 129
  • 1
  • 11