-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SharedStrings model, parse sharedStrings.xml (#8)
As reported in #21, there's no obvious way to get values for some values. That's caused by the fact that some of the values are stored in a separate file, which is usually located at the path `sharedStrings.xml`. This can be parsed to a corresponding model type and then processed by users of `CoreXLSX` to get the string values. * Add SharedStrings model, general cleanup * Add link to Open XML SDK docs for SharedStrings * Fix rebase conflicts * Add public `parseSharedStrings` API with tests
- Loading branch information
1 parent
bd48966
commit bffbcd8
Showing
11 changed files
with
209 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// SharedStrings.swift | ||
// CoreXLSX | ||
// | ||
// Created by Max Desiatov on 18/11/2018. | ||
// | ||
|
||
/// Attributes and nodes are documented at this url: | ||
/// https://docs.microsoft.com/en-us/office/open-xml/working-with-the-shared-string-table | ||
public struct SharedStrings: Codable, Equatable { | ||
public struct Item: Codable, Equatable { | ||
public let text: String? | ||
public let richText: RichText? | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case text = "t" | ||
case richText = "r" | ||
} | ||
} | ||
|
||
public let uniqueCount: UInt | ||
public let items: [Item] | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case items = "si" | ||
case uniqueCount | ||
} | ||
} | ||
|
||
public struct RichText: Codable, Equatable { | ||
public struct Family: Codable, Equatable { | ||
public let value: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case value = "val" | ||
} | ||
} | ||
|
||
public struct Scheme: Codable, Equatable { | ||
public let value: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case value = "val" | ||
} | ||
} | ||
|
||
public struct Size: Codable, Equatable { | ||
public let value: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case value = "val" | ||
} | ||
} | ||
|
||
public struct Color: Codable, Equatable { | ||
let theme: String? | ||
let rgb: String? | ||
} | ||
|
||
public struct Font: Codable, Equatable { | ||
public let value: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case value = "val" | ||
} | ||
} | ||
|
||
public struct Properties: Codable, Equatable { | ||
public let size: Size | ||
public let color: Color | ||
public let font: Font | ||
public let family: Family | ||
public let scheme: Scheme | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case size = "sz" | ||
case color | ||
case font = "rFont" | ||
case family | ||
case scheme | ||
} | ||
} | ||
|
||
public let properties: Properties | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Tests/CoreXLSXTests/CellReferenceTests.swift → Tests/CoreXLSXTests/CellReference.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 9 additions & 2 deletions
11
Tests/CoreXLSXTests/CoreXLSXTests.swift → Tests/CoreXLSXTests/CoreXLSX.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Tests/CoreXLSXTests/RelationshipsTests.swift → Tests/CoreXLSXTests/Relationships.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// SharedStrings.swift | ||
// CoreXLSXTests | ||
// | ||
// Created by Max Desiatov on 30/11/2018. | ||
// | ||
|
||
import XCTest | ||
import XMLCoder | ||
@testable import CoreXLSX | ||
|
||
private let parsed = SharedStrings(uniqueCount: 18, items: [ | ||
SharedStrings.Item(text: "Table 1", richText: nil), | ||
SharedStrings.Item(text: "Item", richText: nil), | ||
SharedStrings.Item(text: "Name", richText: nil), | ||
SharedStrings.Item(text: "Amount", richText: nil), | ||
SharedStrings.Item(text: "Name:", richText: nil), | ||
SharedStrings.Item(text: "Subtotal:", richText: nil), | ||
SharedStrings.Item(text: "Andy", richText: nil), | ||
SharedStrings.Item(text: "Item 1", richText: nil), | ||
SharedStrings.Item(text: "Item 2", richText: nil), | ||
SharedStrings.Item(text: "Item 3", richText: nil), | ||
SharedStrings.Item(text: "Item 4", richText: nil), | ||
SharedStrings.Item(text: "Item 5", richText: nil), | ||
SharedStrings.Item(text: "Chloe", richText: nil), | ||
SharedStrings.Item(text: "Item 6", richText: nil), | ||
SharedStrings.Item(text: "Item 7", richText: nil), | ||
SharedStrings.Item(text: "Item 8", richText: nil), | ||
SharedStrings.Item(text: "Item 9", richText: nil), | ||
SharedStrings.Item(text: "Item 10", richText: nil), | ||
] | ||
) | ||
|
||
|
||
final class SharedStringsTests: XCTestCase { | ||
func testSharedStrings() { | ||
do { | ||
guard let file = | ||
XLSXFile(filepath: "\(currentWorkingPath)/categories.xlsx") else { | ||
XCTAssert(false, "failed to open the file") | ||
return | ||
} | ||
|
||
let sharedStrings = try file.parseSharedStrings() | ||
|
||
// check each individual item so that it's easier to debug when something | ||
// goes wrong | ||
for (i, item) in sharedStrings.items.enumerated() { | ||
XCTAssertEqual(item, parsed.items[i]) | ||
} | ||
|
||
// check the complete value anyway to make sure all properties are equal | ||
XCTAssertEqual(sharedStrings, parsed) | ||
} catch { | ||
XCTAssert(false, "unexpected error \(error)") | ||
} | ||
} | ||
|
||
static let allTests = [ | ||
("testSharedStrings", testSharedStrings), | ||
] | ||
} |
4 changes: 2 additions & 2 deletions
4
Tests/CoreXLSXTests/WorkbookTests.swift → Tests/CoreXLSXTests/Workbook.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Tests/CoreXLSXTests/WorksheetTests.swift → Tests/CoreXLSXTests/Worksheet.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// WorksheetTests.swift | ||
// Worksheet.swift | ||
// CoreXLSXTests | ||
// | ||
// Created by Max Desiatov on 24/11/2018. | ||
|