簡體   English   中英

Google Script Run 通過 Apps Script 中的前端調用提高性能

[英]Google Script Run Improve Performance with frontend calls in Apps Script

我正在為 Apps Script 的 google.script.run 響應時間而苦苦掙扎。 有一個 function 返回用戶權限,因為 boolean: initFunction() { // function code }返回true

我前端的一些 div 顯示基於 initFunction boolean,我不知道為什么回調時間很慢(比如 4-5 秒)

索引.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)
}

我該如何解決這個問題並減少響應時間?

編輯:閱讀您的評論后,我仍然不知道問題出在哪里,但我一直在做測試並且:

  • 從onclick事件調用function時,時間響應非常快,所以與function本身無關。
  • 回答@TheMaster,我的時間響應開始標准是按下打開我的 GAS 側邊欄的菜單 ui 按鈕時。 DOMContentLoaded function 立即觸發,我知道是因為我更改了 check_permission function 中的 google.script.run 和任何其他 javascript 代碼並快速加載它。 所以我想這與 DOMContent 加載緩慢無關。
  • 如果我單擊加載的 html 頁面中調用 function check_permission() 的按鈕,我也會立即得到結果。 當 DOMContentLoaded listenerEvent 觸發 google.script.run 時,我只會得到緩慢的響應時間。

嘗試使用模板化 html 並在渲染之前將用戶權限作為全局加載到模板中。

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

然后在 html 保護是一個全局變量,您可以使用腳本分發它

將變量推入模板

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM