08/11/2014

[Spotfire] Get data from marking via script

Should you need to get data from a marking while inside an IronPython script in Spotfire, you may try:

 from Spotfire.Dxp.Data import *  
 from System import String,Object  
    
 markingName = 'some_name' #can also be passed as parameter!  
 DataTableSelection = 'some_table' #can also be passed as parameter!  
    
 #get marked rows  
 rowMask = Document.Data.Markings[markingName].GetSelection(DataTableSelection)  
    
 #get cursor on the marking for the values we want  
 cursor = DataValueCursor.CreateFormatted(DataTableSelection.Columns["some_column"])  
    
 #cycle on our cursor and do something with the values  
 for row in DataTableSelection.GetRows(rowMask.AsIndexSet(),cursor):  
         print cursor.CurrentValue  


Remember that all inputs can be passed as parameters to our script!

4 comments:

  1. can we assign this value to a property control or lets say create an array out of these values??

    ReplyDelete
    Replies
    1. Well, inside the for loop you can assign the cursor.CurrentValue to whatever other variable you want, so if you have a property control based on a variable (Document Property), you could assign the value from the marking to that variable. Of course it would only register the last value in the set though.

      As for the array, I'm unsure. I remember creating arrays up to version 7 was not as easy as one would think but again, if you have declare an array Document Property, then you should be able to do use it as well; maybe use a counter and increase it each round before assigning the new value. Also remember to completely empty the array before the loop each time, otherwise you might end up with inconsistent data

      Delete
    2. I know this was posted long ago but for others that find this page It is best to create a ironpython array beforehand then append the cursor.CurrentValue to it,
      Here is the code with the array incorporated:
      from Spotfire.Dxp.Data import *
      from System import String,Object

      markingName = 'Marking_Name' #can also be passed as parameter!
      DataTableSelection = Document.Data.Tables['Table_Name'] #can also be passed as parameter!

      array = [] # <--- Empty Python List[]
      #get marked rows
      rowMask = Document.Data.Markings[markingName].GetSelection(DataTableSelection)

      #get cursor on the marking for the values we want
      cursor = DataValueCursor.CreateFormatted(DataTableSelection.Columns["some_column"])

      #cycle on our cursor and do something with the values
      for row in DataTableSelection.GetRows(rowMask.AsIndexSet(),cursor):
      val = cursor.CurrentValue
      array.append(val)

      print array

      Delete
    3. Thank you for the update :)

      Delete

With great power comes great responsibility.

Da grandi poteri derivano grandi responsabilità.