如何使用Applescript从Excel工作表填充页面文件

时间:2011-12-09 17:22:31

标签: macos excel applescript

我正在尝试从Excel电子表格中的数据填充模板。这是我第一次涉足Applescript。我的计划是在模板中包含唯一的文本字符串(即Value_1,Value_2等),然后将每个值存储在变量中,并使用每个字符串查找并替换每个变量。

我非常感谢任何帮助;这是我在的地方:

tell application "Microsoft Excel"
    tell active workbook
        activate object worksheet "Page 2"
        copy range range "B7" of active sheet
        // Store as variable?
    end tell
end tell

tell application "Pages"
    activate
    tell application "System Events"
        keystroke "v" using command down
        // Find and replace?
    end tell
end tell

1 个答案:

答案 0 :(得分:1)

这样的事情会做你想做的事情:

tell application "Microsoft Excel"
    tell active workbook
        activate object worksheet "Sheet2"
        set B7_Value to string value of range "B7" of active sheet
    end tell
end tell

tell application "Pages"
    set my_text to first document's body text
    set temp to do shell script "echo " & quoted form of my_text & " | sed -e 's/B7_Value/" & B7_Value & "/g'"
    set first document's body text to temp
end tell

此脚本不保留格式。如果你想保持格式化可能是这样的:

tell application "Microsoft Excel"
    tell active workbook
        activate object worksheet "Sheet2"
        set B7_Value to string value of range "B7" of active sheet
    end tell
end tell

tell application "Pages"
    activate
    tell application "System Events"
        keystroke "f" using command down
        set the clipboard to "B7_Value"
        keystroke "v" using command down
        delay 0.1
        keystroke tab
        delay 0.1
        set the clipboard to B7_Value
        keystroke "v" using command down
        delay 0.1
        keystroke tab
        delay 0.1
        keystroke space
        delay 0.1
        key code 53 -- escape
    end tell
end tell

要使其正常工作,您需要启用系统偏好设置 - >键盘 - >所有控件。