Get the full browser URL in Apps Script

浪尽此生 提交于 2020-08-09 10:44:09

问题


I'm looking to get the full URL of the current browser window in Apps Scripts while displaying the web app via HTMLService

  1. When I try this, it doesn't work because the Apps Scripts is sandboxed and I get another server url of some sort

    window.location.href 
    


  2. When I try this, I can only fetch the hash or parameters, not the full url

    google.script.url.getLocation(function(location) {
      console.log(location.parameters);
      console.log(location.hash);
    });
    

    source: https://developers.google.com/apps-script/guides/html/reference/url#methods


Purpose for the above: I want a button that people click on in the published Web App to remove the browser toolbar. However, the browser toolbar is not removable in the current browser window. So I want to fetch the Web App URL and put it in window.open() as a new window with the current web app because the browser toolbar can be remove this way. But I can't seem to get the current URL.

Any ideas how I can achieve this?

Thanks in advance!


回答1:


I don't know what you meant by developer mode either but I took a guess at this being the answer to the question. If not, let me know and I'll remove it.

On the clientside you can use Javascript:

<script>
  function getUrl(){
    google.script.run
    .withSuccessHandler(function(url){
       document.getElementById('mydiv').innerHTML='<p>' + url + '</p>';
     })
    .getScriptUrl()
</script>
<body>
  <div id="mydiv"></div>
</body>

In Google Script:

function getScriptURL() {
  return ScriptApp.getService().getUrl();
}



回答2:


I'd suggest this code for the current script URL:

function getScriptURL() {
  // dev URI
  'https://script.google.com/macros/d/' + ScriptApp.getScriptId() + '/edit';
}


来源:https://stackoverflow.com/questions/53698027/get-the-full-browser-url-in-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!