Live updating google web app from a google sheet

379 views
Skip to first unread message

Gary Horan

unread,
Aug 14, 2021, 9:35:14 PM8/14/21
to Google Apps Script Community
I currently have a google sheet with a series of cells containing the status of various sensors. I also have a google webapp that can read the contents of the google sheet / format the contents and display it on a webpage.

I want my javascript to regularly interrogate the sheet and display up to date information on my webpage. I initially thought I could use a javascript setInterval and just read the sheet every 10 seconds or so, but that doesn't seem to be permitted.

Does anyone have any suggestions on how this could be done? Ideally I guess I would like a javascript to be run whenever there is a change to a cell on the sheet that updates my page. If that is not possible then how do I set up an interval where I can periodically check the page for changes?

Laurie Nason

unread,
Aug 15, 2021, 12:40:45 AM8/15/21
to google-apps-sc...@googlegroups.com
HI Gary,
Have you looked at installed triggers?
Good overview of triggers are here
Laurie

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/3e8d0cc3-6e07-4ce8-b023-e079e50a319an%40googlegroups.com.


--

Laurie Nason 

The KAUST School – Operational Analyst

Information Technology Support
Deep in the Bowels of the School
Landline: +966-12-808-6853

Clark Lind

unread,
Aug 15, 2021, 11:13:30 AM8/15/21
to Google Apps Script Community
Hi Gary,
I have a webapp that checks my gmail for any new messages every 5 minutes and grabs them if they exist. This should work (in theory) with sheets too. The main problem I ran into was creating too many timers. So I delete all running timers every time I make a new call. My question is, do you truly need to call the data every 5-10 seconds? Is it critical for safety of life/property? Otherwise, I would decrease how often you call the data if it isn't truly critical. That will help your performance.  Here is a sample of my webapp (it is stand alone, not container-bound) 

front-end code:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>   //yes, I still find jQuery very useful! lol

<script>   
    
  $(function () {
    //clear all running timers
    for (var i = 1;  i <  99; i++) {
      window.clearInterval(i);
    }

    window.setInterval('onLoad()', 300000); //set 5 minute timer
    onLoad(); //run once initially
  });


function onLoad() {
    google.script.run
       .withSuccessHandler(function(contents) {
            // Respond to success conditions here.
            updateDisplay(contents);
          })
       .withFailureHandler(function(msg) {
            // Respond to failure conditions here.
            $('#main-heading-left').text(msg);
            $('#main-heading-left').addClass("error");
            $('#error-message').show();
          })
       .getUnreadEmail();  //this function is in the code.gs file on the backend
  };

function updateDisplay(contents) { 
    //this function modifies the current html file
}


Back-end code (in Code.gs) called with google.script.run

async function getUnreadEmail() {
var filter = "is:unread";
var threads = await GmailApp.search(filter);

for (var i = 0; i < threads.length; i++) {
  if (threads[i].isUnread()) {
    var messages = threads[i].getMessages();
    var message = messages[messages.length -1]

    response.push(['email', message.getId(), message.getFrom(), threads[i].getFirstMessageSubject(), threads[i].getPermalink(), message.getBody()]);
    }
 
    return response;
 }

Basically, I wanted a simplified Inbox with additional features Gmail doesn't have, and without some features it does! :)
Reply all
Reply to author
Forward
0 new messages