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.
I am trying to set up a script to play a sound based on a criteria linked with the current price of a company.
For example:
Cell M2 has the formula: googlefinance("LON:AAL","price") and the price is 1003
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.
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.
Reusing the code in How can I play a sound as part of a triggered function
Here's where I am:
Set up the player as a sidebar and have it open when the sheet is opened
/**
* @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);
}
Check every minute if range values meet the criteria and if yes trigger sound alert (I have got a time driven every minute trigger set up for this and I don't mind if it plays repeatedly)
Having read further, I think this check needs to be done at SidebarJavascript code via setInterval and not here.
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 < 39; i++) {
if (value[i][0] < 0.005) {
playSidebar();
}
}
}
If the approach above is correct, I need to define the playSidebar()
function to tell the sidebar to start playing.
Here's the HTML of the sidebar player
<!-- Use a templated HTML printing scriptlet to import common stylesheet -->
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
<div class="sidebar branding-below">
<p>
A little music for your enjoyment!
</p>
<audio id="player" controls >
<source src="http://soundbible.com/mp3/Fire_pager-jason-1283464858.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<div id="sidebar-status"></div>
</div>
<div class="sidebar bottom">
<span class="gray branding-text">Docs Add-on Sound Demo</span>
</div>
When range criteria is fulfilled sidebar to play the sound.
Either via the playsidebar()
function or via sidebarjavascript.html.
SidebarJavascript.html
// 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.
Can you please help me find the code to tell sidebar player to start playing ?
=Googlefinance()
live formula values cannot be retrieved with script. – TheMaster