Omniscope allows you to write your own JavaScript to extend Omniscope's functionality in several ways. The current scripting language is JavaScript, and in the case of the DataManager Custom Script block, and the DataExplorer Content View this is 'pure' Javascript without any of the browser-specific objects and methods typically seen in a web page. Scripting inside the Omniscope Web View using browser-friendly JavaScript is discussed here [1].
Using the SCRIPT [2] formula function, you can execute Javascript and invoke formula functions directly from your JavaScript. The last value in your Javascript is used as the return value of the SCRIPT function.
For example:
DEPENDENCIES( SCRIPT(`
// Running total of Coupon field for all Categories up to the current Category: var array = subset_uniquesList("Category"); var total = 0; var curval = refVal("Category"); for (var i in array) { var val = array[i]; total += subset_sum("Coupon", subset("Category", val)); if (val==curval) break; // Stop here } // Result value: total; `), /* list of fields referenced by script: */ [Category], [Coupon] )
For a full list of supported functions, look for those with "In script" shown in the Functions Guide [3]. Also see here [4] for further JavaScript-specific functions. Note also the use of back-quotes in formulae [5], making it easier to use regular quotes inside the script.
This SCRIPT formula function can be used in the Content View, as measures in the new Bar/Line view, and also in formula fields. If used in formula fields, it is essential to identify any referenced fields inside your script by wrapping the SCRIPT inside a DEPENDENCIES [6] function as shown above. Without this, Omniscope cannot determine the correct calculation sequence.
By editing the page source and using <# #> fragments, you can insert preprocessing commands into your HTML source code. You might use this to create data-driven repeated sections, conditional sections, or entirely new content by writing arbitrary text to the output.
For example:
<##>
You can also write directly to the output. For example:
<#
out.println("<br>"); // add linebreak
out.println("Records: "+recordCount());
#>
This is much like JSP or ASP, with the HTML document effectively preprocessed by the script "server-side" (the "server" being the Content View's data engine, not actually a separate server).
You can also use this within any rich text field supporting formulae, after switching to source mode (edit, and press the "<>" toolbar button):
"Scripting in formulae [7]" Forums discussion
"Scripting in Content View [8]" Forums discussion
"Scripting with arrays of data [9]" Forums discussion
Note: This type of scripting should not be confused with browser scripting within an embedded web browser.
Many standard formula functions are also available for direct use in JavaScript. These are documented in the Functions Guide [3]. Look for "In scripts" in the Usage column.
All functions behave identically to regular formula functions, as documented, with the exception of subset_uniquesList() which returns a Javascript array rather than a comma-separated String. Functions that are not supported can be accessed using formula() - see below - or by requesting it to be upgraded by posting on the forums.
The following functions are not in the functions guide, since they are only available in JavaScript:
formula("IF(RAND<0.5, true, false)")
Dynamically evaluates an arbitrary formula using normal formula syntax. This supports all functions in the functions guide, but you must build up a valid formula as a Javascript String.
dataArray() or dataArray(fields)
or dataArray(fields, rowSubset) or dataArray(fields, rowSubset, includeFieldNames)
For example: dataArray( ["Field 1", "Field 2"], subset("Region", "Europe"), true )
Returns a two-dimensional array of cell data, structured as [column][row], indexed from 0 to n-1. See example [9]. If includeFieldNames is true, the first 'row' will be the field names (the first entry in each column array).
The Omniscope Web Views are browsers that allow browser-friendly scripts in the page content, executed in the system default native browser and embedded within Omniscope, to access certain Omniscope variables, through a global JavaScript object named 'omniscope'. Currently, the current file URL can be retrieved through this mechanism.
This object is only made available after the page has loaded. You should not access it on page load or earlier, and should use a timeout to ensure the value has been set.
The global object is currently as follows:
omniscope = {"currentFileURL" : "http://....."}
omniscope.currentFileURL : represents the current Omniscope IOK file URL
Links:
[1] http://kb.visokio.com/kb/provide-omniscope-object-to-script
[2] http://kb.visokio.com/kb/functions-guide#SCRIPT
[3] http://kb.visokio.com/kb/functions-guide
[4] http://kb.visokio.com/kb/js-functions-guide
[5] http://forums.visokio.com/discussion/comment/3436/#Comment_3436
[6] http://kb.visokio.com/kb/functions-guide#DEPENDENCIES
[7] http://forums.visokio.com/discussion/1073/scripting-in-formulae-2.7-enhancements/p1
[8] http://forums.visokio.com/discussion/1072/scripting-in-content-view-2.7/p1
[9] http://forums.visokio.com/discussion/1656/scripting-with-arrays-of-data-2.8/p1