Refresh data table

Description: Refresh an existing data table.
Assumptions: An optional 'myTable' data table type script parameter.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

if(myTable.IsRefreshable and myTable.NeedsRefresh):
  myTable.Refresh()

Data table transformations

Rename data table

Description: Rename a data table.
Assumptions: An optional 'myTable' data table type script parameter.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

print "Table Name: " + myTable.Name 
myTable.Name = "NewName"
print "Table Name: " + myTable.Name

Remove data table

Description: Remove a data table.
Assumptions: An optional 'myTable' data table type script parameter.
Warning: Backup Spotfire files before removing data tables.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

#Application.Document.Data.Tables.Remove('DeleteMe') #Name method
Application.Document.Data.Tables.Remove(myTable)    #Data table method

Data table row values

Description: Get row values from two columns in a data table.
Assumptions: An optional 'myTable' data table type script parameter.

from Spotfire.Dxp.Data import DataValueCursor, IndexSet

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

rowCount = myTable.RowCount             #Get a count of existing rows
rowsToInclude = IndexSet(rowCount,True) #Include all existing rows

cursorDate = DataValueCursor.CreateFormatted(myTable.Columns["Date"])   #Prep to extract Date column
cursorValue = DataValueCursor.CreateFormatted(myTable.Columns["Value"]) #Prep to extract Value column

for row in myTable.GetRows(rowsToInclude,cursorDate,cursorValue):       #Loop through all existing rows
  myDate = cursorDate.CurrentValue   #Get Date
  myValue = cursorValue.CurrentValue #Get Value
  print myDate, myValue

Data table column and row counts

Description: Get a count of columns and rows in a data table.
Assumptions: An optional 'myTable' data table type script parameter.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

rowCount = myTable.RowCount              #Get a count of rows
columnCount = myTable.Columns.Count      #Get a count of columns
print columnCount, rowCount

Rename data table column

Description: Rename a column in a data table. 
Assumptions: An optional 'myTable' data table type script parameter.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

myColumn = myTable.Columns.Item["Date"]
myColumn.Name = "DateNew"

Remove data table column

Description: Remove a column from a data table.
Assumptions: An optional 'myTable' data table type script parameter.
Warning: Backup Spotfire files before removing columns.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

#Remove by name method
myTable.Columns.Remove("NewColumn3")

#Remove by object
myColumn = myTable.Columns.Item["NewColumn2"] 
myTable.Columns.Remove(myColumn)

Data table column properties

Description: Display data table column properties.
Assumptions: An optional 'myTable' data table type script parameter.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

for c in myTable.Columns:
  print "Column Name: " + c.Name
  print "IsValid: " + str(c.IsValid)
  print "IsVisible: " + str(c.Visible)
  print "DataType: " + str(c.DataType)
  print "Origin: " + c.Properties.GetProperty('Origin')
  print "Expression: " + c.Properties.Item['Expression']
  print "Calcualted Expression: " + c.Properties.Item['CalculatedExpression']
  print "Column Type: " + str(c.Properties.Item['ColumnType'])
  print "Data Type: " + str(c.Properties.Item['DataType'])
  print "Description: " + c.Properties.Item['Description']
  print "External Id: " + c.Properties.Item['ExternalId']
  print "External Name: " + c.Properties.Item['ExternalName']
  print "\r\n" #Print a blank line

Data table row counts for marking groups

Description: Count marked rows in a data table for all marking groups.
Assumptions: An optional 'myTable' data table type script parameter.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

for marking in Document.Data.Markings:
  print "Marking Name: " + marking.Name
  print "Marking Color: " + marking.Color.Name, marking.Color.ToString()
  print "Table Name: " + myTable.Name 
  print "Marked Row Count: " + str(marking.GetSelection(myTable).IncludedRowCount)
  print "\r\n"

Filtered data table records

Description: Display filtered data table records.
Assumptions: An optional 'myTable' data table type script parameter.

#Pick one of the following data table reference methods:
#myTable = Application.Document.Data.Tables['MyDataTable']        #Name method
#myTable = Application.Document.ActiveDataTableReference          #Active data table method
#myTable = Application.Document.Data.Tables.DefaultTableReference #Default data table method
#myTable as a data table type script parameter                    #Script parameter method

filteredRows = Document.ActiveFilteringSelectionReference.GetSelection(myTable).AsIndexSet()

for row in filteredRows:
  for column in myTable.Columns:
    print column.RowValues.GetFormattedValue(row)

Display data table properties

Description: Display all data table properties.

from Spotfire.Dxp.Data import DataPropertyClass #Document = 0, Column = 1, Table = 2

for property in Document.Data.Properties.GetProperties(DataPropertyClass.Table):
  print "Property Name: " + property.Name
  print "Property Description: " + property.Description
  print "Property Value: " + str(property.Value)
  print "\r\n"