Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function onOpen() is not running

My function includes adding a menu and toast to the document. I have verified that the trigger (onOpen) is set as well. It only works when a user goes into Tools, Script Manager, Run. We have too many users with too many backgrounds to expect then to know how to do this. Why isn't it working? (Using Chrome)

function onOpen()
{
  var menus = [{name: "Advance in Workflow", functionName:"sendEmail"}];

     SpreadsheetApp.getActiveSpreadsheet().addMenu("Auto Advance FG Workflow", menus);

  //sheet.toast(Notify/Remind users);
   sheet.toast("While you are here we kindly ask that you do not add, modify or remove     any columns.","Welcome - " + username,8);
 }

Thanks,

like image 801
user1817058 Avatar asked Nov 12 '12 02:11

user1817058


3 Answers

This is an old post but I just had this problem and find out why it was not working correctly in my case: I had, at the top of my script file a variable that required some authorisations and that prevented the script to correctly run. I saw that OP called var username = Session.getActiveUser().getUsername(); (that requires authorisations, and it's may be the cause).

eg: this code won't work:

function onOpen(){
  SpreadsheetApp.getUi()
  .createMenu("Exportation")
  .addItem("Lancer l'exportation", "exportationMenu")
  .addToUi();
}
var stConsCons= SpreadsheetApp.openById(sgcid).getSheetByName("Consultant");

but this one will work:

function onOpen(){
  SpreadsheetApp.getUi()
  .createMenu("Exportation")
  .addItem("Lancer l'exportation", "exportationMenu")
  .addToUi();
}

function whatever(){
  var stConsCons= SpreadsheetApp.openById(sgcid).getSheetByName("Consultant");
...}
like image 34
Harold Avatar answered Oct 11 '22 00:10

Harold


I was having the same issue.

I realized, sometimes Google create some kind of cache of the scripts (I'm used to have a "test" script and I usually alter it's content, and, sometimes, the script runs as if I didn't).

So, what I did that solved the onOpen() not working was changing the function name and ading a trigger manually.

Go to "Resources -> Current script's triggers…"

Go to "Resources -> Current script's triggers…"

Choose the function to run on open

Choose the function to run on open

It worked like a charm here!

Updated Location Information: from tool bar or from menu bar

Then

trigger select

like image 180
Eduardo Russo Avatar answered Oct 11 '22 00:10

Eduardo Russo


It turns out that you need to add the onOpen(e) function to Triggers!

image description here

like image 26
DiZiNnEs Avatar answered Oct 10 '22 22:10

DiZiNnEs