Skip to content Skip to sidebar Skip to footer

Passing Information To Server-side Function In A Google Docs Add On

I'm working on a Google Docs Add-On based on Google's Quickstart tutorial. I'm trying to change the workflow of the Add On in the tutorial to append a new page and then insert a tr

Solution 1:

Calling server-side functions from client Javascript

You've got a minor syntax error with google.run:

google.script.run(runTranslation)

It should be:

google.script.run.withFailureHander(failFunc) // Optional.withSuccessHander(succFunc) // Optional.serverFunction(optionalParams...);

The two Handler methods assign callback functions from your client-side JavaScript to be invoked in the case of success or failure of the server-side function. In both cases, the client-side function is provided as the only parameter.

The server-side function you want to communicate with is presented as if it is a method itself, with optional parameters to be passed across the divide.

The simplest case for you is:

google.script.run
             .translate(origin, dest, savePrefs);

(You had runTranslation in your posted code, but the server-side function is named translate()... I assume that's the right one.)

Now, this mightwill not take care of all your problems, so you wisely asked about debugging...

Debugging asynchronous client / server code in Google Apps Script

The provided debug environment isn't enough for debugging this sort of client / server exchange. There are also weaknesses in the Debugger for use with asynchronous execution - you can read more about that in Not seeing logs from onEdit trigger.

The simplest tool to get you debugging in this case would be to write logs to a spreadsheet

/**
 * Write message and timestamp to log spreadsheet.
 * From: https://stackoverflow.com/a/32212124/1677912
 */functionmyLog( message ) {
  var ss = SpreadsheetApp.openById( logSpreadsheetId );
  var logSheet = ss.getSheetByName("Log") || ss.insertSheet("Log");
  logSheet.appendRow([ newDate(), message );
}

You can call this from your client-side Javascript thusly:

google.script.run.myLog( "CLIENT: " + message );

That's a basic approach, but you can extend it more through use of utility functions and the BetterLog library. See more about that in my blog entry Did you know? (You can log to a spreadsheet from client JavaScript!)

Post a Comment for "Passing Information To Server-side Function In A Google Docs Add On"