0
votes

I am new to IronPython and trying to use the below code to export my cross table from Spotfire to excel.

How do I modify this script to include defining the vizTable?

import System
from System.IO import FileStream, FileMode
from Spotfire.Dxp.Application.Visuals import TablePlot
from  Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import SaveFileDialog  
SaveFile = SaveFileDialog()   #GETS THE FILE PATH FROM THE USER THROUGH A FILEDIALOG
SaveFile.Filter = "xls Format (*.xls)|*.xlsx|*.xls|*.xlsx"
SaveFile.ShowDialog()
saveFilename = SaveFile.FileName
print "saveFilename=", saveFilename
stream = FileStream(saveFilename, FileMode.Create) #Export Table data to the file
vizTable.As[exportWellHeader]().ExportData(DataWriterTypeIdentifiers.ExcelXlsxDataWriter, stream)
stream.Dispose()

I keep getting the below error. I have set a script parameter but do not know where to incorporate into the script.

Error messages:

saveFilename= C:\Users\D565046\Desktop\test 3.xlsx
Traceback (most recent call last):
File "Spotfire.Dxp.Application.ScriptSupport", line unknown, in ExecuteForDebugging
File "<string>", line 14, in <module>
NameError: name 'vizTable' is not defined

Microsoft.Scripting.Runtime.UnboundNameException: name 'vizTable' is not defined
at IronPython.Runtime.PythonContext.MissingName(SymbolId name)
at Microsoft.Scripting.Runtime.LanguageContext.LookupName(CodeContext context, SymbolId name)
at $456##456(Closure , Scope , LanguageContext )
at Spotfire.Dxp.Application.ScriptSupport.IronPythonScriptEngine.ExecuteForDebugging(String scriptCode, Dictionary`2 scope, Stream outputStream)

enter image description here

1

1 Answers

1
votes

I believe two things are happening here. 1) vizTable should be defined in the "script parameters" area below the script, exactly as you have done with exportWellHeader and referencing the visualization (cross table) you want to export. 2) I believe line 14 is incorrect and you should not have something like exportWellHeader. I think it should read more like: vizTable.As[CrossTablePlot]().ExportText(DataWriterTypeIdentifiers.ExcelXlsxDataWriter, stream)

Source: http://easyspotfire.blogspot.com/2014/11/export-cross-table-visualization-to-file.html

Please note that in the referenced link, the script author is exporting data to a text file, rather than an Excel file.