/
XLSXFile.swift
318 lines (264 loc) · 10.8 KB
/
XLSXFile.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2020 CoreOffice contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Max Desiatov on 27/10/2018.
//
import Foundation
import XMLCoder
import ZIPFoundation
@available(*, deprecated, renamed: "CoreXLSXError")
public typealias XLSXReaderError = CoreXLSXError
public enum CoreXLSXError: Error {
case dataIsNotAnArchive
case archiveEntryNotFound
case invalidCellReference
case unsupportedWorksheetPath
}
/** The entry point class that represents an open file handle to an existing `.xlsx` file on the
user's filesystem.
*/
public class XLSXFile {
private let archive: Archive
private let decoder: XMLDecoder = {
let result = XMLDecoder()
result.trimValueWhitespaces = false
result.shouldProcessNamespaces = true
return result
}()
/// Buffer size passed to `archive.extract` call
private let bufferSize: UInt32
/// - Parameters:
/// - filepath: path to the `.xlsx` file to be processed.
/// - bufferSize: ZIP archive buffer size in bytes. The default is 10MB.
/// You may need to set a bigger buffer size for bigger files.
/// - errorContextLength: The error context length. The default is `0`.
/// Non-zero length makes an error thrown from
/// the XML parser with line/column location repackaged with a context
/// around that location of specified length. For example, if an error was
/// thrown indicating that there's an unexpected character at line 3, column
/// 15 with `errorContextLength` set to 10, a new error type is rethrown
/// containing 5 characters before column 15 and 5 characters after, all on
/// line 3. Line wrapping should be handled correctly too as the context can
/// span more than a few lines.
public init?(
filepath: String,
bufferSize: UInt32 = 10 * 1024 * 1024,
errorContextLength: UInt = 0
) {
let archiveURL = URL(fileURLWithPath: filepath)
guard let archive = Archive(url: archiveURL, accessMode: .read) else {
return nil
}
self.archive = archive
self.bufferSize = bufferSize
decoder.errorContextLength = errorContextLength
}
#if swift(>=5.0)
/// - Parameters:
/// - data: content of the `.xlsx` file to be processed.
/// - bufferSize: ZIP archive buffer size in bytes. The default is 10MB.
/// You may need to set a bigger buffer size for bigger files.
/// - errorContextLength: The error context length. The default is `0`.
/// Non-zero length makes an error thrown from
/// the XML parser with line/column location repackaged with a context
/// around that location of specified length.
public init(
data: Data,
bufferSize: UInt32 = 10 * 1024 * 1024,
errorContextLength: UInt = 0
) throws {
guard let archive = Archive(data: data, accessMode: .read)
else { throw CoreXLSXError.dataIsNotAnArchive }
self.archive = archive
self.bufferSize = bufferSize
decoder.errorContextLength = errorContextLength
}
#endif
/// Parse a file within `archive` at `path`. Parsing result is
/// an instance of `type`.
func parseEntry<T: Decodable>(
_ pathString: String,
_ type: T.Type
) throws -> T {
let path = Path(pathString)
let entryPath = path.isRoot ?
path.components.joined(separator: "/") :
pathString
guard let entry = archive[entryPath] else {
throw CoreXLSXError.archiveEntryNotFound
}
var data = Data()
_ = try archive.extract(entry, bufferSize: bufferSize) {
data += $0
}
return try decoder.decode(type, from: data)
}
public func parseRelationships() throws -> Relationships {
decoder.keyDecodingStrategy = .convertFromCapitalized
return try parseEntry("_rels/.rels", Relationships.self)
}
/// Return an array of paths to relationships of type `officeDocument`
public func parseDocumentPaths() throws -> [String] {
try parseRelationships().items
.filter { $0.type == .officeDocument }
.map { $0.target }
}
public func parseStyles() throws -> Styles {
decoder.keyDecodingStrategy = .useDefaultKeys
return try parseEntry("xl/styles.xml", Styles.self)
}
public func parseSharedStrings() throws -> SharedStrings? {
decoder.keyDecodingStrategy = .useDefaultKeys
do {
return try parseEntry("xl/sharedStrings.xml", SharedStrings.self)
} catch CoreXLSXError.archiveEntryNotFound {
return nil
}
}
private func buildCommentsPath(forWorksheet path: String) throws -> String {
let pattern = "xl\\/worksheets\\/sheet(\\d+)[.]xml"
let regex = try NSRegularExpression(pattern: pattern, options: [])
let range = NSRange(location: 0, length: path.utf16.count)
if let match = regex.firstMatch(in: path, options: [], range: range),
let worksheetIdRange = Range(match.range(at: 1), in: path)
{
let worksheetId = path[worksheetIdRange]
return "xl/comments\(worksheetId).xml"
}
throw CoreXLSXError.unsupportedWorksheetPath
}
public func parseComments(forWorksheet path: String) throws -> Comments {
let commentsPath = try buildCommentsPath(forWorksheet: path)
decoder.keyDecodingStrategy = .useDefaultKeys
return try parseEntry(commentsPath, Comments.self)
}
/// Parse and return an array of workbooks in this file.
/// Worksheet names can be read as properties on the `Workbook` model type.
public func parseWorkbooks() throws -> [Workbook] {
let paths = try parseDocumentPaths()
decoder.keyDecodingStrategy = .useDefaultKeys
return try paths.map {
try parseEntry($0, Workbook.self)
}
}
/** Return pairs of parsed document paths with corresponding relationships.
**Deprecation warning**: this function doesn't handle root paths correctly,
even though some XLSX files do contain root paths instead of relative
paths. Use `parseDocumentRelationships(path:)` instead.
*/
@available(*, deprecated, renamed: "parseDocumentRelationships(path:)")
public func parseDocumentRelationships() throws
-> [([Substring], Relationships)]
{
decoder.keyDecodingStrategy = .convertFromCapitalized
return try parseDocumentPaths()
.compactMap { path -> ([Substring], Relationships)? in
let originalComponents = path.split(separator: "/")
var components = originalComponents
components.insert("_rels", at: 1)
guard let filename = components.last else { return nil }
components[components.count - 1] =
Substring(filename.appending(".rels"))
let relationships = try parseEntry(
components.joined(separator: "/"),
Relationships.self
)
return (originalComponents, relationships)
}
}
/// Return parsed path with a parsed relationships model for a document at
/// given path. Use `parseDocumentPaths` first to get a string path to pass
/// as an argument to this function.
public func parseDocumentRelationships(path: String) throws
-> (Path, Relationships)
{
decoder.keyDecodingStrategy = .convertFromCapitalized
let originalPath = Path(path)
var components = originalPath.components
components.insert("_rels", at: 1)
guard let filename = components.last else { fatalError() }
components[components.count - 1] =
Substring(filename.appending(".rels"))
let relationships = try parseEntry(
components.joined(separator: "/"),
Relationships.self
)
return (originalPath, relationships)
}
/// Parse and return an array of worksheets in this XLSX file with their corresponding names.
public func parseWorksheetPathsAndNames(
workbook: Workbook
) throws -> [(name: String?, path: String)] {
try parseDocumentPaths().map {
try parseDocumentRelationships(path: $0)
}.flatMap { path, relationships -> [(name: String?, path: String)] in
let worksheets = relationships.items.filter { $0.type == .worksheet }
guard !path.isRoot else { return worksheets.map { (name: nil, path: $0.target) } }
// .rels file has paths relative to its directory,
// storing that path in `pathPrefix`
let pathPrefix = path.components.dropLast().joined(separator: "/")
let sheetIDs = Dictionary(uniqueKeysWithValues: workbook.sheets.items.compactMap { sheet in
sheet.name.flatMap { (sheet.relationship, $0) }
})
return worksheets.map { (name: sheetIDs[$0.id], path: $0.path(from: pathPrefix)) }
}
}
/// Parse and return an array of worksheets in this XLSX file.
public func parseWorksheetPaths() throws -> [String] {
try parseDocumentPaths().map {
try parseDocumentRelationships(path: $0)
}.flatMap { path, relationships -> [String] in
let worksheets = relationships.items.filter { $0.type == .worksheet }
guard !path.isRoot else { return worksheets.map { $0.target } }
// .rels file has paths relative to its directory,
// storing that path in `pathPrefix`
let pathPrefix = path.components.dropLast().joined(separator: "/")
return worksheets.map { $0.path(from: pathPrefix) }
}
}
/// Parse a worksheet at a given path contained in this XLSX file.
public func parseWorksheet(at path: String) throws -> Worksheet {
decoder.keyDecodingStrategy = .useDefaultKeys
return try parseEntry(path, Worksheet.self)
}
/// Return all cells that are contained in a given worksheet and set of rows.
@available(*, deprecated, renamed: "Worksheet.cells(atRows:)")
public func cellsInWorksheet(at path: String, rows: [Int]) throws
-> [Cell]
{
let ws = try parseWorksheet(at: path)
return ws.data?.rows.filter { rows.contains(Int($0.reference)) }
.reduce([]) { $0 + $1.cells } ?? []
}
/// Return all cells that are contained in a given worksheet and set of
/// columns. This overloaded version is deprecated, you should pass
/// an array of `ColumnReference` values as `columns` instead of an array
/// of `String`s.
@available(*, deprecated, renamed: "Worksheet.cells(atColumns:)")
public func cellsInWorksheet(at path: String, columns: [String]) throws
-> [Cell]
{
let ws = try parseWorksheet(at: path)
return ws.data?.rows.map {
let rowReference = $0.reference
let targetReferences = columns
.compactMap { (c: String) -> CellReference? in
guard let columnReference = ColumnReference(c) else { return nil }
return CellReference(columnReference, rowReference)
}
return $0.cells.filter { targetReferences.contains($0.reference) }
}
.reduce([]) { $0 + $1 } ?? []
}
}