我有一个 Excel 电子表格大师。 A 列列出单词和短语; B 列列出了它们的缩写。
我想编写一个 AppleScript 来在 Word 文档中查找并用 ColumnB 替换 ColumnA 1); & 2) 在另一个 Excel 电子表格中。
我发现了下面的 AppleScript,它在 Word 文档中查找和替换方面效果相当好。
AppleScript 打开 Excel 电子表格并将其使用的范围复制到行列表中 由查找字符串和替换字符串组成。然后脚本将循环执行 AppleScript 列表中的所有行并在 Word 文档中执行查找/替换。
使用脚本添加
将 XLSX 设置为(选择文件类型(“org.openxmlformats.spreadsheetml.sheet”)) 将 DOCX 设置为(选择类型为 {"org.openxmlformats.wordprocessingml.document"} 的文件)
告诉应用程序“Microsoft Excel” 激活
打开文件 XLSX
告诉文档 1 的活动表 创建一个列表列表,其中每一行将成为两个组件的列表项 由查找字符串和替换字符串组成 将 arange 设置为使用范围的值 结束告诉
结束告诉
告诉应用程序“Microsoft Word” 激活
打开文件 DOCX
设置 myfind 查找活动文档的文本对象的对象 将 myfind 的属性设置为 {匹配大小写:false, 匹配整个单词:true, 匹配通配符:false}
重复排列箭头 执行 find myfind 查找文本(arow 的第 1 项)替换为(arow 的第 2 项)替换 替换全部
结束重复
结束告诉
关于错误消息,
choose file
命令生成一个 alias,这不是 Office 的 open 命令所期望的。将其与 file(例如打开文件 theXLSX)结合起来会使问题变得更加复杂。
choose file of type "org.openxmlformats.wordprocessingml.document"
--> alias "MacHD:Users:username:Desktop:wabbr.docx"
open file theDOCX
--> Microsoft Word got an error: file (alias "MacHD:Users:username:Desktop:wabbr.docx") doesn’t understand the “open” message.
因此,请以文本形式获取参考,如下所示:
choose file of type "org.openxmlformats.wordprocessingml.document")) as text
--> "MacHD:Users:username:Desktop:wabbr.docx"
open theDOCX
--> open "MacHD:Users:username:Desktop:wabbr.docx"
根据我的理解,这是一个应该可以满足您需要的脚本。
use scripting additions
set tx to (choose file of type ("org.openxmlformats.spreadsheetml.sheet")) as text
--> "MacHD:Users:username:Desktop:xabbr.xlsx"
set tw to (choose file of type {"org.openxmlformats.wordprocessingml.document"}) as text
--> "MacHD:Users:username:Desktop:wabbr.docx"
tell application "Microsoft Excel"
set wbx to open workbook workbook file name tx
tell active sheet of wbx
set ur2 to intersect range1 (get used range) range2 column "A:B"
set wordPairList to value of ur2
end tell
end tell
tell application "Microsoft Word"
activate
open tw
set nw to name of front document
set myf to find object of text object of active document
set properties of myf to {match case:false, match whole word:true, match wildcards:false, match all word forms:true}
repeat with eaSearch in wordPairList
execute find myf find text (item 1 of eaSearch) replace with (item 2 of eaSearch) replace replace all
end repeat
execute find find object of text object of active document find text ".." replace with "." replace replace all
execute find find object of text object of active document find text " ^w" replace with space replace replace all
end tell
Excel 有
open workbook
命令,所以我用它来代替。它允许您将生成的工作簿对象分配给变量,这意味着您不需要依赖前面的文档来控制您正在使用的文档。
我发现使用的范围不稳定,所以我使用
intersect
命令限制它。
注意配置查找对象时使用的属性。
匹配通配符可以设置为true,然后您可以使用单词的任何标准通配符产品。
也就是说,还有一个匹配所有单词形式选项,它应该做你想做的事情,因为它会找到不同形式的单词,例如,如果它设置为 true,那么搜索“保证”也会找到‘保证’和‘保证’。我建议将其设置为 true 并将“匹配通配符”设置为 false 并测试此组合以查看它是否满足您的需求。
..
)。
例如:
the guarantee.
the guar..
因此,一次搜索将找到
..
.
。
(^w)的搜索,它代表空白,可以是单个空格或一系列连续空格。为了防止它毫无意义地替换每个单独的空格,我在其中添加了一个空格。因此,它将搜索任意 2 个以上空格,并将它们替换为单个空格。这使您可以一次性完成所有此类替换。