Applescript:打开 Excel 文件并以不同格式保存(相同名称和路径)

编程入门 行业动态 更新时间:2024-10-26 14:36:24
本文介绍了Applescript:打开 Excel 文件并以不同格式保存(相同名称和路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 .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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 14:06:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并以   路径   名称   格式   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!