Solution:
Below is the script which adds rows to a data table, saves it to the library, then reload it if you're using the same SBDF file in the dashboard.
Please find the sample dashboard attached to the KB Article.
Input Parameters:
Visuals -- Table Visualization
SBDF -- Data Table
from Spotfire.Dxp.Data import AddRowsSettings
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings
textData = "CarMake,Price,Country\r\nMaruti,10000,India\r\n"
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)
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)
settings = AddRowsSettings(Document.ActiveDataTableReference,textDataSource)
Document.ActiveDataTableReference.AddRows(textDataSource,settings)
from System.IO import *
from Spotfire.Dxp.Application.Visuals import VisualContent
from System import Array,Guid,String,Object
from Spotfire.Dxp.Data.Import import *
from Spotfire.Dxp.Framework.Library import LibraryManager, LibraryItemRetrievalOption, LibraryItemType, LibraryItem
vc=Visuals.As[VisualContent]() #Visuals = Script parameter for Table/Cross Table visualization
memStream = MemoryStream();
sWriter = StreamWriter(memStream);
#Exporting the data to Memory Stream
vc.ExportText(sWriter); #exports data in tab separated text
sReader = StreamReader(memStream);
memStream.Seek(0, SeekOrigin.Begin);
textDataSource = TextFileDataSource(memStream);
tables = Document.Data.Tables["ExportData"] # Create dummy exportdata datatable and have the rows in visualization be exported to that table
tables.ReplaceData(textDataSource)
print tables
# uploadFile: the file name to write in the library
# libraryFolder: the full path to the folder to write to in the library
# sourceData: the data table in the analysis to export out
libraryManager = Application.GetService[LibraryManager]()
libraryFolder = "/SBDF"
uploadFile= tables.Name
sourceData= tables.Name
print uploadFile,sourceData
items = libraryManager.Search("type:SBDF and title:"+uploadFile,LibraryItemRetrievalOption.IncludePath)
print "items",items
if (items.Count > 0):
tables.ExportDataToLibrary(items[0], uploadFile)
print "1"
else:
## File Not Found, Searching for Folder to Create File In
items = libraryManager.Search("type:folder",LibraryItemRetrievalOption.IncludePath)
print "2"
for item in items:
print "3"
if (libraryFolder == item.Path):
tables.ExportDataToLibrary(item, uploadFile)
print "4"
SBDF.ReloadLinkedData()
Comments
0 comments
Please sign in to leave a comment.