Scripting

Scripting-Adding JavaScript to Omniscope

Using your own scripting code 

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.

Scripting in formulae using SCRIPT function

Using the SCRIPT 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. Also see here for further JavaScript-specific functions.  Note also the use of back-quotes in formulae, 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 function as shown above.  Without this, Omniscope cannot determine the correct calculation sequence.

Script Pre-processing in the Content View

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:

<#
  if (x=5) {
    y = 3;
  } else {
    y = 2;
  }
  out.println("Write some text into the document, programmatically");
  var array = subset_uniquesList("Category");
  for (var i in array) {
    
#>
    <b>Some text and HTML with <#=array[i]#> (inserted values)</b>
    <#
  }

#> 

 

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):

  • Content View
  • Document layout
  • Page footer
  • View footer
  • Email message body ("dynamic content" option)

See also 

"Scripting in formulae" Forums discussion

"Scripting in Content View" Forums discussion

"Scripting with arrays of data" Forums discussion