简体   繁体   中英

Pass URL from Google Script to HTML file

I am having an issue passing a URL that I want the user to go to when they click a button on my sidebar.

  <body>
      <p class="header">First we need to Authorize.</p>
    <button class="center waves-effect waves-light btn" id="btn">Authorize</button>

     <script>
        document.getElementById("btn").addEventListener("click",authorize);
 
        function authorize(){
          google.script.run.authorizeSidebar();

        }
 </script>
  </body>

My code on GS is:

function authorizeSidebar(){
var Authservice = getAuthorization();
    var authorizationUrl = Authservice.getAuthorizationUrl();
    //Logger.log(authorizationUrl);
 
 }

I'm able to output the URL I need on the second function, but I'm having a hard time passing it from the GS file to the HTML file for the user to click on.

Any guidance would be appreciated.

Callback function in .withSuccessHandler() would receive the url returned by the server function:

Client side:

<script>
document.getElementById("btn").addEventListener("click",authorize);

function callbackReceiver(url){//📞☎️
  alert(url);
}


function authorize(){
   google.script.run
    .withSuccessHandler(callbackReceiver)
    .authorizeSidebar();
}
</script>

Server side:

function authorizeSidebar(){
  return getAuthorization()
        .getAuthorizationUrl();   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM