1

I'm struggling with Apps Script's google.script.run response time. There is a function that returns user permission as boolean: initFunction() { // function code } returns true.

Some divs in my frontend are showing based on the initFunction boolean, and I don't know why the callback time is soo slow (like 4-5 seconds)

INDEX.HTML

<script>
  function check_permission () {
    google.script.run.withSuccessHandler(function(data){
      if (data === true) {
          document.getElementById('hidden_div').style.display = 'block';
      } else {
        document.getElementById('hidden_div').style.display = 'none';
      }
    }).initFunction();
  }

  document.addEventListener('DOMContentLoaded', () => { check_permission() });
</script>

I've tried calling initFunction just after sidebar load function just to check the function time and it returns true in 0.5 seconds, so it's not about the function, i suppose it's about google.script.run

function sidebarLoad() {
  let route = HtmlService.createTemplateFromFile('index').evaluate();
  SpreadsheetApp.getUi().showSidebar(route);
  let permission = initFunction(); SpreadsheetApp.getUi().alert(permission)
}

How could I solve this and reduce response time?

Edits: after reading your comments I still don't know where is the problem but i've been doing tests and:

  • When calling the function from onclick event, the time response is very fast, so it's not about the function itself.
  • Answering @TheMaster, my start criteria for time response is when pressing the menu ui button that opens my GAS sidebar. The DOMContentLoaded function triggers immediately, I know because I changed the google.script.run in check_permission function with any other javascript code and it's loaded quicky. So I suppose it's not about DOMContent loading slowly.
  • If I click a button in the loaded html page that calls the function check_permission() I also get the results immediately. I only get the slow response time when google.script.run is triggered by DOMContentLoaded listenerEvent.
alsanmph
  • 77
  • 5
  • Show network tab in browser devtools – TheMaster Sep 14 '22 at 12:12
  • i'm reading the network tab but I don't know how or what exactly share to you – alsanmph Sep 14 '22 at 12:25
  • 1
    Delays are logged there. See when the `.run` makes a request and show that it indeed takes 5s or figure out what else is cause of the delay. – TheMaster Sep 14 '22 at 12:27
  • 1
    Also show executions tab on the server. It should show if `initPermission()` takes more than 5s, only when called from `.run` – TheMaster Sep 14 '22 at 12:29
  • That question does not answer me, sorry. It says that google.script.run costs a delay time, and that's ok, but if I call the function with onclick event it gets the boolean return instantly from the server side, and calling with DOMContentLoaded 4-5 seconds. – alsanmph Sep 14 '22 at 14:24
  • 1
    If the linked duplicate answers doesn't answer your question, [edit] your question to explain how the answers don't satisfy your question. Note that there's a proper [etiquette](https://meta.stackoverflow.com/questions/252252) to dispute a duplicate. – TheMaster Sep 14 '22 at 14:29
  • `calling with DOMContentLoaded 4-5 seconds.` 5s from when? From pageload? What's the start and end criteria? If from page load, then your browser is taking time to parse the data. Page load: 00:00; DOMContentLoaded: 00:05.001; Server callback: 00:05.002. So, it took less than a millisecond, but you attribute all that time to `google.script.run` – TheMaster Sep 14 '22 at 14:38

1 Answers1

0

Try using templated html and load user permission as a global into the template before it's rendered.

function myFunction() {
  let temp = HtmlService.createTemplateFromFile('filename');
  temp.protection = initFunction();
  let html = temp.evaluate();
}

Then in the html protection is a global variable and you can distribute it with scriptlets

Pushing variables into templates

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • I've tried adding this to my function, using temp.protection = initFunction(), but adding != protection ?> to my html file results in "protection is not defined" exception. I don't know what I'm missing. I've tried using a boolean and string return of the function. – alsanmph Sep 14 '22 at 16:31
  • I made a mistake. I had the evaluate in the wrong place. – Cooper Sep 14 '22 at 16:37
  • 1
    thank you very much, it works really good although I don't like scriptlets very much. Also, my scriptlet was inside a – alsanmph Sep 14 '22 at 17:35