使用Ironpython在Spotfire中运行VBA宏

时间:2017-01-19 16:23:58

标签: excel excel-vba ironpython spotfire vba

所以我会尝试在这个帖子IronPython - Run an Excel Macro中询问,但我没有足够的声誉。

所以大致遵循链接中给出的代码,我创建了一些代码,将文件保存到特定位置,然后打开一个存在的工作簿,调用其中的宏,这将执行少量对我下载到.xls的数据进行操作,以使其更具代表性。

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

Spotfire通常不是那么有用,但它让我很少在这里继续。它似乎与.NET有关,但这就是我能说的全部内容。

错误讯息

  

Traceback(最近一次调用最后一次):文件   " Spotfire.Dxp.Application.ScriptSupport",行未知,在   执行ForDebugging文件"",行未知,在   StandardError:目标抛出了异常   调用

剧本

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()

1 个答案:

答案 0 :(得分:1)

是的,所以我在这里回答我自己的问题,但我希望它对某人有所帮助。所以上面的代码,据我所知是完全没问题,但是我的spotfire环境配置方式不能很好。然而,在采取更加宏观的方法之后,我能够找到解决方案。

在spotfire端我有两个输入字段,一个给出文件路径,另一个给出文件名。然后有一个按钮来运行下面的脚本。它们在脚本中连接在一起但是至关重要,因为文件名需要输入到一个单独的文件中,由主宏调用,以便它可以找到文件的文件位置。

从根本上说,我创建了三个xml来实现这个解决方案。第一个是包含所有主要vba内容的主要内容。这将查看另一个xml中的一个文件夹,该脚本填充该文件夹以查找spotfire中输入字段中指定的文件的保存位置。使用自定义函数查找最新文件,它将对相关单元格值运行一系列简单的条件颜色操作。

此脚本填充两个xml文件并告诉主宏运行,该宏中的代码在完成后自动关闭excel。

第三个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运行excel宏,一个解决方案是使用我上面使用的system.dianostics方法,只需将宏设置为自动运行。

相关问题