我在一个单独的库中定义了一次这个函数(以及许多其他函数)
on getActiveBookAndSheet()
try
tell application "Microsoft Excel"
set theBook to workbook (get name of active workbook)
set theSheet to worksheet (get name of active sheet) of theBook
# set theSheet to worksheet (get entry index of active sheet of theBook) -- alternative
return {theBook, theSheet}
end tell
on error
error "Could't find any opened document in Excel."
end try
end getActiveBookAndSheet
将此作为 ExcelLib.app 保存在“Computer Scripts”文件夹中的“Common”文件夹中
在每个与 Excel 相关的 applescript 的开头,我添加了这一行:
set myExcelLib to load script alias ((path to library folder as string) & "Scripts:Common:ExcelLib.app")
到了拿到工作簿和工作表的时候,我就用这个:
set {myBook, mySheet} to myExcelLib's getActiveBookAndSheet()
然后,每次您想处理工作表的特定范围时,只需这样做:
set value of range "A2:F3" of mySheet to "etc." -- for a single line
或
tell mySheet
set value of range "A2:F3" to "etc." -- if you need to perform a whole list of operations
end