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

发布于 2024-12-20 16:47:27 字数 551 浏览 3 评论 0原文

我正在尝试根据 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

I'm trying to populate a template from data in an Excel spreadsheet. This is my first foray into Applescript. My plan is to have unique text strings in the template (i.e. Value_1, Value_2, etc) then store each value in a variable and find and replace each variable with each string.

I would very much appreciate any help; here is where I'm at:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-12-27 16:47:27

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

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

为此,您需要启用系统偏好设置 -->键盘-->所有控制。

Something like this will do what you want :

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

This script does not keep formatting. If you want to keep formatting perhaps something like this :

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

For this to work you will need to enable System Preferences --> Keyboard --> All controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文