GAS 同步后端脚本与页面加载

GAS syncing backend scripting with page load

在我的 html 代码中,我调用脚本来编辑 google 表单,然后加载该表单。我的问题,网页在脚本完成更新之前加载。

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <script>
  google.script.run.changeForm();     
  window.top.location.href='https://docs.google.com/forms/d/e/ABCDpQLSdV2e61UFkG-_LcVBRpWe9F1MizNJ-P5JUCUGRlWiFSoImPkA/viewform';
  </script>
  <body>
  </body>
</html>    

我的临时修复是 setTimeout(),但这感觉像是一个肮脏的小把戏。

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <script>
  google.script.run.changeForm();  
  setTimeout(function(){  
  window.top.location.href='https://docs.google.com/forms/d/e/ABCDpQLSdV2e61UFkG-_LcVBRpWe9F1MizNJ-P5JUCUGRlWiFSoImPkA/viewform';
  }, 2000);
  </script>
  <body>
  </body>
</html>

有没有更好的方法来解决这个问题?

你应该试试

google.script.run.withSuccessHandler(success).changeForm();
function success(){
window.top.location.href='https://docs.google.com/forms/d/e/ABCDpQLSdV2e61UFkG-_LcVBRpWe9F1MizNJ-P5JUCUGRlWiFSoImPkA/viewform';
}

withSuccessHandler(success) 运行 参数中的函数 changeForm() 完成执行后