Search code examples
javascriptaudiogoogle-apps-scriptgoogle-sheets

Google Script: Play Sound when a specific cell change the Value


Situation:

Example Spreadsheet

Sheet: Support
Column: H has the following function "=IF(D:D>0;IF($B$1>=$G:G;"Call";"In Time");" ")" that changes the value depending on the result.

Problem:

I need to:

  1. Play a sound when a cell in column H changes to "Call" on the sheet "Support".
  2. This function will need to run every 5min.
  3. Does the sound need to be uploaded to Drive or can I use a sound from a URL?

I will appreciate to anyone can help on it... I see a lot of code but I didn't understand very well.


Solution

  • This is a pretty tough problem, but it can be done with a sidebar that periodically polls the H column for changes.

    Code.gs

    // creates a custom menu when the spreadsheet is opened
    function onOpen() {
      var ui = SpreadsheetApp.getUi()
        .createMenu('Call App')
        .addItem('Open Call Notifier', 'openCallNotifier')
        .addToUi();
    
      // you could also open the call notifier sidebar when the spreadsheet opens
      // if you find that more convenient
      // openCallNotifier();
    }
    
    // opens the sidebar app
    function openCallNotifier() {
      // get the html from the file called "Page.html"
      var html = HtmlService.createHtmlOutputFromFile('Page') 
        .setTitle("Call Notifier");
    
      // open the sidebar
      SpreadsheetApp.getUi()
        .showSidebar(html);
    }
    
    // returns a list of values in column H
    function getColumnH() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Support");
    
      // get the values in column H and turn the rows into a single values
      return sheet.getRange(1, 8, sheet.getLastRow(), 1).getValues().map(function (row) { return row[0]; });
    }
    

    Page.html

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
      </head>
      <body>
        <p id="message">Checking for calls...</p>
    
        <audio id="call">
          <source src="||a URL is best here||" type="audio/mp3">
          Your browser does not support the audio element.
        </audio>
    
        <script>
        var lastTime = []; // store the last result to track changes
    
        function checkCalls() {
    
          // This calls the "getColumnH" function on the server
          // Then it waits for the results
          // When it gets the results back from the server,
          // it calls the callback function passed into withSuccessHandler
          google.script.run.withSuccessHandler(function (columnH) {
            for (var i = 0; i < columnH.length; i++) {
    
              // if there's a difference and it's a call, notify the user
              if (lastTime[i] !== columnH[i] && columnH[i] === "Call") {
                notify();
              }
            }
    
            // store results for next time
            lastTime = columnH;
    
            console.log(lastTime);
    
            // poll again in x miliseconds
            var x = 1000; // 1 second
            window.setTimeout(checkCalls, x);
          }).getColumnH();
        }
    
        function notify() {
          document.getElementById("call").play();
        }
    
        window.onload = function () {
          checkCalls();
        }
    
        </script>
      </body>
    </html>
    

    Some sources to help: