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

我正尝试从Excel电子表格中的数据填充模板。 这是我第一次进入Applescript。 我的计划是在模板中有唯一的文本string(即Value_1,Value_2等),然后将每个值存储在一个variables中,并查找和replace每个string的每个variables。

我非常感谢任何帮助; 这里是我在哪里:

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 

像这样的东西会做你想要的:

 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 

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