使用Ironpython在Spotfire中运行VBAmacros

所以我会尝试在这个线程中询问IronPython – 运行一个Excelmacros,但我没有足够的声誉。

因此,粗略地遵循链接中给出的代码,我创build了一些代码,将文件保存到一个特定的位置,然后打开一个存在的工作簿,调用其中的macros,这将执行less量的数据处理,我下载到.xls,使其更具有代表性。

现在我已经把问题隔离到代码的这个特定部分(下面)。

Spotfire通常不是那种提供信息的,但是让我很less去这里。 这似乎是与.NET有关的东西,但这是我所能说的。

错误消息

Traceback(最近调用的最后一个):文件“Spotfire.Dxp.Application.ScriptSupport”,未知行,在ExecuteForDebugging File“”中,未知行,在StandardError:Exception中被调用的目标抛出。

剧本

from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers from System.IO import File, Directory import clr clr.AddReference("Microsoft.Office.Interop.Excel") import Microsoft.Office.Interop.Excel as Excel excel = Excel.ApplicationClass() excel.Visible = True excel.DisplayAlerts = False workbook = ex.Workbooks.Open('myfilelocation') excel.Run('OpenUp') excel.Run('ActiveWorkbook') excel.Run('DoStuff') excel.Quit() 

对,所以我在这里回答我自己的问题,但我希望这有助于某人。 所以上面的代码,据我所知是非常好的,但没有配合我的spotfire环境configuration的方式。 然而,在采取更macros观的方法之后,我能够find一个解决scheme。

在spotfire结束我有两个input字段,一个给出文件path,另一个文件名。 然后有一个button来运行下面的脚本。 这些在脚本中联合在一起,但是至关重要,因为文件名需要被input到单独的文件中,被主macros调用,以便它能够find文件的文件位置。

基本上我创build了三个XML来实现这个解决scheme。 第一个是主要包含所有主要vba的东西。 这将查看另一个xml中的文件夹,该脚本填充该文件以查找在spotfire中的input字段中指定的文件的保存位置。 使用自定义函数查找最近的文件,它将针对有问题的单元格值运行一系列简单的条件颜色操作。

这个脚本填充两个xml文件,并告诉主macros运行,macros完成后的代码自动closuresexcel。

第三个XML是一系列的颜色规则,所以用户可以根据他们的仪表板自定义它们。 这为定制提供了一些灵活性。 没有必要,但一些用户的潜在要求,所以我决定击败他们的一拳。

无论如何,代码如下。

 from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers from System.IO import File, Directory import clr clr.AddReference("Microsoft.Office.Interop.Excel") import Microsoft.Office.Interop.Excel as Excel from Spotfire.Dxp.Data.Export import * from Spotfire.Dxp.Application.Visuals import * from System.IO import * from System.Diagnostics import Process # Input field which takes the name of the file you want to save name = Document.Properties['NameOfDocument'] # Document property that takes the path location = Document.Properties['FileLocation'] # just to debug to make sure it parses correctly. Declaring this in the script # parameters section will mean that the escape characters of "\" will render in a # unusable way whereas doing it here doesn't. Took me a long time to figure that out. print(location) # Gives the file the correct extension. # Couldn't risk leaving it up to the user. newname = name + ".xls" #Join the two strings together into a single file path finalProduct = location + "\\" + newname #initialises the writer and filtering schema writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsDataWriter) filtering = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet() # Writes to file stream = File.OpenWrite(finalProduct) # Here I created a seperate xls which would hold the file path. This # file path would then be used by the invoked macro to find the correct folder. names = [] for col in table.Columns: names.append(col.Name) writer.Write(stream, table, filtering, names) stream.Close() # Location of the macro. As this will be stored centrally # it will not change so it's okay to hardcode it in. runMacro = "File location\macro name.xls" # uses System.Diagnostics to run the macro I declared. This will look in the folder I # declared above in the second xls, auto run a function in vba to find the most # up to date file p = Process() p.StartInfo.FileName = runMacro p.Start() 

长话短说:从spotfire运行excelmacros的一个解决scheme是使用我以上使用的system.dianostics方法,只需将您的macros设置为自动运行。