在 Apple Swift 中解析 Excel 数据 [英] Parsing Excel Data in Apple Swift

查看:59
本文介绍了在 Apple Swift 中解析 Excel 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的工作流程涉及使用 Applescript 来分隔 Excel 数据并将其格式化为纯文本文件.我们正在推动全 Swift 环境,但我还没有找到任何类型的工具包来将我的 Excel 数据解析为 Swift.

My current workflow involves using Applescript to essentially delimit Excel data and format it into plain text files. We're pushing towards an all Swift environment, but I haven't yet found any sort of kits for parsing my Excel data into Swift.

我唯一能想到的就是用C什么的,把它包起来,但这并不理想.对于解析这些数据以在 Swift 中使用有什么更好的建议吗?

The only thing I can think of is to use C or something and wrap it, but that's not ideal. Any better suggestions for parsing this data for use in Swift?

目标是消除 Applescript,但我不确定在仍然与 Excel 文件交互的同时是否可行.通过 Applescript 编写 Excel 脚本似乎是唯一的方法.

The goal is to eliminate Applescript, but I'm not sure if that will be possible while still interacting with Excel files. Scripting Excel via Applescript seems to be the only method.

我没有从这个工作流程中删除 Excel 的选项.这就是数据进入应用程序的方式,因此我必须包含它.

I don't have the option of eliminating Excel from this workflow. This is how the data will be coming to the application, thus I have to include it.

能够简化解析这些数据然后处理它的过程将是最重要的.我知道 Applescript 过去在帮助我处理它方面做得很好;然而,它对我来说有点太封闭了.

Being able to streamline the process of parsing this data then processing it will be paramount. I know Applescript has been good in the past with helping me to process it; however, it's getting a little too closed-off for me.

我一直在考虑用 Swift/Cocoa 编写一些东西,但这仍然可能需要使用 Applescript 提取数据,对吗?

I've been looking at writing something in Swift/Cocoa, but that still might require the data to be extracted with an Applescript, right?

推动 Swift 的一大优势是可读性.我不太了解 Objective-C,我觉得 swift 会更容易转换.

A big plus for pushing Swift is the readability. I don't know Objective-C all that well, and swift would be an easier transition, I feel.

我在 PC 上的工作流程一直在使用 COM 对象,如前所述,它在 Mac Excel 应用程序中不可用.我现在只是在寻找数据提取.一些以前的应用程序确实在应用程序内进行了处理,但我希望使这个非常独立,因此所有处理都在我正在开发的应用程序中进行.从 .XLS 或 .XLSX 文件中提取数据后,我将通过 RegEx 进行一些文本编辑,可能还会进行一些数字运算.没什么太疯狂的.截至目前,它将在客户端运行,但我希望将其扩展到服务器进程.

My workflow on PC has been using the COM object, which as has been said, isn't available in the Mac Excel app. I'm only looking for data extraction at this point. Some previous apps did processing within the app, but I'm looking to make this very self-contained, thus all processing within the app I'm developing. Once the data is extracted from the .XLS or .XLSX files, I'll be doing some text editing via RegEx and perhaps a little number crunching. Nothing too crazy. As of now, it will run on the client side, but I'm looking to extend this to a server process.

推荐答案

在 Mac OS X 10.6 Snow Leopard 中,Apple 引入了 AppleScriptObjC 框架,这使得 Cocoa 和 AppleScript 之间的交互变得非常容易.AppleScript 代码和类似 Objective-C 的语法可以在同一个源文件中使用.它比Scripting BridgeNSAppleScript 方便多了.

In Mac OS X 10.6 Snow Leopard Apple introduced the AppleScriptObjC framework which makes it very easy to interact between Cocoa and AppleScript. AppleScript code and a Objective-C like syntax can be used in the same source file. It's much more convenient than Scripting Bridge and NSAppleScript.

AppleScriptObjC 不能直接在 Swift 中使用,因为 NSBundle 的命令 loadAppleScriptObjectiveCScripts 没有桥接到 Swift.

AppleScriptObjC cannot be used directly in Swift because the command loadAppleScriptObjectiveCScripts of NSBundle is not bridged to Swift.

但是,您可以使用 Objective-C 桥接类,例如

However you can use a Objective-C bridge class for example

@import Foundation;
@import AppleScriptObjC;

@interface NSObject (Excel)
- (void)openExcelDocument:(NSString *)filePath;
- (NSArray *)valueOfUsedRange;

@end

@interface ASObjC : NSObject

+ (ASObjC *)sharedASObjC;

@property id Excel;

@end

ASObjC.m

#import "ASObjC.h"

@implementation ASObjC

+ (void)initialize
{
    if (self == [ASObjC class]) {
        [[NSBundle mainBundle] loadAppleScriptObjectiveCScripts];
    }
}

+ (ASObjC *)sharedASObjC
{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[ASObjC alloc] init];
    });

    return sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _Excel = NSClassFromString(@"ASExcel");
    }
    return self;
}

@end

从 AppleScriptObjC 模板创建一个 AppleScript 源文件

Create a AppleScript source file form the AppleScriptObjC template

script ASExcel
  property parent: class "NSObject"

  on openExcelDocument:filePath
    set asFilePath to filePath as text
    tell application "Microsoft Excel"
      set sourceBook to open workbook workbook file name asFilePath
      repeat
        try
          get workbooks
          return
        end try
        delay 0.5
      end repeat
    end tell
  end openDocument

  on valueOfUsedRange()
    tell application "Microsoft Excel"
      tell active sheet
        set activeRange to used range
        return value of activeRange
      end tell
    end tell
  end valueOfUsedRange

end script

如有必要,可链接到 AppleScriptObjC 框架.
创建桥接头并导入 ASObjC.h

Link to the AppleScriptObjC framework if necessary.
Create the Bridging Header and import ASObjC.h

然后你可以用 Swift 调用 AppleScriptObjC

Then you can call AppleScriptObjC from Swift with

 ASObjC.sharedASObjC().Excel.openExcelDocument("Macintosh HD:Users:MyUser:Path:To:ExcelFile.xlsx")

let excelData = ASObjC.sharedASObjC().Excel.valueOfUsedRange() as! Array<[String]>

这篇关于在 Apple Swift 中解析 Excel 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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