4

Good afternoon.

I am trying to create a button to refresh the page, through the Google app Script Web, but the method "window.location.reload ();", does not work could someone help?

link Google app Script Web: https://script.google.com/d/1jyWe9jm0dIqHsY1uKJZ0pOJYzLuq9dip1cSsXhxBwv53cFakW2LHgM_n/edit?mid=ACjPJvGbLXQwvhjDh7fsdifcTFvgQby-gsIUWeCSmwUzTdnVvXw8LNckG8rXcsyhcYvAcWAN7pU4E05bQEJwZl_1189N-bwyvttAxpHxmbvzUvx_d9SDKh19x3VzY9XxClhXweVlmqdMOSs&uiv=2

<html>

<base target="_se">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://tablesorter.com/__jquery.tablesorter.min.js" type="text/javascript"></script>

<button onclick="sortTable()">Sort</button>
<button onclick="refresh()">refresh </button>

  <table class="striped centered ajuste" id="tab">
           <thead >           
             <tr>

              <th>ID</th>
              <th>NOME</th>
              <th>DATA</th>

             </tr>
           </thead>
            <tbody id="coportoTabela">

            </tbody>
  </table>

 document.addEventListener("DOMContentLoaded", function(){

        google.script.run.withSuccessHandler(gerarTabela).pegarTabelaEstoque();

  });

  function gerarTabela(dataArray){
    var tcorpo =  document.getElementById("coportoTabela");    

     dataArray.forEach(function(r){

     var linha = document.createElement("tr");

     var coluna1 = document.createElement("td");
     coluna1.textContent = r[0];
     var coluna2 = document.createElement("td");
     coluna2.textContent = r[1];
     var coluna3 = document.createElement("td");
     coluna3.textContent = r[2];

     linha.appendChild(coluna1);
     linha.appendChild(coluna2);
     linha.appendChild(coluna3);

     tcorpo.appendChild(linha); 

     });      

  }


   function refresh(){
     window.location.reload();
   }     

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Fragosojp
  • 141
  • 2
  • 8
  • Does this answer your question? [How to reload a Google Apps Script web app with a link?](https://stackoverflow.com/questions/59475342/how-to-reload-a-google-apps-script-web-app-with-a-link) – Rubén Apr 03 '20 at 19:18
  • Does this answer your question? [Can't stop Google Apps Script from masking redirected URL](https://stackoverflow.com/questions/56685553/cant-stop-google-apps-script-from-masking-redirected-url) – TheMaster Apr 03 '20 at 19:53

2 Answers2

16

Try this:

Javascript:

function reLoad() {
       google.script.run
      .withSuccessHandler(function(url){
        window.open(url,'_top');
      })
      .getScriptURL();
}

GS:

function getScriptURL() {
  return ScriptApp.getService().getUrl();
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • 2
    Finally! Something that works for Apps Script apps. Thank you! Google does some strange hosing of URLs alright. – jeff Dec 10 '20 at 21:45
  • For documentation, it works with deployed webapp (the link with exec at the end), not in dev mode. But good solution! Thanks :) – Waxim Corp Oct 25 '21 at 12:58
  • Update for August 2023. This Cooper solution works perfect also for development mode (dev mode). – Nerea Aug 19 '23 at 12:57
1

After location.reload() you should return false in onclick function:

function teste(){
location.reload();
return false;
}
Abilogos
  • 4,777
  • 2
  • 19
  • 39
  • the function even works but the screen is all white – Fragosojp Apr 03 '20 at 19:14
  • What do you mean by White Screen? dosent it reload The Page? can you see the reloaded page in browser network tab? – Abilogos Apr 03 '20 at 19:16
  • see through this application link: https://script.google.com/macros/s/AKfycbx3RHYokreumTcdvQ12PXnGoNSiXoOYKTLCPq6Tj6s/dev – Fragosojp Apr 03 '20 at 19:18
  • The Page is showing from an iframe in script.google . i think it prevents inner script from reloading and redirecting because of mal scripts. and this should work fine on your local pc – Abilogos Apr 03 '20 at 19:27