使用AppleScript从邮件正文导出内容并导出到excel

时间:2019-01-28 16:45:42

标签: excel email applescript

我正在尝试导出具有设置模板的电子邮件,以便将每个字段放置在单独的Excel列中。

因此,目前我收到的所有我需要导出的电子邮件都被放入收件箱的单独文件夹中。该电子邮件将包含以下内容;

Name:
Age:
Email address:
Phone Number:

我正在寻找一种运行脚本的方法,该脚本将遍历此文件夹中的所有电子邮件,并从每个字段获取信息,并将其放在Excel的列中,因此将有名称列,年龄列等

当前,我有一个几乎可以运行的脚本,但是无法获取它来拆分电子邮件正文的内容。

当前,我将创建Excel文档,但无法将其拆分为单独的电子邮件正文。我已经注释了一些代码,这些代码将返回分隔的字段,但只有一封电子邮件而不是全部。

tell application "Microsoft Excel"
    set LinkRemoval to make new workbook
    set theSheet to active sheet of LinkRemoval
    set formula of range "F1" of theSheet to "Phone Number"
    set formula of range "E1" of theSheet to "Email address"
    set formula of range "D1" of theSheet to "Age"
    set formula of range "C1" of theSheet to "Name"
    set formula of range "B1" of theSheet to "From"
    set formula of range "A1" of theSheet to "Date"

end tell

tell application "Mail"
    set theRow to 2
    set theAccount to "Gmail"
    set theMessages to messages of mailbox "signups" of account "Gmail"
    set mySelection to selection
    set myMail to first item of mySelection
    set myLines to every paragraph of (content of myMail)
    repeat with aMessage in theMessages
        my SetDate(date received of aMessage, theRow, theSheet)
        my SetFrom(sender of aMessage, theRow, theSheet)
        my SetMessageC(content of aMessage, theRow, theSheet)
        my SetMessageD(content of aMessage, theRow, theSheet)
        my SetMessageE(content of aMessage, theRow, theSheet)
        my SetMessageF(content of aMessage, theRow, theSheet)
        set theRow to theRow + 1
    end repeat
end tell

(* set myEvent to ExtractfromRichText(myLines) -- parse the paragraphs to make a record {EName, ERoom,EStart,EEnd}

on ExtractfromRichText(LocalLines) -- convert the rich text with fixed format into a data set
    set AppleScript's text item delimiters to {":", return & linefeed, return, linefeed} -- to remove all rich text end lines
    repeat with aLine in LocalLines
        try
            if text item 1 of aLine contains "Name" then set Name to text item 2 of aLine
            if text item 1 of aLine contains "Age" then set Age to text item 2 of aLine
            if text item 1 of aLine contains "Email Address is" then set emailAddress to text item 2 of aLine
            if text item 1 of aLine contains "Phone Number" then set phoneNumber to text item 2 of aLine
        on error -- unexpected format
            log "error"
            return {EName:""} -- not proper email format for calendar events. return empty name
        end try
    end repeat
    return {Name:Name, Age:Age, Email Address:emailAddress, Phone Number:phoneNumber}
end ExtractfromRichText *)

on SetDate(theDate, theRow, theSheet)
    tell application "Microsoft Excel"
        set theRange to "A" & theRow
        set number format of column 1 to "dd/mm/yyyy"
        set formula of range theRange of theSheet to theDate
    end tell
end SetDate

on SetFrom(theSender, theRow, theSheet)
    tell application "Microsoft Excel"
        set theRange to "B" & theRow
        set formula of range theRange of theSheet to theSender
    end tell
end SetFrom

on SetMessageC(theMessage, theRow, theSheet)
    tell application "Microsoft Excel"
        set AppleScript's text item delimiters to {":", return & linefeed, return, linefeed}
        set theRange to "C" & theRow
        set number format of column 3 to "Text"
        set formula of range theRange of theSheet to theMessage
    end tell
end SetMessageC

on SetMessageD(theMessage, theRow, theSheet)
    tell application "Microsoft Excel"
        set theRange to "D" & theRow
        set number format of column 4 to "Text"
        set formula of range theRange of theSheet to theMessage
    end tell
end SetMessageD

on SetMessageE(theMessage, theRow, theSheet)
    tell application "Microsoft Excel"
        set theRange to "E" & theRow
        set number format of column 5 to "Text"
        set formula of range theRange of theSheet to theMessage
    end tell
end SetMessageE

on SetMessageF(theMessage, theRow, theSheet)
    tell application "Microsoft Excel"
        set theRange to "F" & theRow
        set number format of column 6 to "Text"
        set formula of range theRange of theSheet to theMessage
    end tell
end SetMessageF

0 个答案:

没有答案
相关问题