1

Let's say I have this function:

function alertar(userEmail) {
   var doc = DocumentApp.getActiveDocument();
   // Get the URL of the document.
   var url = doc.getUrl();
   // Send yourself an email with a link to the document.
   var u = Session.getActiveUser();

   ui.alert('it worked');
   GmailApp.sendEmail(userEmail, "assunto", "hey, click here" +  url);

}

And then I created menus as follows:

 var editors = DocumentApp.getActiveDocument().getEditors();
 var m = ui.createMenu('uPaPoPe Actions');
 var subMenu = ui.createMenu('Escritores');
 for (var i=0;i<editors.length;i++)
 {
   subMenu.addItem(editors[i].getEmail(), 'function()          {alertar("'+editors[i].getEmail()+'");}');  //line with problem
  }

How do I pass parameters to a function that requires them from a google apps script menu item? Doing any of the following constructions is not valid:

   subMenu.addItem(editors[i].getEmail(), 'function()          {alertar("'+editors[i].getEmail()+'");}');  


   subMenu.addItem(editors[i].getEmail(), 'alertar("'+editors[i].getEmail()+'");');  

This is allowed:

   subMenu.addItem(editors[i].getEmail(), 'alertar');  

But the latter does not allow me to pass the editor's email as parameter.

ClayKaboom
  • 1,833
  • 1
  • 22
  • 42
  • 1
    [AddMenu should be able to use anonymous functions or at least pass parameters](http://code.google.com/p/google-apps-script-issues/issues/detail?id=477) – wchiquito Jan 02 '14 at 01:09
  • exactly that! I was trying to use callbacks, but it only accepts string.. Anyway I ended up so far using an HTML that instead of the menus to call the "alertar" function... – ClayKaboom Jan 02 '14 at 22:13
  • Here is a very simple way to achieve what you want to do: http://stackoverflow.com/a/17527883/5526708 – flo5783 Apr 18 '17 at 16:43

0 Answers0