How to script Excel or Numbers on a mac, and launch from bash? - excel

I want to write a bash script for a mac which takes a text file containing a table of numbers (can be .csv), converts the numbers to a chart, saves the chart (any file format that I can display on my web page), and exits. It must do this unattended. No user interaction.
I know bash, perl, and a little AppleScript, and I can learn VBA or whatever else. I am about to plunk down money for the new Excel 2011 for Mac. I think I want Excel rather than Numbers'09, but I am not at all sure, and I think VBA or AppleScript will allow me to automate most of the work.
Is AppleScript + Excel a good choice? If so, then I'm home free. Applescript programs can be launched from bash via osascript command.
But maybe VBA is better? But how can I launch a Excel VBA script from bash on a mac?
Summary of questions: (1) AppleScript or VBA or something else? (2) Excel or Numbers? (3) How to launch Excel VBA script from bash?
Thanks in advance,
Ken

Don't forget to try OpenOffice (and derivatives). It's been a while since I last worked with it, but they have a macro language and some VBA support (don't know how good it is now, I'm sure it's progressed) - so it could likely generate your graphs for you.
Alternatively, this sounds like something Gnuplot could very easily do. See this article where they start with the raw data in a text file, and create a PNG file for use in a web page: http://www.ibm.com/developerworks/aix/library/au-gnuplot/index.html
Just thought you should know about other options than Excel and Numbers...

You should definitively consider using gnuplot (free), which can easily create graphics and even HTML5 canvas elements from different input file formats via the command line.

iWork 09 does support Applescript. Pages is pretty good with its support. Numbers far less so. I think most expect more expansive Applescript support with the new version as Numbers only has partial support. So for what the original question asks you really need to use Excel.
If you are going to script I'd look seriously at Python or Ruby combined with Appscript rather than Applescript or VBA. Applescript is a bear of a language to write in and using one of the other languages gives you all the libraries of that language. That allows quite a bit more powerful code to be written.
If you are just writing for Office and don't need much other functionality then I'd probably stick with VBA.

You might like to take a look at Google Chart Tools.
It is exceptionally useful for including charts and graphs within web pages.
You just create a URL containing the data as a src for an image, then tada, it appears.
Have a play.
For instance, a few parameters:
chd=t:10,20,30
chco=FF0000|00FF00|0000FF
chs=320x240
cht=p3
chdl=Red|Green|Blue
Makes a URL: https://chart.googleapis.com/chart?chd=t:10,20,30&chco=FF0000|00FF00|0000FF&chs=320x240&cht=p3&chdl=Red|Green|Blue
Which forms a chart:

I would recommend using Numbers and AppleScript if you are on OS X—these are well integrated. Also, you could use Automator to make your life even simpler.

Microsoft removed VBA support from Office with v2008. iWork '08 has no Applescript support, and while I can't speak directly about v'09 given Apple's recent decline in scriptable applications I wouldn't count on it.
The sure, long-term bet here would be to use Excel with Applescript.

Related

CSV to Excel Doc?

I was curious what the community thinks is the easiest way to take a CSV file and 'save as' a Excel document with only a couple formulas pasted in?
I am trying to do this behind the scenes, and not physically navigating. e.g. opening, selecting save As, etc -- even though this is already VERY simple I **need to do this in code (Think automation)
Background: I have a c++ command line program generating the .csv, and a C# GUI starting this process. Either programs could hold the code, but I figure this is easiest in C# (InterOp?) The reasons I don't directly send code into the csv is because of the amount of comma characters that will mess up the csv and because other Excel documents need to reference the sheets so they need to be in .xls format.
=AVERAGE(C2:C999)
=COUNTIFS(C:C,">0",C:C,"<31")
=COUNTIFS(C:C,">31",C:C,"<55")
=COUNTIF(C:C,">55")
Have a look and see whether command-line scipting of openoffice will do the job. It can do quite a lot of conversions very easily. Otherwise there are a lot of Excel-producing libraries, for example PHPExcel, but you'd need to wrap some programming around them.

Using different program office extension

I have a program that can access a database with a whole bunch of articles.
Due to copyright, I can't access the database straight from my program, but I have a different program that can access it, and it's legitimate to copy small bits from the articles.
Because my friends and I quote a lot from these articles, I thought it would be useful if we could find an add-in for Word that will copy the requested part from an article.
Is there any add-in for Word that would let me use the program that I mentioned above so that I can access the database from within Word?
I would like to program this add-in myself, if possible.
Without further information about which operating system, and version of Word you are using, I can offer only a general outline.
1) It seems to me that you want to make a Word macro using Word Basic, or Visual Basic.
2) When you want to call your program which is external to Word, you need to use the shell command as outlined here from Microsoft's webpage.
I hope that helps you get started writing your macro!
CHEERS
Well its a wrokaround but you can use an automation tool which can run a sequence of actions on a given GUI like Winrunner or TestQuest to semulate the usage of the program, i assume these tools can get an input from a given xml or text file and log outputs in log text file.
If you have the output in a text file you will be able to parse the file using any programmign language and get the information you need and write it to eord or whatever format using OLE objects.

Batch convert xls-Files to csv

