Rename a column from within Python, error "Property 'NewField.Type' can not be set."
Forums
Suppose I have a simple task, I want to rename a column. My IDEAScript is as follows:
Sub Main
Call ModifyField()
End Sub
Function ModifyField
Set db = Client.OpenDatabase("myfile.IMD")
Set task = db.TableManagement
Set field = db.TableDef.NewField
field.Name = "newname"
field.Description = "field"
field.Type = WI_CHAR_FIELD
field.Equation = ""
field.Length = 1
task.ReplaceField "oldname", field
task.PerformTask
Set task = Nothing
Set db = Nothing
Set field = Nothing
End Function
Now, I want to perform the same task using Python win32com. I tried:
import win32com.client as win32ComClient
wd = os.getcwd()
try:
idea = win32ComClient.Dispatch(dispatch="Idea.IdeaClient")
db = idea.OpenDatabase(os.path.join(wd, 'myfile.IMD'))
task = db.TableManagement()
field = db.TableDef().NewField()
field.Name = "oldname"
field.Description = "description"
field.Type = WI_CHAR_FIELD
field.Equation = ""
field.Length = 1
task.ReplaceField("newname", field)
task.PerformTask
task = Nothing
db = Nothing
field = Nothing
idea = None
finally:
idea = None
I first had some errors on the db.TableManagement() and the db.TableDef().NewField() statement, as I missed the brackets. I also had to change task.ReplaceField "oldname", field to task.ReplaceField("newname", field). This seemed to be fixed, however now I get the error:
AttributeError: Property 'NewField.Type' can not be set.and it marks the line field.Type = WI_CHAR_FIELD. I tried different combinations, like field.Type = "WI_CHAR_FIELD" or field.Type(WI_CHAR_FIELD) but it does not work. So how can I get this working? Thanks for the help!
Dear Brian,
Dear Brian,
once again a big thanks for the fast and good answer!
I now changed it (I put a WI_CHAR_FIELD = 3 statement before the try:) and get no error anymore, however, nothing is changed. Python works and also the imd file is opened in IDEA, but the column does not get renamed?
I closed the file in IDEA before running the code and also refreshed in IDEA, but it just displays the old column name, no change.
Hi Bert, here is an update
Hi Bert, here is an update for you to try:
import win32com.client as win32ComClient
try:
idea = win32ComClient.Dispatch(dispatch="Idea.IdeaClient")
db = idea.OpenDatabase('myfile.IMD')
task = db.TableManagement()
field = db.TableDef().NewField()
field.Name = "NEW_NAME"
field.Description = "description"
field.Type = WI_CHAR_FIELD
field.Equation = ""
field.Length = 1
task.ReplaceField("OLD_NAME", field)
task.PerformTask()
finally:
task = None
db = None
field = None
idea = None
Some problems I found, I
Some problems I found, I removed the path info as when you open IDEA it will automatically be in the current project folder, so you don't really need it or you should probably be using the internal IDEA working folder line. What you had was fine but just not needed in this scenario.
I capitalized the field names. In IDEA the field names are always capitalized and if you give it to them lower case it will capitalize it for you but not always the way you might expect, just best practice to captalize them yourself instead of having IDEA do it for you.
You were missing the () from the PerformTask, that is probably why it wouldn't run.
The Nothing should be changed to None in Python.
I moved all the = None to the finally as this will always run even if the try fails, so you want to make sure anything you open has been properly closed.
The WI_CHAR_FIELD is a
The WI_CHAR_FIELD is a constant but within IDEAScript it knows the value. When you port it over to python, python has no idea what the value of that constant is so you will need to define it. Here are some of them.