Jump to content
  • List tables and its columns


    This script will output the names and data types of columns for both a specified table and all tables in the document.

    To iterate through the columns of a specific table in Spotfire and print their names and data types, you can use the following code. This script iterates through the columns of a specified table and prints the column names along with their data types.

    # Replace with the actual table name
    table_name = "Data Table"  
    
    # Get the table by name
    table = Document.Data.Tables[table_name]
    
    # Print the table name
    print("Table:", table_name)
    
    # Iterate through the columns and print their names and data types
    for column in table.Columns:
        print(f"{column.Name} ({column.Properties.DataType})")
    
    # Alternatively, iterate through all tables and their columns
    for t in Document.Data.Tables:
        print("Table:", t.Name)
        columnCollection = t.Columns
        for col in columnCollection:
            print("\t", col.Name, "(", col.Properties.DataType, ")")

    Here is an explanation of each part of the script:

    1. Get the Specific Table:
      table_name = "Data Table": Specify the name of the table you want to examine.
      table = Document.Data.Tables[table_name]: Get the table object by its name.
       
    2. Iterate Through the Columns of the Specified Table:
      for column in table.Columns:: Loop through each column in the specified table.
      print(f"{column.Name} ({column.Properties.DataType})"): Print the column name and its data type.
       
    3. Iterate Through All Tables and Their Columns:
      for t in Document.Data.Tables:: Loop through each table in the document.
      print("Table:", t.Name): Print the name of the table.
      columnCollection = t.Columns: Get the collection of columns for the current table.
      for col in columnCollection:: Loop through each column in the current table.
      print("\t", col.Name, "(", col.Properties.DataType, ")"): Print the column name and its data type with proper formatting.

     

    Sample output

    Table: Data Table
    Column1 (String)
    Column2 (Integer)
    Column3 (Real)
    Column4 (DateTime)
    
    Table: Data Table
        Column1 (String)
        Column2 (Integer)
        Column3 (Real)
        Column4 (DateTime)
    
    Table: Other Table
        ColumnA (String)
        ColumnB (Integer)
        ColumnC (Boolean)
    

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...