2017-01-19 18 views
0

그래서이 스레드에서 물어보십시오. IronPython - Run an Excel Macro하지만 충분한 평판이 없습니다.IronPthon을 사용하여 Spotfire에서 VBA 매크로 실행

대략적으로 링크에 주어진 코드를 따라 특정 위치에 파일을 저장하는 코드를 만든 다음 거기에있는 통합 문서를 열고 매크로를 호출하여 작은 양의 조작을 수행합니다. .xls로 다운로드 한 데이터를 더보기 좋게 만들 수 있습니다.

이제 코드의이 특정 부분 (아래)에서 문제를 격리했습니다.

Spotfire는 일반적으로 유익하지 않지만 여기에 대해서는 거의 알려주지 않습니다. .NET과 관련된 것으로 보이지만 그건 내가 말할 수있는 전부입니다.

Traceback (most recent call last): File "Spotfire.Dxp.Application.ScriptSupport", line unknown, in ExecuteForDebugging File "", line unknown, in StandardError: Exception has been thrown by the target of an invocation.

스크립트

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

매크로에서 무엇을하고 있습니까? 프로세스에서 모두 제거 할 수 있습니다. http://xyproblem.info/ – scsimon

+0

답장을 보내 주셔서 감사합니다. 매크로는 불행하게도 필요합니다. 적어도 나는 그렇다고 믿습니다. 기본적으로 spotfire의 조건부 채색을 유지하는 Excel 테이블을 내보내려고합니다. 내가 생각했던 간단한 요청. 잠재적으로 html 테이블로 변환 한 다음 HTML에서 조건부 컬러링을 사용하기 때문에 거기에서 Excel로 내보내 보았습니다. html/spotfire/excel 솔루션이 좋지 않았습니다.그래서 저는 저에게 필요한 작업을 수행 할 수있는 중앙화 된 매크로를 가지고 있다고 생각하고 있습니다. 그렇게하면 여러 대시 보드에 푸시 될 수 있습니다. @scsimon –

+0

@scsimon 어느 쪽이든 나는 spotfire 내에서 Excel의 기능에 액세스 할 수 있다는 것이 좋을 것이라고 생각합니다. 테이블을 내보낼 잠재력을 상상해보십시오. 중요한 테이블 작업을 수행 한 다음 해당 데이터와의 실제 연결에서 해당 작업을 잠재적으로 다시 불러 들일 수 있습니다. 그것은 spotfire의 기능에 또 다른 레이어를 추가 할 것입니다. 일상적인 사용을위한 것은 아니지만 ... –

답변

1

오른쪽, 그래서 내가 여기에 내 자신의 질문에 대답 해요하지만 난 그게 누군가 도움이되기를 바랍니다 오류 메시지입니다. 위의 코드는 내가 아는 한 완벽하게 괜찮 았지만 스폿 파이어 환경이 구성된 방식으로는 잘 작동하지 않았습니다. 그러나 훨씬 더 무겁게 접근 한 후에 해결책을 찾을 수있었습니다.

spotfire 끝에서 필자는 두 개의 입력 필드, 하나는 파일 경로를 제공하고 다른 하나는 파일 이름을 제공합니다. 그런 다음 아래 스크립트를 실행하는 버튼이 있습니다. 이 파일은 스크립트에서 함께 결합되지만 파일 이름을 별도의 파일에 입력해야하므로 파일의 파일 위치를 찾을 수 있도록 주 매크로에서 호출해야하므로 완전히 구분됩니다.

기본적으로 나는이 솔루션을 달성하기 위해 세 개의 xml을 만들었습니다. 첫 번째는 주요 vba의 모든 것을 포함하는 주된 것이 었습니다. 그러면이 스크립트가 spotfire의 입력 필드에 지정된 파일의 저장 위치를 ​​찾기 위해 채우는 다른 xml의 폴더가 검색됩니다. 커스텀 메이드 함수를 사용하여 가장 최근의 파일을 찾으면 문제의 셀 값에 대한 간단한 일련의 조건부 컬러 연산을 실행합니다.

이 스크립트는 두 개의 XML 파일을 채우고 주 매크로가 실행되도록 지시합니다. 매크로가 닫히면 자동으로 닫힙니다.

세 번째 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() 

긴 이야기 : 하나 개의 솔루션이 자동 실행에 매크로 세트가 내가 위에서 간단하게 사용하는 system.dianostics 방법을 사용하는 것입니다 Spotfire의에서 엑셀을 매크로의를 실행합니다.