excel - 尝试使用 AppleScript 将 .xls 和 .xlsx 转换为 .txt(制表符分隔),需要修复

标签 excel macos vba applescript ms-office

我从另一个线程得到的答案中复制了这个。我正在尝试将 ~300 .xls 和 .xlsx 文件转换为制表符分隔。它们都在同一个文件夹中。如果有人知道更好的方法,请告诉我。

property type_list : {"XLS6", "XLS7", "XLS8", "XLSX"}
property extension_list : {"xls", "xlsx"}


on open these_workbooks
repeat with k from 1 to the count of these_workbooks
    set this_item to item k of these_workbooks
    set the item_info to info for this_item

    --this if statement tests to make sure the items you're converting are Excel spreadsheets and not folders or aliases
    if (folder of the item_info is false) and (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then

        tell application "Finder" to open this_item

        tell application "Microsoft Excel"
            --this just tacks on ".txt" to your file name
            set workbookName to (name of active workbook & ".txt")
            --save the current open workbook as a tab-delimited text file
            tell active workbook to save workbook as filename workbookName file format text Mac file format
            close active workbook saving no
        end tell
    end if
end repeat
end open

on run
    display dialog "Drop Excel files onto this icon."
end run

这只是打开一个对话框,什么也不做。尽管它本来就是一个 Droplet,但当我将文件拖到其中时什么也没有发生。

最佳答案

在 Applescript 中,当脚本或应用程序正常运行时,run 处理程序就会运行。同时,当某些文件或文件 dropped onto the icon 时,open VarName 处理程序将运行。应用程序和文件的名称被设置为变量 VarName。您发布的脚本巧妙地将显示对话框放置在on run处理程序中,以尝试帮助您理解这种用法​​。相反,请将脚本另存为应用程序,然后将文件放入其中。

编辑:

在 Mountain Lion 机器上进行快速测试后,终于安装了 Excel 2011(我没有意识到我的操作过于复杂):

property type_list : {"XLS6", "XLS7", "XLS8", "XLSX"}
property extension_list : {"xls", "xlsx"}


on open these_workbooks
    repeat with k from 1 to the count of these_workbooks
        set this_item to item k of these_workbooks
        set the item_info to info for this_item

        --this if statement tests to make sure the items you're converting are Excel spreadsheets and not folders or aliases
        if (folder of the item_info is false) and (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then

            tell application "Finder" to open this_item

            tell application "Microsoft Excel"
                --this just tacks on ".txt" to your file name
                set workbookName to (path to desktop as string) & "After:" & (name of active workbook & ".txt")
                display dialog workbookName
                --save the current open workbook as a tab-delimited text file
                tell active workbook to save workbook as filename workbookName file format text Mac file format
                close active workbook saving no
            end tell
        end if
    end repeat
end open

on run
    display dialog "Drop Excel files onto this icon."
end run

关于excel - 尝试使用 AppleScript 将 .xls 和 .xlsx 转换为 .txt(制表符分隔),需要修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18259576/

相关文章:

php - Cakephp 2.3.0,CsvHelper 不会在线生成下载弹出窗口,但可以在 localhost 中工作

excel - 带下拉列表的 VBA 自定义 Excel 函数

sql - 如何在 Access 2007 中查询截止日期

css - 表单字段概述

ms-access - 声明和调用 Sleep API

excel - 如何获取默认的 Excel 工作表字体?

excel - SSAS 2005、Excel 2007、参差不齐的层次结构

swift - 主窗口关闭时如何退出应用程序

macos - "Enter Full Screen"wxWidgets 中的热键

excel - 暂时关闭 Excel 备份