Jump to content
We've recently updated our Privacy Statement, available here. ×
  • How to append new Rows to an existing Data Table in Spotfire® Using IronPython Scripting


    How to append new Rows to an existing Data Table in Spotfire® Using IronPython Scripting

    Introduction

    How to append new Rows to an existing Data Table in Spotfire® Using IronPython Scripting

    Example

    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
    
    #Create some data with columns that match an existing table to which new rows need to be added. 
    textData = "CarMake,Price,CountryrnMaruti,10000,Indiarn" 
    
    #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.Integer)
    readerSettings.SetDataType(2, 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)
    
    #Note-Memory stream is just used for illustration purpose. The same process can be followed with a URL to a file as well.
     

     

    References

     

    Attachments: Download the file add_rows_datatable from Resources below.

    add_rows_datatable.dxp


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...