1

I am trying to replicate a simple Web app tutorial video, but can't get google.script.run.function to create a log in my code.gs. Here is my code:

Code.gs

function doGet(){
  return HtmlService.createHtmlOutputFromFile("clock");
}

function userClicked(){
  Logger.log("Log clicked");
  console.log("Console click");
}

clock.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>test</h1>
    <div id="replace">___</div>
    <button id="submit">Submit</button>

    <script>
      document.getElementById("submit").addEventListener("click",doSubmit);

      function doSubmit(){
        google.script.run.userClicked();
        document.getElementById("replace").innerHTML = "clicked";
      }
    </script>
  </body>
</html>

When I run userClicked within the editor, the execution logs show up fine. Also, when the button is clicked in the webapp, the "clicked" text shows up just fine. Also, both doGet and userClicked show up in my Executions. The problem is that the logs do not show up in my execution logs when run from the webapp. I have a found a couple threads similar to this, but it never seems to get resolved.

UPDATE: I also tried adding withSuccessHandler and the results are the same - the functions both run fine in the Executions, but no log shows up in the Executions Log (also true of "Logs" if I test in the legacy version). The new html is this:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>test</h1>
    <div id="replace">___</div>
    <button id="submit">Submit</button>

    <script>
      document.getElementById("submit").addEventListener("click",doSubmit);

      function onSuccess(){
        document.getElementById("replace").innerHTML = "success";
      }

      function doSubmit(){
        google.script.run.withSuccessHandler(onSuccess).userClicked();        
      }
    </script>
  </body>
</html>
  • You need to use the SuccessHandler – Cooper Oct 23 '21 at 16:15
  • https://developers.google.com/apps-script/guides/html/communication#success_handlers – Cooper Oct 23 '21 at 16:29
  • 1
    Look in ``executions``(not `code editor`) after 2-3 minutes – TheMaster Oct 23 '21 at 16:34
  • @TheMaster both doGet and userClicked show up in Executions almost immediately. The problem is that "Log clicked" text doesn't show up in editor log and I don't know why. – Matthew Michaud Oct 23 '21 at 16:57
  • @Cooper I just updated to add withSuccessHandler; same results – Matthew Michaud Oct 23 '21 at 17:45
  • @TheMaster are you saying that Logger.log and console.log content is not expected to show up in the editor's log anymore? Is it only expected to show up in the Executions tab? – Matthew Michaud Oct 23 '21 at 18:25
  • Does it show up in executions tab? In the old editor, even doGet or anything related to webapp won't show in editor executions, but they do show up in cloud logs. – TheMaster Oct 23 '21 at 20:12
  • @TheMaster yes, the log messages show up in the executions tab. In the tutorial I was following, the old View->Logs is all that is used. When mine didn't behave the same, I assumed there was an error. If it now only shows in the executions tab, that's fine. https://youtu.be/RRQvySxaCW0?t=1141 – Matthew Michaud Oct 23 '21 at 22:30
  • Did you [update the deployment](https://developers.google.com/apps-script/concepts/deployments) after adding the loggers to `userClicked`? – Iamblichus Oct 25 '21 at 07:31

1 Answers1

1

What works then is if you create a function and call that function with your server function, so:

function logHi(){
  console.log("hi")
}

// And then use it
function userClicked(){
  logHi()
}

Neven Subotic
  • 1,399
  • 1
  • 6
  • 18