Hi guys,
As this is my first post, I’d first like to thank the members of this forum for saving me hours of head scratching during my [somewhat steep] learning curve…
However, I’ve hit a brick wall. I have witten the following script to extract event details from ical and strip out some not needed characters. My next task is to populate an excel spreadsheet - herein lies the difficulty.
It would seem that currently it is inserting the last line only of the result as a complete chunk of text in every cell in the range A2:F (7 in this case).
I have column headings in the following order: Date | Item Description | Unit Price | Qty | P&P | Total
and would like each line of the result populating as such.
Here is the script:
set {year:y, month:m, day:d} to current date
set str to (d as string) & " " & (m as string) & " " & (y as string)
set today to date str
set tomorrow to today + 60 * 60 * 24
set startOfPeriod to today - (today's day) * days + days
tell startOfPeriod + 32 * days to set endOfPeriod to it - ((its day) - 1) * days - 1
tell application "iCal"
tell calendar "Sales"
set results to {}
set theEvents to {}
set AllEvents to every event whose (start date is greater than startOfPeriod) and (start date is less than endOfPeriod)
set eventCount to number of AllEvents
repeat with anEvent in AllEvents
set eventString to {}
set eventDate to start date of anEvent
set eventDescription to description of anEvent
set eventDesc to paragraphs of eventDescription
set unitPrice to {}
set Qty to {}
set Postage to {}
set totalPaid to {}
repeat with i from 1 to count of eventDesc
set thisPara to item i of eventDesc
if thisPara contains "Item Desc: " then -- gets the item description
set itemDescription to ((characters 12 thru -1 of thisPara) as string) -- strips the first 13 characters (Item Desc: )
end if
if thisPara contains "Unit Price: " then -- gets the unit price
set unitPrice to ((characters 14 thru -1 of thisPara) as string) -- strips the first 15 characters (Unit Price: )
end if
if thisPara contains "Qty: " then -- gets the quantity
set Qty to ((characters 7 thru -1 of thisPara) as string) -- strips the first 8 characters (Qty: )
end if
if thisPara contains "P&P: " then -- gets the P&P
set Postage to ((characters 7 thru -1 of thisPara) as string) -- strips the first 8 characters (P&P: )
end if
if thisPara contains "Total Paid: " then -- gets the total paid
set totalPaid to ((characters 14 thru -1 of thisPara) as string) -- strips the first 15 characters (Total Paid: )
end if
set eventString to eventDate & "," & itemDescription & "," & unitPrice & "," & Qty & "," & Postage & "," & totalPaid & linefeed as string
end repeat
set results to results & eventString
end repeat
end tell
end tell
tell application "Microsoft Excel"
set {month:currentMonth} to current date
set currentMonth to currentMonth as string
set sheetName to "Sales - " & currentMonth as string
tell active workbook
activate object sheet sheetName
end tell
tell sheet sheetName of active workbook
activate object cell "A2"
set value of range ("A2:F" & (eventCount + 1)) to eventString
end tell
end tell
return results
Here is the set of results that the script produces:
{"Sunday, 6 January 2013 23:46:32,2.Shotgun Cartridge Fridge Magnets (1 pair) novelty, clay,3.99,1,1.00,4.99
", "Tuesday, 8 January 2013 23:46:32,3.Shotgun Cartridge Fridge Magnets (1 pair) novelty, clay,3.99,1,1.00,4.99
", "Friday, 18 January 2013 23:46:32,4.Shotgun Cartridge Fridge Magnets (1 pair) novelty, clay,3.99,1,1.00,74.99
", "Tuesday, 22 January 2013 23:46:32,5.Shotgun Cartridge Fridge Magnets (1 pair) novelty, clay,3.99,1,1.00,4.99
", "Wednesday, 2 January 2013 23:46:32,1.Shotgun Cartridge Fridge Magnets (1 pair) novelty, clay,53.99,1,1.00,4.99
", "Wednesday, 30 January 2013 23:46:32,6.Shotgun Cartridge Fridge Magnets (1 pair) novelty, clay,3.99,1,1.00,4.99
"}
I know I’m doing something fundamentally wrong - I just dont know what! Please be gentle though - I’ve only been scripting in Applescript for a few days!
edit: as a side note, to reproduce the error, create an event in a calendar of your choice (preferably an empty one) and insert the following into the notes section:
Item Desc: this is the product name
Unit Price: £5.99
Qty: 4
P&P: £1.00
Total Paid: £24.96
Thanks in advance guys!
Mark
Model: iMac
AppleScript: 2.3
Browser: Chrome
Operating System: Mac OS X (10.6)