Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script - Error: script function not found when library used

When I create a script within a spreadsheet using UiApp, I have a button with a server handler which ends in an underscore. If I run the script from within the spreadsheet then it works fine, but if I add this project into another sheet and run it then I get an error saying the script function is not found. It has no problem finding functions that end with an underscore when they are used in the code, it just seems to be when they are called from a server handler.

To replicate:

Create a new spreadsheet

Insert the code:

function buildForm() {
  var app = UiApp.createApplication();

  // show that calling a function ending in underscore works
  var labelText = getLabelText_();

  app.add(app.createLabel(labelText).setId("label"));

  var handler = app.createServerHandler("clickHere_");
  app.add(app.createButton("Click Here",handler));

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.show(app);
}

// This is just to show that underscore on the end works as long as it is not a server     handler
function getLabelText_() {
  return "label text";
}


// called from server handler
function clickHere_(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById("label").setText("You clicked there");
  return app;
}

Run the code and see that it works as expected

Create another spreadsheet, add the first spreadsheet as a library and call it MyCode. Make sure you have saved a version etc etc.

In the new spreadsheet, insert the code:

function runIt() {
  MyCode.buildForm()
}

Run this code, note that getLabelText_() works fine and the UI is created, but when you press the button, the function not found error will appear.

If I remove the underscore from clickHere_(e) and change the server handler accordingly, then it works.

Is the only solution to remove the underscore from the end of all functions called by a server handler?

like image 560
user3097534 Avatar asked Dec 31 '25 20:12

user3097534


1 Answers

This is the wanted comportment for google apps script library:
https://developers.google.com/apps-script/guide_libraries#writingLibrary
That's designed to make private functions.

EDIT:

as it say in the documentation:

If you want one or more methods of your script to not be visible (nor usable) to your library users, you can end the name of the method with an underscore. For example, myPrivateMethod_()

As "library function", function buildForm() can call function getLabelText_() that is also in that library. But you can't call directly function getLabelText_() from the script using the library. Neither your script has the right to call directly function clickHere_(e).
So when used as server handler the function clickHere_(e) is called from the script not from the library, and it won't work. To call this function you should remove the "_" at the end of it and call it this way: libraryName.libraryFunction();

like image 131
Harold Avatar answered Jan 06 '26 15:01

Harold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!