0

I created a google web app that gets information from a google spreadsheet. If I have the web app opened in a browser, then go make a change in the spreadsheet, within the webpage, can I display some text that the page needs to be refreshed?

bigr1822
  • 27
  • 2
  • You want to read about [polling](https://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet?rq=1) and `window.setTimeout`/`window.setInterval`. There's nothing on the Google server side that can "push" info to the client, the client has to ask for the information. – dwmorrin Aug 09 '19 at 23:44

1 Answers1

0

A simple Polling Example:

Just copy the code and run start polling. Click the Start Polling button. Change the value in A1 of the active sheet and watch the sidebar.

function startPolling() {
  var html='<div id="msg"></div><input type="button" value="Start Polling" onClick="startPolling();" />';
  html+='<script>function startPolling(){setInterval(getA1,5000);}function getA1(){google.script.run.withSuccessHandler(function(hl){document.getElementById(\'msg\').innerHTML=hl;}).getA1();}</script>';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showSidebar(userInterface); 
}

function getA1() {
  //SpreadsheetApp.getActive().toast('Polling');//helpful for debugging
  return SpreadsheetApp.getActiveSheet().getRange('A1').getValue();
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thanks for your help! I’ll give this code a shot and see what happens, then let you know what I find out. Thanks again! – bigr1822 Aug 11 '19 at 02:06
  • Something is wrong with the code, and I am not sure how to fix it. Secondly, does this get put into the HTML or the Code.gs – bigr1822 Aug 11 '19 at 11:40
  • Both functions get put in the Code.gs file. I tested this before I gave it to you. It works. Follow the instructions. – Cooper Aug 11 '19 at 11:49
  • if I just copy and paste it into the code.gs, I get an error. I obviously have something different. Sorry, I’m working on another project. I haven’t look at the code very closely. I’m really new to this stuff but I’m sure I’ll figure it out. – bigr1822 Aug 11 '19 at 12:31
  • What's the error? The error code be from other code that's not formatted properly. – Cooper Aug 11 '19 at 14:19
  • @bigr1822: If you want to deploy your script as a Web App and use the polling in the browser, rather than in a Spreadsheet side bar - do the following. 1. rename the `startPolling() function` to `doGet() function` 2. Replace `SpreadsheetApp.getUi().showSidebar(userInterface);` by `return userInterface;` 3. Deploy the script as a Web App 4. Open the web App URL in a browser window, click on 'Start Polling', see value from cell `A1` 5. Change the value in `A1` and observe how the output in the browser updates. Note: `getActiveSheet()` works only if the script is bound to the Spreadsheet. – ziganotschka Aug 12 '19 at 11:15
  • A Sidebar still runs on the browser – Cooper Aug 12 '19 at 12:20
  • @ziganotschka can you have two doGet() functions? I assume I need to put this into that function if I already have one? I did put it in my doGet() towards the top, and I do see the "Start Polling" button now, but do not see anything else. All my HTML isn't being displayed. – bigr1822 Aug 12 '19 at 12:32
  • No, if you already have a doGet function(), please incorporate the startPolling code into it, rather than defining a second function. – ziganotschka Aug 12 '19 at 12:44