3

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?

1 Answer 1

3

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();

Sign up to request clarification or add additional context in comments.

1 Comment

Even though the library contains the code for the server handler which calls the private function, the server handlers can only call functions which are not private. Thanks for the clarification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.