Applescript:打开Excel文件并以不同的格式保存(同名和路径) [英] Applescript: Open Excel File and Save in a Different Format (same name and path)
问题描述
到目前为止,我有一个工作循环和文件列表,除了保存功能之外,我完成了所有的任务:
将file_Name设置为/Users/me/path/to/data.xls
告诉应用程序Microsoft Excel
激活
打开file_Name
将工作簿保存为工作簿(活动工作簿)filename file_Name文件格式(Excel98to2004文件格式),覆盖
关闭工作簿
退出
结束告诉
当我运行此代码时,我收到以下回复:
告诉应用程序Microsoft Excel
激活
打开/Users/drewmcdonald/Desktop/Madhupur_10February2014.xls
获取活动工作簿
- >活动工作簿
将工作簿保存为工作簿(活动工作簿)filename/Users/drewmcdonald/Desktop/test.xlsx
- >缺少值
关闭每个工作簿
退出
结束告诉
电子表格已成功打开和关闭,但文件格式没有更改,我不知道该怎么做缺少值错误。任何想法如何让这个工作?谢谢!
编辑:只是注意到文本格式参数在回复文本中丢失,所以我猜这个问题在那里。
得到它。
我需要 set
工作簿名称变量获取活动工作簿的全名
,然后再尝试保存。这是因为Excel分配的工作簿名称在打开工作表时有些不一致。最终代码如下所示:
将file_Name设置为/Users/me/path/to/data.xls
告诉应用程序Microsoft Excel
激活
打开file_Name
设置wkbk_name获取活动工作簿的全名
将工作簿另存为工作簿wkbk_name文件名file_Name文件格式Excel98to2004文件格式与覆盖
关闭工作簿
退出
结束告诉
I have a folder of .xls files of which some are genuine excel files and some are excel xml files (but still with the .xls extension). To work with the files in a separate program, I'm trying to convert them all to a consistent format (Excel 98-2004) and want to use an applescript to do this while retaining the original file names and directory structure.
As of now, I have a working loop and list of files, and am accomplishing everything but the save function:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
save workbook as workbook (active workbook) filename file_Name file format (Excel98to2004 file format) with overwrite
close workbooks
quit
end tell
When I run this code, I get the following replies:
tell application "Microsoft Excel"
activate
open "/Users/drewmcdonald/Desktop/Madhupur_10February2014.xls"
get active workbook
--> active workbook
save workbook as workbook (active workbook) filename "/Users/drewmcdonald/Desktop/test.xlsx"
--> missing value
close every workbook
quit
end tell
The spreadsheets are successfully opening and closing, but the file format is not changing and I don't know what to make of the "missing value" error. Any idea how to get this to work? Thanks!
EDIT: Just noticed that the file format parameter is missing from the reply text, so I'm guessing the problem is in there.
Got it.
I needed to set
a workbook name variable to get full name of active workbook
before trying to save it. This is because the workbook name that Excel assigns as it opens sheets is somewhat inconsistent. Final code looks like this:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
set wkbk_name to get full name of active workbook
save workbook as workbook wkbk_name filename file_Name file format Excel98to2004 file format with overwrite
close workbooks
quit
end tell
这篇关于Applescript:打开Excel文件并以不同的格式保存(同名和路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!