0

I have a script that I use to export expense entries from Excel to QIF format. To create the output files, I have a table in Excel that lists the expense (account) types:

PaymentType Category           Acct Type
----------- -----------------  ---------
A           American Express   CCard
B           Bob                Cash
C           Capital One        CCard
E           Expense Income     Bank
N           PNC                Bank
P           PayPal             CCard
S           Sears              CCard
U           USAir MC           CCard

The content of the table is labeled: PaymentSourceTable

And I have this script to read the contents of the table and subsequently create an output file for each:

tell application "Microsoft Excel"
    #
    # Get the list of payment sources from the spreadsheet and
    #   create a QIF file for each
    #
    #   Also create the list of filenames corresponding to the sources.
    #   That list will be used when appending expense records to the files.
    #
    set PaymentSources to {}
    tell worksheet ListsWorksheet
        set PaymentSources to value of range PaymentSourceTable
    end tell

Everything works very well in the rest of my script but sometimes PaymentSources contains the actual table values twice - as if I had appended my table values twice. The entries appear A-U then A-U again.

It only happens when I run the script from the script menu while in Excel, and then only sometimes. It never happens if I run the script from the Applescript editor - or at least hasn't happened yet in my many tests. That in itself seems really odd and I keep trying it over and over, thinking I'm just imagining it. :-)

I'm baffled.
Has anyone else seen this or could suggest a way to debug?

btw, I'm using Excel for Mac 2011.

Update As it turns out, PaymentSources was not the problem. the following code was the real culprit:

repeat with source in PaymentSources
    set _ptype to item 1 of source
    if _ptype is not equal to "" then
        set _type to "!Type:" & item 3 of source
        set _furl to my constructFileName(source)
        my createFile(_type, _furl)
        set end of FileNames to {item 1 of source, _furl, 0}
    end if
end repeat

that and the fact that I defined FileNames as a property:

property FileNames : {}

.

I totally missed the fact that properties persist between runs. Lesson learned.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
SwimsZoots
  • 387
  • 2
  • 4
  • 13
  • Have you tried the script without this line: `set PaymentSources to {}` – Craig Smith May 30 '15 at 04:38
  • @CraigSmith, Thanks. I tried that and it worked correctly the first time through then reverted back to its bad behavior. – SwimsZoots May 30 '15 at 19:00
  • That's crazy. Is this your entire code? – Craig Smith May 30 '15 at 20:56
  • Craig, no there's quite a bit more to it. But as I suspected, it was user error. A rookie mistake actually. Turns out it wasn't **PaymentSources** that was the problem. It was an array of filenames that I filled based on the PaymentSources. I had defined it as a property which I didn't realize persisted between runs, so each time through, would get appended with another set of names. Apparently, running from the editor vs from Excel menu invokes separate instances so I would run from the editor and it would work as expected then go to Excel and run with the extra entries. – SwimsZoots Jun 03 '15 at 13:04
  • Glad you figured it out. – Craig Smith Jun 03 '15 at 13:47

0 Answers0