I need to convert over 100 Excel files to CSV. Worse these files consist of multiple sheets and I only need one of them.
At first I stumbled upon the Perl program xls2csv. Luckily I even found on XLS file conversion at the bottom a convenient script that converts all sheets into seperate csv files. But unluckily this converter is broken and skips lines.
I also tried pyodconverter but that only converts the first sheet.
Any suggestions? It would be ok if that conversion had to be done on Windows though I would really prefer Linux. And if it has to be Windows it would be nice if it wouldn't need an Excel installation.
There's a very useful java library called Apache POI at http://poi.apache.org/
The following link provides an example application that converts xls to csv.
http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java
If you know java you can adjust it to your needs. Since it's java it runs also on linux.
you could also have a look at StatTransfer... (Win only, I'm afraid)
I know this is late but there is actually an HTA (HTML Application) which can do this. The details and download link can be found here.

How can I GZip compress a file from Excel VBA using code in an .xla file only?

I need to be able to GZip compress a file in an Excel VBA function. Specifically I need to be able to use the 'deflate' algorithm.
Is there a way to do this without having to exec a command line application? With no dependency on external tools the code will be more robust.
Ideally the code would make use of pre-installed VBA or COM library functions - I don't want to have to implement this logic myself or install DLLs etc.
If possible, I want installation of the function to be as simple as adding a .xla to the available Excel Add-Ins. No DLLs, EXEs, registry entries etc. required.
Edit Can I make use of the .NET GZipStream to do this?
VBA (which is really a dialect of VB6) is slow for these kind of applications. I remember I once implemented Shannon-Fano algorithm on VB6 and on C, the C version was about 10 times faster, even after being turned into a DLLMain and called from there rather than on a command-line executable.
There are lots of COM DLLs that provide compression services, both open source and shareware, and some of them implement GZIP's deflate algorithm. It'd be really simple to just call one function from such a DLL from your VBA code to do the compression on your behalf.
I understand your being reluctant on using something external to your application, though in this case you might have to apply an exception for performance's sake.
In an effort to completely spoil your fun, examine file ZIPFLDR.DLL on windows\system32. you may also like to take a look at these links:
This has an example of how to do what you want (zipping using windows built-in ZIP capabilities) from VB.NET, it shouldn't be much different from VBA or VB6:
Transparent ZIP with DLL call
This one has a sample application on VB6 using windows built-in capabilities to zip (in ZIP rather than GZIP format, of course): Using Windows XP "Compressed Folder" shell extension to work with .zip files
Found both thru googling, you should be able to find more/better examples.
OK, I think I have an answer for you.
zlib is a library written by the guy that wrote the deflate algorithm you don't want to implement. There is a win32 DLL available. Here's the FAQ regarding using it from Windows:
http://www.zlib.net/DLL_FAQ.txt
Check out question 7. The authors don't seem too keen on Windows users, and don't seem at all keen on VB users, but as long as they're kind enough to provide the library we can do the rest.
If this is enough to help you, then great. If you want help with calling the C library from VBA add a comment and we'll figure it out. I haven't done any VB-to-C calls in years--it sounds like fun.
It seems that you want to open a bottle of wine but you definitly refuse to use a bottle-opener. As long as there is no VBA function allowing the GZipping of a file, you will not be able to do the job without some external ressource such as a dll or exe file.
If somebody wanted to compress files without relying on 3rd-party software they would generally implement it as a COM object/DLL so it would be available to more than just Excel. If somebody wanted to incorporate zip functionality into Excel they would use 3rd-party tools so they wouldn't have to re-implement the algorithm. So you're swimming against the tide. However...
http://www.cpearson.com/excel/SaveCopyAndZip.htm
There are two versions. The COM Add-in version "...allows you to zip any workbook that has been saved to disk (but it may be in an unsaved state)." It relies on a Moonlight Software component but all the components and set-up are contained in the installer. It's not quite public domain but the license is less restrictive than the GPL. The end result is an Excel add-in (that uses a 3rd-party component).
But if you really, truly don't want any dependencies on external tools you're either going to have to implement the compression algorithm yourself or wait until Microsoft builds that functionality into Windows and exposes it through Excel.
I hope this helps.
If you want to implement the algorithm in VBA, you would need to (in VBA) save the spreadsheet and then use VB's I/O functions to open the file, deflate it, and save it again. For all intents and purposes it's identical to writing an ordinary VB application that works on a file. You might need to put the VBA macro in a separate workbook to avoid "file in use" types of errors, but if you reopen the file as read-only and save it with a different filename you should be OK keeping everything in one workbook.
But I'm almost certain that shelling out to gzip from within the VBA would be functionally identical and infinitely easier.
EDIT: Some code. It didn't fail when I ran it, so it's OK to keep everything in the same workbook.
Sub main()
ActiveWorkbook.Save
Open "macrotest.xls" For Binary Access Read As #1
Open "newfile.zip" For Binary Access Write As #2
'do your stuff here
Close #2
Close #1
End Sub

Using Office to programmatically convert documents?

I'm interested in using Office 2007 to convert between the pre-2007 binary formats (.doc, .xls, .ppt) and the new Office Open XML formats (.docx, .xlsx, .pptx)
How would I do this? I'd like to write a simple command line app that takes in two filenames (input and output) and perhaps the source and/or destination types, and performs the conversion.
Microsoft has a page which gives several examples of writing scripts to "drive" MS Word. One such example shows how to convert from a Word document to HTML. By changing the last parameter to any values listed here, you can get the output in different formats.
The easiest way would be to use Automation thru the Microsoft.Office.Interop. libraries. You can create an instance of a Word application, for example. There are methods attached to the Application object that will allow you to open and close documents, plus pretty much anything else you can accomplish in VBA by recording a macro.
You could also just write the VBA code in your Office application to do roughly the same thing. Both approaches are equally valid, depending on your comfort in programming in C#, VB.NET or VBA.

Resources