【问题标题】:Trying to Use AppleScript to Convert .xls and .xlsx to .txt (Tab Delimited), Needs Fixing尝试使用 AppleScript 将 .xls 和 .xlsx 转换为 .txt(制表符分隔),需要修复
【发布时间】:2013-08-18 01:10:45
【问题描述】:

我从另一个线程得到的答案复制了这个。我正在尝试将 ~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

这只是打开一个对话框,什么都不做。即使它是一个液滴,当我将一个文件拖到它上面时,什么也没有发生。

【问题讨论】:

  • 此脚本正在使用Droplets (macscripter.net/viewtopic.php?id=24772)。将其保存为可执行应用程序,然后将文件拖放到上面(这就是显示对话框试图告诉您的内容)

标签: excel macos vba applescript ms-office


【解决方案1】:

在 Applescript 中,run 处理程序在脚本或应用程序正常运行时运行。同时,当某些文件或文件是应用程序的dropped onto the icon 并且文件被设置为变量VarName 时,open VarName 处理程序将运行。您发布的脚本巧妙地将display dialog 放在on run 处理程序中,以尝试帮助您理解这种用法​​。而是将脚本保存为应用程序,然后将文件放到它上面。

编辑:

在终于拥有 Excel 2011 的 Mountain Lion 机器上进行了快速测试后(我没有意识到我有多么复杂):

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

【讨论】:

  • 太棒了!谢谢!有什么我可以添加到脚本中以导出到不同文件夹的吗?另外,如何更改文件名以摆脱 .xls.txt 扩展名并将其更改为 .txt?
  • 所以这基本上用文件名“文档 1”、“文档 2”等在我的桌面上喷出所有东西。有没有办法为一大堆文件指定输出目标,同时仍然保留他们的原名?
  • @Jart 抱歉,我试图证明您设置的任何路径 workbookName 都将是保存它的位置。如果您想要不同的目的地,只需更改该变量(您现在会注意到它只是进入桌面,然后将其命名为“Doc k.txt”,其中 k 是当前迭代)
  • Err...你能澄清一下吗?我不擅长VBA。假设我在桌面上名为“Before”的文件夹中有一个名为 spreadsheet.xlsx 的文件。我希望它在我桌面上名为“After”的文件夹中以制表符分隔的形式命名为电子表格.txt(而不是电子表格.xlsx.txt)。
  • @Jart 经过一番折腾,我刚刚更新了我的答案。这仅适用于xls,不适用于xlsx。对于xlsx,将text 1 thru -4 更改为text 1 thru -5。我可以添加一个 if 语句来检查它,但我认为你所有的文件可能都是相同的格式,所以不需要添加额外的代码
猜你喜欢
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多