重复循环变量范围的脚本

时间:2014-10-19 00:50:54

标签: excel variables applescript

我对Apple脚本几乎一无所知,非常感谢您的帮助!这是我想要做的:

  

编写一个脚本,将excel单元格复制并粘贴到word中   然后自动将文件保存为pdf。然后脚本会   自动化Apple邮件程序发送一堆电子邮件(用   附件)基于excel电子表格。

到目前为止,我已经编写了一个脚本来执行所有这些操作,除了我无法让脚本重复下一个单元格的过程等等,直到所有单元格和电子邮件都完成为止。以下是我到目前为止的情况:

tell application "Microsoft Excel"
activate
set empName to string value of range "A4" of active sheet
set myVal to string value of range "P4" of active sheet
tell application "Finder"
    set theFile to "Macintosh HD:users:deve:desktop:C.dotx"
    tell application "Finder"
        open file theFile
        set the clipboard to myVal
        tell application "Microsoft Word"
            activate
            tell application "System Events"
                tell application process "Microsoft Word"
                    keystroke "v" using command down
                    keystroke "a" using command down
                    tell application "font" - this is just so I can fix a font issue (and I don't know how to do it using applescript so I made an automator program)
                        activate
                        delay 1
                    end tell
                    tell application "Microsoft Word"
                        save as active document file name "Macintosh HD:Users:Deve:Desktop:Materials for Applescript:CL:" & empName & " Letter.pdf" file format format PDF
                    end tell
                    tell application "Mail"
                        delay 2
                        set theMessage to make new outgoing message with properties {visible:true, subject:"Message", content:myVal}

                        tell theMessage
                            make new to recipient at end of to recipients with properties {name:empName, address:"XXXX@gmail.com"}
                        end tell
                        tell theMessage
                            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:CL:" & empName & " Letter.pdf" as alias} at after the last paragraph
                            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Work Book.pdf" as alias} at after the last paragraph
                            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Picture File.pdf" as alias} at after the last paragraph
                            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Lyrics.pdf" as alias} at after the last paragraph
                            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Comparison.pdf" as alias} at after the last paragraph
                            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Earlier Version.pdf" as alias} at after the last paragraph

                        end tell
                    end tell
                end tell
              end tell
          end tell
      end tell
  end tell
end tell

我如何循环整个过程,使细胞每次向下移动一次,以便empNamemyValA5P5获取其值......然后A6P6一直到42?

1 个答案:

答案 0 :(得分:0)

您只需要将行指定变为变量并将代码置于重复循环中,然后在每次迭代后将其增加1。这是一个如何做到的例子。 (我也可以抵制消除你的一些错误的嵌套告诉块(如果你没有理由这么做,不要在其他应用程序中嵌套告诉块告诉块。

property firstRow : 5
property lastRow : 42

set r to firstRow
repeat until r is (lastRow + 1)

    tell application "Microsoft Excel"
        activate
        set empName to string value of range ("A" & r) of active sheet
        set myVal to string value of range ("P" & r) of active sheet
    end tell

    tell application "Finder"
        set theFile to "Macintosh HD:users:deve:desktop:C.dotx"
        open file theFile
        set the clipboard to myVal
    end tell

    tell application "Microsoft Word"
        activate
        tell application "System Events"
            tell application process "Microsoft Word"
                keystroke "v" using command down
                keystroke "a" using command down
            end tell
        end tell
    end tell

    tell application "font" -- this is just so I can fix a font issue (and I don't know how to do it using applescript so I made an automator program)
        activate
        delay 1
    end tell
    tell application "Microsoft Word"
        save as active document file name "Macintosh HD:Users:Deve:Desktop:Materials for Applescript:CL:" & empName & " Letter.pdf" file format format PDF
    end tell

    tell application "Mail"
        delay 2
        set theMessage to make new outgoing message with properties {visible:true, subject:"Message", content:myVal}

        tell theMessage
            make new to recipient at end of to recipients with properties {name:empName, address:"XXXX@gmail.com"}
        end tell
        tell theMessage
            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:CL:" & empName & " Letter.pdf" as alias} at after the last paragraph
            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Work Book.pdf" as alias} at after the last paragraph
            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Picture File.pdf" as alias} at after the last paragraph
            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Lyrics.pdf" as alias} at after the last paragraph
            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Comparison.pdf" as alias} at after the last paragraph
            make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Earlier Version.pdf" as alias} at after the last paragraph
        end tell
    end tell

    set r to r + 1
end repeat

我没有在我的机器上测试这个,但这回答了你的基本问题。发布您需要的任何后续帮助。