1
votes

So I would try ask over in this thread IronPython - Run an Excel Macro but I don't have enough reputation.

So roughly following the code given in the link I created some code which would save a file to a specific location, then open a workbook that exists there, calling the macro's within it, which would perform a small amount of manipulation on the data which I downloaded to .xls, to make it more presentable.

Now I've isolated the problem to this particular part of the code (below).

Spotfire normally is not that informative but it gives me very little to go on here. It seems to be something .NET related but that's about all I can tell.

The Error Message

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.

The Script

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
What are you doing in the macro? You may be able to remove it from your process all together. You could have fallen into this xyproblem.info - scsimon
Thanks for replying. The macro is unfortunately necessary, at least I believe it is. Essentially I'm looking to export a table to excel which keeps the conditional colouring of spotfire. A simple enough request I thought. I tried potentially translating it to a html table and then exporting to excel from there, as html would have the conditional colouring build into it. The html/spotfire/excel solution didn't play nice. So I'm playing with the idea of having a centralised macro which would do the necessary work for me. That way it can be pushed across a number of different dashboards. @scsimon - Alex Hannon
@scsimon Either way I think it would be great to be able to access excel's capabilities from within spotfire. Imagine the potential of exporting a table, preforming some significant vb work on it and then potentially bringing that work back into spotfire from a live connection with that data. It would add another layer to spotfire's capabilities. It wouldn't be for everyday use of course but... - Alex Hannon
Well in most applications this is redundant cleansing though sometimes needed. Is the macro work simply coloring columns? because Spotfire wouldn't keep the Microsoft scheme. What I mean is what is it you are doing in excel that you can't do in Spotfire? - scsimon
@scsimon Yes, this is simply just a request by a few users. I've been asked to find a solution that would put as little overhead on them as possible because of varying technical ability within their group. As a result simple acts such as exporting tables from spotfire and applying your own colouring scheme seem somewhat beyond some of them, thus it would be nice to have a very simple macro which would run from a centralised location that would do that for us; Applying the same colour scheme that we have in spotfire onto its equivalent excel table - Alex Hannon

1 Answers

2
votes

Right, so I'm answering my own question here but I hope it helps somebody. So the above code, as far as I'm aware was perfectly fine but didn't play well with the way my spotfire environment is configured. However, after going for a much more macro heavy approach I was able to find a solution.

On the spotfire end I've two input fields, one which gives the file path, the other the file name. Then there's a button to run the below script. These are joined together in the script but are crucially separate, as the file name needs to be inputted into a separate file, to be called by the main macro so that it can find the file location of the file.

Fundamentally I created three xml's to achieve this solution. The first was the main one which contained all of the main vba stuff. This would look into a folder in another xml which this script populates to find the save location of the file specified in an input field in spotfire. Finding the most recent file using a custom made function, it would run a simple series of conditional colour operations on the cell values in question.

This script populates two xml files and tells the main macro to run, the code in that macro automatically closing excel after it is done.

The third xml is for a series of colour rules, so user can custom define them depending on their dashboard. This gives it some flexibility for customisation. Not necessary but a potential request by some user so I decided to beat them to the punch.

Anyway, code is below.

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

Long story short: To run excel macro's from spotfire one solution is to use the system.dianostics method I use above and simply have your macro set to auto run.