Fragosojp
Fragosojp

Reputation: 151

How to refresh page, Google app Script Web

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();
   }     

Upvotes: 5

Views: 12510

Answers (3)

Zig Mandel
Zig Mandel

Reputation: 19835

While the top-voted answer works, it can push a new page into the browser's navigation stack. This might break site navigation, as the browser's "Back" button could now return users to the same page repeatedly (once for each refresh). To avoid this issue, you can use:

window.top.location.replace(url);

This method replaces the current URL without adding a new entry to the browser's history, ensuring smooth navigation.


Getting the URL Inside an iframe

Since you're working within an iframe, you have two options to obtain the page URL:

1. Pass the URL as a Parameter on Page Load

This approach avoids additional server calls and is highly recommended for its efficiency.

From your doGet function in Apps Script, you can pass the URL to the HTML template like this:

const template = HtmlService.createTemplateFromFile('Index');
// Pass parameters to the template
template.urlParam = ScriptApp.getService().getUrl();
return template.evaluate();

Then, in your HTML file, you can retrieve and use the URL:

<script>
    // Access the URL passed from the server
    const url = "<?= urlParam ?>";

    // Replace the current page in the iframe
    window.top.location.replace(url);
</script>

2. Make a Server Call to Retrieve the URL

While not as efficient, you could make a server call to retrieve the URL dynamically. This is generally less optimal.


By using the first method, you ensure that the URL is readily available when the iframe loads, and you avoid unnecessary server calls, keeping the navigation experience smooth and seamless.

If your page modifies the URL while running (possibly adding URL parameters) you may need to also call google.script.url.getLocation to construct the new URL including parameters.

Upvotes: 0

Cooper
Cooper

Reputation: 64042

Try this:

Javascript:

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

GS:

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

Upvotes: 17

Abilogos
Abilogos

Reputation: 5010

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

function teste(){
location.reload();
return false;
}

Upvotes: 0

Related Questions