Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to put text from email message into a range of Spreadsheet cells?

I have a short script that collects some text from an email message which I then wish to place into a range of cells in an Excel spreadsheet.


So inside a try event I have this to get the text:

tell application "Mail"


set emailSelection to (get selection)

set theMessage to (item 1 of the emailSelection)


copy content of (item 1 of the emailSelection) to theText-- as string doesn't work generates compile error

set pasteText to (paragraphs from paragraph 4 to the last paragraph) of theText

end tell



followed by

tell application "Microsoft Excel"

clear range (range "A1:A50" of worksheet "TP from website" of workbook "Contacts Database (for script testing)")

copy pasteText


paste (rangeA1)


which was converted to

tell application "Microsoft Excel"

clear range (range "A1:A50" of worksheet "TP from website" of workbook "Contacts Database (for script testing)")

«event misccopy» pasteText


paste (rangeA1)


I guess a copy command needs two objects, one to the other from but not sure how to get my pasteText content into the spreadsheet.

Posted on Feb 22, 2014 9:28 PM

Reply
Question marked as Best reply

Posted on Feb 23, 2014 2:49 AM

Hello


I don't think you need to resort to copy and paste becausee Excel has direct scripting support to set value of range. Try something liket the following code. (The part scripting Excel is not tested, for I don't have it.)


Good luck,

H


tell application "Mail"
    set mm to (get selection)
    set m to mm's item 1
    set t to m's content
    set pp to t's paragraphs 4 thru -1
end tell

set qq to {}
repeat with p in pp
    set qq's end to {p's contents}
end repeat
repeat 50 - (count qq) times
    set qq's end to {""}
end repeat
set r to "A1:A" & (count qq)
--return {r, qq} -- for test

tell application "Microsoft Excel"
    tell worksheet "TP from website" of workbook "Contacts Database (for script testing)"
        tell range r
            --clear range
            set value to qq
        end tell
    end tell
end tell
7 replies
Question marked as Best reply

Feb 23, 2014 2:49 AM in response to wideEyedPupil

Hello


I don't think you need to resort to copy and paste becausee Excel has direct scripting support to set value of range. Try something liket the following code. (The part scripting Excel is not tested, for I don't have it.)


Good luck,

H


tell application "Mail"
    set mm to (get selection)
    set m to mm's item 1
    set t to m's content
    set pp to t's paragraphs 4 thru -1
end tell

set qq to {}
repeat with p in pp
    set qq's end to {p's contents}
end repeat
repeat 50 - (count qq) times
    set qq's end to {""}
end repeat
set r to "A1:A" & (count qq)
--return {r, qq} -- for test

tell application "Microsoft Excel"
    tell worksheet "TP from website" of workbook "Contacts Database (for script testing)"
        tell range r
            --clear range
            set value to qq
        end tell
    end tell
end tell

Feb 24, 2014 2:03 AM in response to wideEyedPupil

Hello


If I'm not mistaken, the following script will retrieve the value of range "A1:B3" of active sheet as list of value of rows:


tell application "Microsoft Excel"
    tell active sheet
        tell range "A1:B3"
            return value
        end tell
    end tell
end tell


E.g., if cell A1 contains string "A1" etc, then the resulting list will be:

{{"A1", "B1"}, {"A2", "B2"}, {"A3", "B3"}}


And the following script will set the value of range "A1:B3" to given list of lists:


tell application "Microsoft Excel"
    tell active sheet
        tell range "A1:B3"
            set value to {{11, 12}, {21, 22}, {31, 32}}
        end tell
    end tell
end tell


Here's the reference I use, which is old but the last excel applescript reference in pdf format I can find in their site.

http://download.microsoft.com/download/3/A/7/3A70FB4B-0C6A-43E3-AAB7-AC9166B2563 2/Excel2004AppleScriptRef.pdf


Good luck,

H

Feb 27, 2014 5:30 PM in response to Hiroto

These scripts work if I place them in seprate script windows. If I concatinate the two scripts into one script the second part (tell application "Mcrisoft Excel"…end tell) doesn't execute.


This seems to be a problem I'm having with lots of scripts. The first part runs but when I then tell another application to do something it wont do it. Unbelieveable!

Feb 28, 2014 2:54 AM in response to wideEyedPupil

Well, the "return" statement exits the handler which lexiclally encloses the statment. If it is executed in top level of script, which is indeed in an implicit run handler of the script, it will exits the script itself.


So if perchance you're using "return" statement inadequately, that would be the reason you have your script be terminated prematurely.


* I use "return" statement to check the intermediate states of the script in testing, for "log" statement, which is supposedly to be used in testing, does some unsolicited conversions on values to be checked. If my use of "return" had confused you, my appologies.


The following script should run to the ends:


tell application "Microsoft Excel"
    tell active sheet
        tell range "A1:B3"
            --return value -- this will put the values in result window and terminate the script here
            --log value   -- this will log the values in event log window and let the script continue
            set old_values to value -- save the values in varable for later use and let the script continue
        end tell
    end tell
end tell

tell application "Microsoft Excel"
    tell active sheet
        tell range "A1:B3"
            set value to {{11, 12}, {21, 22}, {31, 32}}
        end tell
    end tell
end tell

return old_values


Hope this may help,

H

Feb 28, 2014 5:37 PM in response to Hiroto

Okay I posted these two scripts to StackOver flow and noted seperately they work together only the top srcipt works and the thing I didn't realise is that the return statement actually as well as giving a result stops the script (or the script block that has been called I suppose). So these problems I have been having were because I uncommented the return statements and expected the script to keep runining past with them operational. That was an expensive less to learn, one whole days confusion!!

How to put text from email message into a range of Spreadsheet cells?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.