如何使用 AppleScript 保存为 excel 文件
Posted
技术标签:
【中文标题】如何使用 AppleScript 保存为 excel 文件【英文标题】:How to save as a excel file using AppleScript 【发布时间】:2021-12-31 19:15:14 【问题描述】:我想使用 Excel 工作簿(.xlsx) 格式在同一文件夹中另存为 Excel 文件,而不覆盖打开的文档
输入文件类型 --> .xls
输出文件类型 --> .xlsx
告诉应用程序“Finder”
set theWorkbookFile to (choose file with prompt "Select Excel file to process:" of type "xls", "xlsx" without invisibles)
set theWorkbookPath to theWorkbookFile as Unicode text
tell application "Microsoft Excel"
activate
open theWorkbookFile
set myBorders to border top, border bottom, border left, border right
tell active workbook to tell active sheet
tell used range to set rowsCount, columnsCount to count of rows, count of columns
repeat with rowValue from 1 to rowsCount
set theCell to cell ("A1" & ":K" & rowValue)
repeat with i from 1 to 4
set theBorder to get border theCell which border (item i of myBorders)
set weight of theBorder to border weight thin
end repeat
end repeat
repeat with rowValueColor from 1 to rowsCount
tell interior object of range ("B1" & ":B" & rowValueColor) of active sheet
set color to 255, 242, 204
end tell
tell interior object of range ("D1" & ":D" & rowValueColor) of active sheet
set color to 255, 242, 204
end tell
tell interior object of range ("H1" & ":H" & rowValueColor) of active sheet
set color to 255, 242, 204
end tell
tell interior object of range ("I1" & ":I" & rowValueColor) of active sheet
set color to 255, 242, 204
end tell
end repeat
(*
tell application "System Events"
keystroke "s" using shift down, command down -- shift-command-left
end tell
*)
end tell
end tell
说完
【问题讨论】:
为什么选择 Applescript? Excel有vba。可以用 Numbers 理解 Applescript…… @SolarMike 感谢您的建议,但我正在尝试与另一个脚本集成。 【参考方案1】:您为什么要使用 GUI 脚本? Excel 的 AppleScript 界面非常广泛,尽管一团糟。我不知道它在新版本中是否有所改进,但传统上你会像这样使用save workbook as
命令:
set xlsxPath to "/Users/FOO/test.xlsx"
-- very old apps may not accept POSIX file values or POSIX path strings, requiring long-obsoleted HFS paths instead
set hfsPath to xlsxPath as POSIX file as string
tell application "Microsoft Excel"
activate
tell workbook 1
save workbook as filename hfsPath file format Excel XML file format
end tell
end tell
(没有最新版本来查看它是否已更改,但希望可以帮助您入门。)
【讨论】:
感谢脚本,但如果我使用“选择带提示的文件”,posix 文件不接受。 我假设您的意思是choose file name
,因为它可以让您选择一个尚不存在的文件。该命令返回一个POSIX file
值,所以使用set hfsPath to (choose file name) as string
。如果您确实使用choose file
,则返回一个alias
值,因此再次将其直接强制转换为字符串以获取HFS 路径字符串。 (是的,在 AS 中指定文件绝对是一团糟。)【参考方案2】:
终于有了分隔符:这是使用文件提示符将文档保存在同一文件夹中的代码。
告诉应用程序“Finder”
set theWorkbookFile to (choose file with prompt "Select Excel file to process:" of type "xls", "xlsx" without invisibles)
set theWorkbookPath to theWorkbookFile as Unicode text
--text delimiter saving a document inside same path
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set folderPath to (text 1 thru text item -2 of theWorkbookPath) & ":"
set AppleScript's text item delimiters to saveTID
set theOutputFilePath to folderPath & "Output.xlsx"
tell application "Microsoft Excel"
activate
open theWorkbookFile
set myBorders to border top, border bottom, border left, border right
tell active workbook to tell active sheet
tell used range to set rowsCount, columnsCount to count of rows, count of columns
repeat with rowValue from 1 to rowsCount
set theCell to cell ("A1" & ":K" & rowValue)
repeat with i from 1 to 4
set theBorder to get border theCell which border (item i of myBorders)
set weight of theBorder to border weight thin
end repeat
end repeat
repeat with rowValueColor from 1 to rowsCount
tell interior object of range ("B1" & ":B" & rowValueColor) of active sheet
set color to 255, 242, 204
end tell
tell interior object of range ("D1" & ":D" & rowValueColor) of active sheet
set color to 255, 242, 204
end tell
tell interior object of range ("H1" & ":H" & rowValueColor) of active sheet
set color to 255, 242, 204
end tell
tell interior object of range ("I1" & ":I" & rowValueColor) of active sheet
set color to 255, 242, 204
end tell
end repeat
end tell
save active workbook in theOutputFilePath as Excel XML file format
close workbooks
display alert "Processed Completed"
end tell
说完
【讨论】:
答案(或问题)中是否需要“边界”内容?它与保存为不同文件格式的问题并不真正相关(并且比实际答案占用更多空间)。请看How to create a Minimal, Reproducible Example。 另外,在你的开始和结束的tell语句前面放4个空格;这会将它们合并到代码格式化块中。此外,在第 2 行中,您不需要 'unicode',因为默认情况下就是这样;实际上,您可以将as text
附加到第一行,然后您可以删除整个 unicode 行。以上是关于如何使用 AppleScript 保存为 excel 文件的主要内容,如果未能解决你的问题,请参考以下文章
在 Dock 上使用 AppleScript 为应用程序添加自定义菜单项
OS X Messages AppleScript 处理程序:保存图像?
如何将 traceback / sys.exc_info() 值保存在变量中?