1

Long time user very first question:

I am trying to update the formula in a calculated column in Spotfire using ironpython. I have a table called tbl_test with calculated column 'Biz_rule'. I create a string called str_statement and try to use it to replace the expression in the caluclated column with the code below:

calc_col = tbl_test.Columns.Item['Biz_rule']
calc_col = calc_col.As<CalculatedColumn>()
calc_col.Expression = str_statement;

I get the following error.

Traceback (most recent call last):
  File "Spotfire.Dxp.Application.ScriptSupport", line unknown, in ExecuteForDebugging
  File "<string>", line 25, in <module>
  AttributeError: 'bool' object has no attribute 'Expression'

I assume because

calc_col.As<CalculatedColumn>() 

returns a boolean indicating whether or not the operation succeeded. How do I get an instance of the calculated column so I can update the expression?

2 Answers2

1
tbl_test = Document.Data.Tables['data source scott']
calc_col = tbl_test.Columns.Item['Job Job']
calc_col.Properties.SetProperty('Expression','[JOB]')

The above code works for me. Is it what you tried to do ? The question is why did you use

'calc_col.As<CalculatedColumn>()' 

?

Jacek Sierajewski
  • 613
  • 1
  • 11
  • 33
  • That worked like a charm! I just didn't realize that I could access the Expression property without the object being a 'CalculatedColumn' since tbl_test.Columns.Item[] returns a DataColumn. I was basically trying to typecast using As . Thank you very much! – Luis Zuleta Benavides Jan 04 '16 at 16:45
0

I first try to update an existing calculation. If that fails I try to delete the column 1st (if it's frozen), then I add a new column with the calculation.

The basic of the replace is: lc_myField = ldt_tbl.Columns['Column Name'].AsCalculatedColumn lc_myField.Expression = ls_CalcExpression

The basics of the add are: ldt_tbl.Columns.AddCalculatedColumn('Column Name', ls_CalcExpression)

where the ldt_tbl is the data table, and ls_calcExpression contains your expression.