title: Play a sound based on a criteria linked with Googlefinance formulae on Google Sheets
body: <p>I have a list of companies in Google Sheets and I am pulling their stock market price with a Googlefinance formula (e.g. googlefinance("LON:AAL","price")). This formula automatically updates the prices without manual intervention.</p>
  
  <p>I am trying to set up a script to play a sound based on a criteria linked with the current price of a company. </p>
  
  <p>For example:</p>
  
  <p>Cell M2 has the formula: googlefinance("LON:AAL","price") and the price is 1003</p>
  
  <p>When googlefinance formulae is auto updated, the price changes to 1050. 
  1050 is above the threshold I set which is 1040. Therefore Google sheets plays a sound when the price is updated to 1050.</p>
  
  <p>I'd like to set this up for a column of data which is all updated by googlefinance formulas. Any one of them meets the threshold criteria, I'd like to hear a sound.</p>
  
  <p>Reusing the code in
  <a href="https://stackoverflow.com/questions/26787307/how-can-i-play-a-sound-as-part-of-a-triggered-function">How can I play a sound as part of a triggered function</a> </p>
  
  <p>Here's where I am:</p>
  
  <p><strong>Set up the player as a sidebar and have it open when the sheet is opened</strong></p>
  
  <pre><code>/**
   * @OnlyCurrentDoc  Limits the script to only accessing the current spreadsheet.
   */
  
  var SIDEBAR_TITLE = 'Sidebar Musicbox';
  
  /**
   * Adds a custom menu with items to show the sidebar and dialog.
   * @param {Object} e The event parameter for a simple onOpen trigger.
   */
  function onOpen(e) {
    SpreadsheetApp.getUi()
        .createAddonMenu()
        .addItem('Show sidebar', 'showSidebar')
        .addToUi();
    showSidebar();
  }
  
  /**
   * Runs when the add-on is installed; calls onOpen() to ensure menu creation and
   * any other initializion work is done immediately.
   *
   * @param {Object} e The event parameter for a simple onInstall trigger.
   */
  function onInstall(e) {
    onOpen(e);
  }
  
  /**
   * Opens a sidebar. The sidebar structure is described in the Sidebar.html
   * project file.
   */
  
  function showSidebar() {
    var ui = HtmlService.createTemplateFromFile('Sidebar')
        .evaluate()
        .setSandboxMode(HtmlService.SandboxMode.IFRAME)
        .setTitle(SIDEBAR_TITLE);
    SpreadsheetApp.getUi().showSidebar(ui);
  }
  </code></pre>
  
  <p><strong>Check every minute if range values meet the criteria and if yes trigger sound alert</strong>
  (I have got a time driven every minute trigger set up for this and I don't mind if it plays repeatedly)</p>
  
  <p>Having read further, I think this check needs to be done <strong><em>at SidebarJavascript</em></strong> code via setInterval and not here.</p>
  
  <pre><code>function checkrange() {
    // Trigger set up for every minute
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('23.05.2015');
    var value = ss.getRange("M2:M40").getValues();
    for (var i = 0; i &lt; 39; i++) {
    if (value[i][0] &lt; 0.005) {
    playSidebar();
    }
    } 
    }
  </code></pre>
  
  <p>If the approach above is correct, I need to define the <code>playSidebar()</code> function to tell the sidebar to start playing.</p>
  
  <p><strong>Here's the HTML of the sidebar player</strong></p>
  
  <pre><code>&lt;!-- Use a templated HTML printing scriptlet to import common stylesheet --&gt;
  &lt;?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?&gt;
  
  &lt;div class="sidebar branding-below"&gt;
      &lt;p&gt;
      A little music for your enjoyment!
      &lt;/p&gt;
      &lt;audio id="player" controls &gt;
        &lt;source src="http://soundbible.com/mp3/Fire_pager-jason-1283464858.mp3" type="audio/mpeg"&gt;
        Your browser does not support the audio element.
      &lt;/audio&gt;
      &lt;div id="sidebar-status"&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="sidebar bottom"&gt;
    &lt;span class="gray branding-text"&gt;Docs Add-on Sound Demo&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>When range criteria is fulfilled sidebar to play the sound.</strong></p>
  
  <p>Either via the <code>playsidebar()</code> function or via sidebarjavascript.html.</p>
  
  <p><strong>SidebarJavascript.html</strong></p>
  
  <pre><code>// setInterval to check the range values every minute and play when the criteria is fulfilled. 
  // No need for the checkrange() function above if the check needs to be done at sidebarjavascript.
  </code></pre>
  
  <p>Can you please help me find the code to tell sidebar player to start playing ? </p>