ironpythonspotfire

Spotfire ironpython replacing data table values


in Spotfire I'm trying to write a string into marked rows of a table column.

from Spotfire.Dxp.Data import *

dataTable = Document.Data.Tables["myTable"]
cursor = DataValueCursor.CreateFormatted(dataTable.Columns["myColumn"])
markings = Document.ActiveMarkingSelectionReference.GetSelection(dataTable)

some_string = "some_string"

for row in dataTable.GetRows(markings.AsIndexSet(),cursor):
    cursor.CurrentValue = some_string

according to this question, it should be possible to write into the table on the cursor position. However, I'm getting the error AttributeError: can't assign to read-only property CurrentValue of type 'DataColumnValueCursor[str]'

What am I doing wrong?


Edit: I used the wrong link, now it's corrected


Solution

  • The cursor value you are retrieving is indeed read only. The example you are referring to seems to be replacing the expression generating a calculated column, rather than specific values in the rows.

    Replacing specific values is more complicated. When you do it without a script, you double-click on a column in a row, then the dialog asks you if you want to replace all rows having that value, or only the specific row. If you want to replace the specific row, you then have to supply a column that contains a unique row identifier. You have to double click on each row you want to change, and repeat the action. As a result, in the data canvas you will have added a transformation operation for each row.

    Similarly in Iron Python, for each row you will have to identify an id column and add a transformation that replaces the original value.

    The following script worked for me (using the iris dataset as an example, plus a calculated 'ID' column that I created with expression RowId())).

    I also needed to make sure that I was appending the transformation to the existing ones (otherwise, in my case, it would have tried to apply the transformation before I had created the ID column).

    from Spotfire.Dxp.Data import *
    from Spotfire.Dxp.Data.Transformations import *
    
    dataTable = Document.Data.Tables["iris"]
    column=dataTable.Columns["Species"]
    id_column=dataTable.Columns["ID"]
    
    cursor = DataValueCursor.CreateFormatted(column)
    id_cursor = DataValueCursor.CreateFormatted(id_column)
    markings = Document.ActiveMarkingSelectionReference.GetSelection(dataTable)
    
    data_operations = 
    dataTable.GenerateSourceView().OperationsSupportingTransformations
    last_data_operation=data_operations[len(data_operations)-1]
    transformations = last_data_operation.GetTransformations()
    
    col_signature = DataColumnSignature(column.Name,DataType.String)
    id_col_signature = DataColumnSignature(id_column.Name,DataType.Integer)
    
    some_string = "some_string"
    
    for row in dataTable.GetRows(markings.AsIndexSet(),cursor,id_cursor):
        value=cursor.CurrentValue
        id_value=int(id_cursor.CurrentValue)
        transformations.Add(ReplaceSpecificValueTransformation(col_signature, 
    value,some_string,\
            [id_col_signature], [id_value],True))
    
    
    last_data_operation.ReplaceTransformations(transformations)