Jump to content

Saving input field text with special characters to data table


Scott Thompson 5

Recommended Posts

I am using a text area to create a form for user input fields and a control button to save/append the data table: I created two string data type input fields for 'Rank' and 'Comment' document properties. I have used the script below applied to a control button to append user input to a data table. The script runs fine unless the text entered into the input fields contains a comma, if present the text entered with commas ends up split over a number of columns. For my end use, the user input to 'Comments" in particular will have special characters and I would like to store the entire comment (with special characters) in one column.

Control buttton script :

from Spotfire.Dxp.Data import AddRowsSettings

from System.IO import StreamWriter, MemoryStream, SeekOrigin

from Spotfire.Dxp.Data import DataType, DataTableSaveSettings

from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings

x = Document.Properties['Rank']

y =Document.Properties['Comment']

print(x,y)

#Create some data with columns that match an existing table to which new rows need to be added. 

textData = "Rank,Comment,rn"+x+","+y+"rn" 

print(textData)

#Using a Memory Stream as a placeholder to write the columns to which can then be used to make a data source

stream = MemoryStream()

writer = StreamWriter(stream)

writer.Write(textData)

writer.Flush()

stream.Seek(0, SeekOrigin.Begin)

#Settings to tell the system the data types being imported.Here it is defined as a comma separated list of column index and their data types

readerSettings = TextDataReaderSettings()

readerSettings.Separator = ","

readerSettings.AddColumnNameRow(0)

readerSettings.SetDataType(0, DataType.String)

readerSettings.SetDataType(1, DataType.String)

textDataSource = TextFileDataSource(stream,readerSettings)

#Use settings here to automatically have the system match column names between the data table and the memory data source created above.

settings = AddRowsSettings(Document.ActiveDataTableReference,textDataSource)

#Add the rows from the datasource.

Document.ActiveDataTableReference.AddRows(textDataSource,settings)

Link to comment
Share on other sites

You could try to set the separator to something other than comma, something you know does not appear in the Comment text. I suspect your real data is more complex than this example, but you could try a separator you know for sure would not appear in the data.

So basically define:

mySeparator='WEIRDSEPARATOR' #anything you like here

textData = "Rank"+mySeparator+"Comment"+"rn"+str(x)+mySeparator+y+"rn" 

...

readerSettings.Separator = mySeparator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...