google.script.run isn't working?

4,915 views
Skip to first unread message

Sean Carroll

unread,
May 18, 2021, 7:30:23 AM5/18/21
to Google Apps Script Community
I am just beginning with App Script and am unable to run a function using google.script.run. Ive included code below its pretty simple so not sure whats wrong. Basically my "doSomething" function is not logging anything even though the executions says it has run. 

If I run the function manually from the IDE it operates as expected.  

I have deployed as a web app and checked Im running the most up to date one. Also my log says "No functions have been run in this editor session." 

Code.gs 
 function doGet() {
  return HtmlService.createHtmlOutputFromFile('index');
}

function doSomething() {
  Logger.log('I was called!');
  console.log('I was called'); 
}

index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>

  <body>
      <script>  google.script.run.doSomething(); </script>
  </body>
</html>

Ive done a fair bit of searching and found the problem a few times with no solutions so any help would be hugely appreciated.

Mark Grimshaw-Aagaard

unread,
May 18, 2021, 8:09:02 AM5/18/21
to Google Apps Script Community
Hi Sean,

I've no idea what's wrong with your script (but you're probably missing a withSuccessHandler – see below) but I notice you're trying to log events from within .gs. As a newbie myself to GS a few months back, this was something that confused me – GS has its own environment that is rather arcane until you get used to it and logging is one area that is puzzling. I don't think you will get anything back from console.log in .gs but you will in your sidebar .html code. The trick is to get output from .gs back to .html. I never bothered with Logger etc. as it seemed far too complicated to set up for something so trivial so came up with the following (explanation below):

HTML...
.
.
.
<script>
function heartbeat() {
google.script.run
.withSuccessHandler(heartbeatResult)
.userCheckHeartbeat(document.getElementById("url").value);
}
function heartbeatResult(result) {
if(!result.xmlResponse) {
displayError(result.message);
return;
}
displaySuccess(result.message);
return;
}

// Display SUCCESS messages
function displaySuccess(success) {
document.getElementById("success").innerHTML = success;
}
</script>
.
.
.
.gs...
.
.
.
(function(){ // GLOBAL variables accessible in other scripts – must be defined!
successHeartbeat = "Yes, I am alive and kicking. Try searching me . . .";
errorXMLHTTP = "ERROR: XMLHTTP error – could not connect to the WIKINDX.";
})();

function userCheckHeartbeat(url) {
var formData = {
method: 'heartbeat'
};
var response = doXml(url, formData);
if (response.xmlResponse === false) {
return {
xmlResponse: false,
message: response.message
};
} else {
return {
xmlResponse: true,
message: successHeartbeat
};
}
}
function doXml(url, formData) {
url += "office.php";
formData.source = 'googleDocs';
formData.compatibility = compatibility; // Set in wikindx.gs
var options = {
method: 'post',
payload: formData,
muteHttpExceptions: true
};
try {
var response = UrlFetchApp.fetch(url, options);
if (response === null) {
return {
xmlResponse: false,
message: errorXMLHTTP
};
} else if (response.getResponseCode() != 200) {
return {
xmlResponse: false,
message: errorXMLHTTP
};
} else if(JSON.parse(response.getContentText()) == 'access denied') {
return {
xmlResponse: false,
message: errorAccess
};
} else if(JSON.parse(response.getContentText()) == 'incompatible') {
return {
xmlResponse: false,
message: errorCompatibility
};
} else {
return {
xmlResponse: true,
xmlArray: JSON.parse(response.getContentText())
};
}
} catch(e) {
return {
xmlResponse: false,
url: url + '?' + response.payload,
message: errorXMLHTTP
};
}
}
.
.
.

This basically takes user input from the sidebar (in this case a URL) and checks the URL is alive and kicking. That doesn't really matter for your question, but, of relevance to you, it uses objects (put anything you like in there) to pass data around within the .gs and back from the .gs to the .html. From there you can console.log the object variables or, as I do here, print to the sidebar. Of course, the object data need not be errors . . .

Hope it helps.

Mark

Alan Wells

unread,
May 18, 2021, 9:17:41 AM5/18/21
to Google Apps Script Community
Change to:

<script>  window.onload = function() { google.script.run.doSomething(); } </script>

Sean Carroll

unread,
May 19, 2021, 10:06:45 AM5/19/21
to Google Apps Script Community
Thanks both. 

The window.onload isn't working. Results are the same 

Basically I open the web app and my Executions look like this...

and my logs look like this 

@mgbot thanks for your code. Im not sure I fully understand it as Im a bit of a js noob but will dig around and try to get something to work. 

Cheers 

Alan Wells

unread,
May 19, 2021, 10:26:13 AM5/19/21
to Google Apps Script Community
Did you change anything in your  appsscript.json manifest file?
It should have a line for:
"exceptionLogging": "STACKDRIVER",

Sean Carroll

unread,
May 19, 2021, 10:32:49 AM5/19/21
to google-apps-sc...@googlegroups.com
Thanks for the quick response. Im a bit of a newbie here so not sure what you mean. I have not added a son manifest file? Im basically following a YT tutorial and a stuck after 5 mins. 

If I go to the stack driver logging or error reporting I get this 

Would I need to set up the different project mentioned? 

Thanks for your help 





-- 
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/9vHisfARGhg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/bdcb742d-0ca1-4357-a294-e7d8334b6017n%40googlegroups.com.

Alan Wells

unread,
May 19, 2021, 11:25:24 AM5/19/21
to Google Apps Script Community
You probably didn't change anything in the manifest file, but I'm not sure what the problem is, so we need to start looking at other possibilities.
Which code editor are you using, legacy or new?
Are you running the code from the account that created/is the owner or from a different account?
Take a look at the documentation for troubleshooting:

Mark Grimshaw-Aagaard

unread,
May 19, 2021, 11:27:26 AM5/19/21
to Google Apps Script Community
Instead of window.onload why not just in your sidebar.html:

<script>
.
.
.
initialize();

/** Initialization stuff */
function initialize() {
google.script.run
.withSuccessHandler(initializeResult)
.initializeGS(); // i.e. the function in your gs script
}
function initializeResult(result) {
if(!result.response) { // FALSE = error
// do something on an error in the gs script for initializeGS();
return;
} else {
// do something else on success
}
}
.
.
.
</script>

This works fine for me and initialize() runs when the add-on is loaded. As in my message above, I'm using withSuccessHandler() to deal with ANYTHING I want to return from the gs script.

Admittedly, the above is for a Google Docs add-on so perhaps web apps function differently.

Mark

Sean Carroll

unread,
May 20, 2021, 5:40:39 AM5/20/21
to Google Apps Script Community

Thanks for the quick replies! 

Which code editor are you using, legacy or new?

The legacy one. 


Are you running the code from the account that created/is the owner or from a different account?

No I literally am following this tutorial from the start. https://www.youtube.com/watch?v=RRQvySxaCW0&list=PLv9Pf9aNgemt82hBENyneRyHnD-zORB3l
Im concerned that my log is not responding at all how his is in the first few minutes where he is testing parameters, maybe that is important.  

Unless I run the function from the editor I get this 

Ive simplified hist code to take out the button call but otherwise the same. I will dig into the trouble shooting guide you shared. 

also @Mgbot Thanks Ill give this a go

Andrew Apell

unread,
May 28, 2021, 5:11:08 AM5/28/21
to Google Apps Script Community
I had this problem for 2 years or so and my issue was that I was closing the modal window too soon after running google.script.run.doSomething().
When I changed my code, google.script.run started running consistently.
It is because of this that I think that the issue with your case is this line Logger.log('I was called!') or any other line of code that is moving on too fast from doSoomething().

Sean Carroll

unread,
May 28, 2021, 5:23:37 AM5/28/21
to google-apps-sc...@googlegroups.com
Thanks, a few people have suggested that the timing might be an issue. Can you give me an idea how you changed the code to solve it? 

Thanks for the help 



Andrew Apell

unread,
May 28, 2021, 5:44:32 AM5/28/21
to Google Apps Script Community
There is a way of causing the script to pause for a few seconds but I don't have the code right now. You could check Stack Overflow for this or search this forum (it was posted ages ago,,, I think).
However, personally, I just removed the line of code that was closing the modal window.

Sean Carroll

unread,
May 28, 2021, 5:46:40 AM5/28/21
to google-apps-sc...@googlegroups.com
Thanks, Ill have a look. Sorry for the noob question but what do you mean by modal window…did you have a pop up?

All the best 



Jimmy Li

unread,
May 9, 2024, 2:19:45 AMMay 9
to Google Apps Script Community
Don't mean to necro an old thread but this is the top result on Google for "google.script.run not working", so here is a summary of potential problems you may run into:
  • There is a known bug where google.script.run does not work and returns "PERMISSION_DENIED" when attempting to run when logged in as non-primary Google account, see Stack Overflow link and Google Issue Tracker link. Workarounds include disabling the v8 runtime and using Google Rhino (which downgrades Javascript and is not compatible with a lot of things including let), and developing on the primary account only.
  • The function may have run successfully, but the client-side function may not be handling the result with the right timing. google.script.run is asynchronous, so client-side code will proceed after calling it without waiting for a result. It's better to use .withSuccessHandler() and .withFailureHandler() callbacks which are guaranteed to call the client-side function when the server-side google.script.run function is done. See documentation here
  • The function may have run, but you won't see log messages in the client-side log. Logs of server-side functions called from google.script.run can be seen by visiting Executions in the online code editor:  My Executions - Apps Script (google.com) (make sure you are logged in as the same account which called the function), or by enabling Cloud Logging

Douglas Sirkka

unread,
May 9, 2024, 7:35:53 AMMay 9
to Google Apps Script Community
Since we are reviving this... I'm not sure if it helps with the OP's problem, but I had an issue passing dates in objects and had to send them as strings. No error code was given... it just wouldn't return data. 
Doug
Reply all
Reply to author
Forward
0 new messages