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

查看:425
本文介绍了在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进行交互文件。脚本Excel通过Applescript似乎是唯一的方法。

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一切都很好,而且快速的将是一个更容易的过渡,我觉得。

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的语法可以在同一个源文件中使用。比脚本桥 NSAppleScript 更方便。

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



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

创建一个AppleScript源文件表单AppleScriptObjC模板

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天全站免登陆