Setting Document Properties and Executing Scripts when an Analysis opens


By using the ‘History Arrows’ navigation option, which is available starting in 4.0, it is possible to setup a page so that Scripts and Document Properties are loaded before the user starts the analysis process. 

First, when you setup the analysis file, change the page navigation to ‘History Arrows’.

Then, make sure the first page in the analysis includes some information about the analysis file along with an Action Control.   This Action Control can be attached to a Script which can do any of the following tasks:

  • Preset some Document Properties before the users goes into the rest of the analysis
  • Execute additional Scripts that should be executed before the user goes into the rest of the analysis
  • Set the page navigation mode back to ‘tabs’ or ‘step-by-step’ if desired
  • Redirect the user to the next page in the analysis

Let’s assume we have a data set that looks at expense reports. These reports are filed using USD, GBP, EUR , and CAD currency. However, to analyze properly, we would like to convert everything to USD.  It may make sense that you want to convert the rates using the rates for the day the expenses were incurred, but let’s assume you want to load in the current rates at the time you are loading the analysis.   In a previous tip, we explained how to set the exchange rates up as Document properties.  


Those properties are still using static values. What we would like to do is trigger the update of those properties using the current exchange rates.  To do this, we can use the following Script, which pulls the rates down from openexchangerates.org, and then updates the Document Properties with those values.

from System.Net import WebClient
from System.Collections.Generic import Dictionary
from System.IO import StreamReader
from System.Web.Script.Serialization import JavaScriptSerializer

# get the data from openexchangerates
url = 'http://openexchangerates.org/latest.json'
wc = WebClient()
wc.Headers.Add('Content-Type', 'application/x-www-form-urlencoded')
requestData= wc.OpenRead(url)
reader = StreamReader (requestData);
response = reader.ReadToEnd()

#Parse the results to get the exchange rates
js = JavaScriptSerializer()
exchangeRateDict = js.Deserialize(response,object)
gbpRate = float(exchangeRateDict["rates"]["GBP"])
eurRate = float(exchangeRateDict["rates"]["EUR"])
cadRate = float(exchangeRateDict["rates"]["CAD"])

#Set the Document Properties in the Spotfire file with the current rates
Document.Properties["PoundConversion"] = gbpRate
Document.Properties["EuroConversion"] = eurRate
Document.Properties["CADconversion"] = cadRate

In addition, using a previous tip, we can execute additional scripts from within this script  to perform any additional actions we want.

Finally, if you want to set the page navigation back away from ‘history arrows’, you can by using the following code in your script:

Document.Pages.NavigationMode = 0

With 0 setting the navigation to ‘tabs’ and 1 setting the navigation to ‘links’. You can also redirect the user to the next page in the analysis:

Document.ActivePageReference = Document.Pages[1]

Now when the file is opened in the Web Player, the user can only view the first page in the analysis and must click on the ‘Click here to start’ button.

 

 

Clicking on this Action Control  will trigger the Script discussed above, which will then update the exchange rates, change the navigation to tabbed, and then redirect the user to page 2 to start the analysis.


 

If you are intersted in learning more about Script Controls, please consider taking our SP232 Script Controls course. The course was just recently updated, and now includes over 50 separate scripts used for demos or exercises. If you are intersted in registering for this online course, or learning more, please click here.