1

Here is my HTML code, from where I call the function loadClient when load button is clicked,

//LOAD CLIENT BUTTON`
document.getElementById("load_button").addEventListener('click',function(){
google.script.run.loadClient();
});

here is the respective function in the code.gs file

//function02-loadClient
function loadClient() {
    eval(UrlFetchApp.fetch('https://apis.google.com/js/api.js').getContentText());
    return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/webmasters/v3/rest")
       .then(function() { Logger.log("GAPI client loaded for API"); },
              function() { Logger.log("Error loading GAPI client for API" ); });
}
4
  • Can you first confirm that any other part of loadClient() works when you click load_button? Commented Jun 10, 2018 at 18:15
  • I don't think that you need to use eval() What is that for? Also gapi is a PHP class, isn't it? I don't think that's an Apps Script class. Commented Jun 10, 2018 at 18:41
  • It looks like you are trying to put client side JavaScript code into your HTML. If the code at https://apis.google.com/js/api.js is client side code that you wan to run in the browser, then you can use the src attribute of a script tag. src attribute <head><script src="https://apis.google.com/js/api.js"></script></head> Commented Jun 10, 2018 at 19:01
  • FYI gapi is the Google API Client Library for Javascript. Commented Jun 11, 2018 at 2:35

2 Answers 2

1

When you call google.script.run in the client-side environment, you're requesting execution of a server side Apps Script function.

Your implementation of loadClient() doesn't make sense in the context of server-side Apps Script execution.

Here's a full, simple example of successfully triggering a Logger.log() call on the server-side by way of a client side button click:

client.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <script>
    function registerListener() {
      document.getElementById('callServerFunction').addEventListener('click', function() {
        google.script.run
            .withSuccessHandler(function() {
              alert("Successfully called server function.");
            })
            .withFailureHandler(function() {
              alert("Failed to call server function.");
            })
            .serverFunction();
      });
    }
  </script>
  <body onload="registerListener()">
    <button id="callServerFunction">Call Server Side Function</button>
  </body>
</html>

Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('client');
}

function serverFunction() {
  Logger.log("Server function called.");
}

Server side logging results after the button is clicked

[18-06-10 13:48:20:359 PDT] Server function called.

Regarding the Javascript Google API Client Libraries (i.e. https://apis.google.com/js/api.js), that's not meant to be used in the Apps Script server side context. The whole point of Apps Script is that there is a laundry list of services ready to use immediately without any setup. In addition to not being compatible, trying to load the client-side JS libraries in the server side Apps Script context is simply redundant.

Similarly, trying to use the client-side JS libraries in client-side Apps Script code doesn't make too much sense either, as you have the full set of server-side functionality available via google.script.run.

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

6 Comments

Thanks for pointing out that Google API client libraries are redundant in Google App Scripts, is there any other way to achieve this?
I don't know what it is you're trying to achieve. In Apps Script, the standard way to interact with Google services is via the numerous APIs exposed by default (DriveApp, MailApp, SpreadsheetApp, etc.) See the documentation I linked to in my answer.
I am trying to duplicate the functionality of an addon made by google,: chrome.google.com/webstore/detail/search-analytics-for-shee/… and the Search Console API is not available in advanced Script functions, I checked in the "laundry" list that you provided. thank you
To interact with APIs not included by default, you can use this Apps Script OAuth2 library to facilitate authentication. For reference, this Search Console API documentation details the required OAuth scopes. FYI, that Search Analytics for Sheets add-on is not produced by Google.
In particular, note the samples directory within the repository. There are a couple examples of interacting with Google APIs that do not have Apps Script services setup.
|
0

"trying to load the client-side JS libraries in the server side Apps Script context is simply redundant."

...is not redundant when G. fails to provide/implement/disclose a client-side method unimplemented on the server side; e.g.:

Drive.files.emptyTrash()

https://developers.google.com/drive/api/v2/reference/files/delete?hl=en#javascript

  • empty trash is simply nowhere to be found among GA Apps Script server-side js calls references urls

https://developers.google.com/apps-script/reference/drive

https://developers.google.com/apps-script/reference/drive#methods

https://developers.google.com/apps-script/advanced/drive

The sought-after capability is NOT THE SAME as file.setTrashed(true) read the necessity as: empty the trash, not 'put a file in the trash', 'mark a file as trashed'; nor is any 'interactive' option viable for e.g. for deleting thousands of files directly or via the trash.

  • it's impossible with Drive's GAS to permanently delete a file directly
  • semantics of 'delete' Google admits existence of in Google's Drive client side APIs:

https://developers.google.com/drive/api/v2/reference/files/delete?hl=en

  • which can't be called from, and for which there is no equivalent, on the server side !
  • no english phrase I can conceive of adequately captures the implications, otherwise, and economic, of this glaring omission

But perhaps someone can provide the (?undocumented) GAS Apps Script call counterexample

Comments

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.