簡體   English   中英

使用Ironpython在Spotfire中運行VBA宏

[英]Run a VBA macro in Spotfire using Ironpython

因此,我嘗試在該線程中進行詢問-IronPython-運行Excel宏,但我的信譽不足。

因此,大致按照鏈接中給出的代碼,我創建了一些代碼,該代碼將文件保存到特定位置,然后打開一個存在於其中的工作簿,調用其中的宏,這將對我所處理的數據執行少量操作下載到.xls,以使其更加美觀。

現在,我將問題隔離到代碼的這一特定部分(如下)。

通常,Spotfire的信息不多,但在這里卻很少。 似乎與.NET相關,但這僅是我能說的。

錯誤訊息

追溯(最近一次呼叫最近):文件“ Spotfire.Dxp.Application.ScriptSupport”,行未知,在ExecuteForDebugging中文件“”,行未知,在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()

是的,所以我在這里回答自己的問題,但希望對您有所幫助。 因此,據我所知,上面的代碼非常完美,但是與我的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方法,只需將您的宏設置為自動運行即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM