Applescript:打开 Excel 文件并以不同格式保存(相同名称和路径) [英] Applescript: Open Excel File and Save in a Different Format (same name and path)

查看:34
本文介绍了Applescript:打开 Excel 文件并以不同格式保存(相同名称和路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .xls 文件的文件夹,其中一些是真正的 excel 文件,一些是 excel xml 文件(但仍然带有 .xls 扩展名).为了在单独的程序中处理这些文件,我试图将它们全部转换为一致的格式 (Excel 98-2004),并希望使用 applescript 来执行此操作,同时保留原始文件名和目录结构.

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!

刚刚注意到回复文本中缺少文件格式参数,所以我猜问题出在那里.

Just noticed that the file format parameter is missing from the reply text, so I'm guessing the problem is in there.

推荐答案

知道了.

在尝试保存之前,我需要设置一个工作簿名称变量以获取活动工作簿的全名.这是因为 Excel 在打开工作表时分配的工作簿名称有些不一致.最终代码如下所示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