Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add font(in:) and format(in:) functions #135

Merged
merged 4 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CoreXLSX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,12 @@
attributes = {
LastUpgradeCheck = 9999;
TargetAttributes = {
"CoreXLSX::CoreXLSX" = {
LastSwiftMigration = 1220;
};
"CoreXLSX::CoreXLSXTests" = {
LastSwiftMigration = 1220;
};
D15021D021A1CA7D00BFA4FC = {
CreatedOnToolsVersion = 10.1;
};
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ let styles = try file.parseStyles()
let fonts = styles.fonts?.items.compactMap { $0.name?.value }
```

To get formatting for a given cell, use `format(in:)` and `font(in:)` functions, passing it
the result of `parseStyles`:

```swift
let styles = try file.parseStyles()
let format = worksheet.data?.rows.first?.cells.first?.format(in: styles)
let font = worksheet.data?.rows.first?.cells.first?.font(in: styles)
```

## Reporting compatibility issues

If you stumble upon a file that can't be parsed, please [file an
Expand Down
9 changes: 2 additions & 7 deletions Sources/CoreXLSX/Worksheet/Cell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ public enum CellType: String, Codable {
public struct Cell: Codable, Equatable {
public let reference: CellReference
public let type: CellType?

// FIXME: Attribute "s" in a cell is an index into the styles table,
// while the cell type "s" corresponds to the shared string table.
// Can XMLCoder distinguish between an attribute and an
// element having the same name?
public let s: String?
public let styleIndex: Int?

/** Not every string in a cell is an inline string. You should use `stringValue(_: SharedStrings)`
on the `Cell` type, supplying it the result of `parseSharedStrings()` called on your `XLSXFile`
Expand All @@ -68,6 +63,6 @@ public struct Cell: Codable, Equatable {
case inlineString = "is"
case reference = "r"
case type = "t"
case s
case styleIndex = "s"
}
}
12 changes: 12 additions & 0 deletions Sources/CoreXLSX/Worksheet/CellQueries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,16 @@ public extension Cell {

return referenceCalendar.date(byAdding: .second, value: seconds, to: addedDays)
}

func format(in styles: Styles) -> Format? {
guard let styleIndex = styleIndex else { return nil }

return styles.cellFormats?.items[styleIndex]
}

func font(in styles: Styles) -> Font? {
guard let fontID = format(in: styles)?.fontId else { return nil }

return styles.fonts?.items[fontID]
}
}
2 changes: 1 addition & 1 deletion Sources/CoreXLSX/XLSXFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public class XLSXFile {
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 worksheetIdRange = Range(match.range(at: 1), in: path)
{
let worksheetId = path[worksheetIdRange]
return "xl/comments\(worksheetId).xml"
Expand Down
20 changes: 10 additions & 10 deletions Tests/CoreXLSXTests/CellQueries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,79 +25,79 @@ final class CellQueriesTests: XCTestCase {
Cell(
reference: CellReference(ColumnReference("I")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.39583333333212067"
),
Cell(
reference: CellReference(ColumnReference("J")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.54166666666787933"
),
Cell(
reference: CellReference(ColumnReference("K")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.625"
),
Cell(
reference: CellReference(ColumnReference("L")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.8125"
),
Cell(
reference: CellReference(ColumnReference("O")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.625"
),
Cell(
reference: CellReference(ColumnReference("P")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.75"
),
Cell(
reference: CellReference(ColumnReference("U")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.33333333333212067"
),
Cell(
reference: CellReference(ColumnReference("V")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.45833333333212067"
),
Cell(
reference: CellReference(ColumnReference("Y")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.33333333333212067"
),
Cell(
reference: CellReference(ColumnReference("Z")!, 2),
type: nil,
s: Optional("7"),
styleIndex: 7,
inlineString: nil,
formula: nil,
value: "0.45833333333212067"
Expand Down
16 changes: 8 additions & 8 deletions Tests/CoreXLSXTests/CellReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class CellReferenceTests: XCTestCase {
c1,
Cell(
reference: cr1,
type: nil, s: nil, inlineString: nil, formula: nil,
type: nil, styleIndex: nil, inlineString: nil, formula: nil,
value: nil
)
)
Expand All @@ -58,7 +58,7 @@ final class CellReferenceTests: XCTestCase {
c2,
Cell(
reference: cr2,
type: nil, s: nil, inlineString: nil, formula: nil,
type: nil, styleIndex: nil, inlineString: nil, formula: nil,
value: nil
)
)
Expand Down Expand Up @@ -89,8 +89,8 @@ final class CellReferenceTests: XCTestCase {

func testColumnReferenceDistance() {
guard let a = ColumnReference("A"), let z = ColumnReference("z"),
let aa = ColumnReference("AA"), let az = ColumnReference("Az"),
let ba = ColumnReference("ba"), let bz = ColumnReference("bz")
let aa = ColumnReference("AA"), let az = ColumnReference("Az"),
let ba = ColumnReference("ba"), let bz = ColumnReference("bz")
else {
XCTFail("failed to create simple column references")
return
Expand All @@ -111,10 +111,10 @@ final class CellReferenceTests: XCTestCase {

func testColumnReferenceIntInitializer() {
guard let a = ColumnReference("A"), let z = ColumnReference("z"),
let aa = ColumnReference("AA"), let az = ColumnReference("Az"),
let ba = ColumnReference("ba"), let bz = ColumnReference("bz"),
let abc = ColumnReference("abc"), let azz = ColumnReference("azz"),
let zz = ColumnReference("zz"), let azza = ColumnReference("azza")
let aa = ColumnReference("AA"), let az = ColumnReference("Az"),
let ba = ColumnReference("ba"), let bz = ColumnReference("bz"),
let abc = ColumnReference("abc"), let azz = ColumnReference("azz"),
let zz = ColumnReference("zz"), let azza = ColumnReference("azza")
else {
XCTFail("failed to create simple column references")
return
Expand Down
9 changes: 9 additions & 0 deletions Tests/CoreXLSXTests/Styles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,14 @@ final class StylesTests: XCTestCase {
XCTAssertEqual(styles.differentialFormats!.count, 0)
XCTAssertEqual(styles.tableStyles!.count, 0)
XCTAssertEqual(styles.colors!.indexed.rgbColors.count, 14)

let ws = try file.parseWorksheet(at: file.parseWorksheetPaths()[0])
guard let sd = ws.data else {
XCTFail("no sheet data available")
return
}

XCTAssertEqual(sd.rows.first?.cells.first?.font(in: styles)?.size?.value, 12)
XCTAssertEqual(sd.rows.last?.cells.first?.font(in: styles)?.size?.value, 10)
}
}
3 changes: 3 additions & 0 deletions Tests/CoreXLSXTests/XLSXFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ final class CoreXLSXTests: XCTestCase {

let ws = try file.parseWorksheet(at: "xl/worksheets/sheet1.xml")
XCTAssertEqual(ws.data?.rows.count, 1)

let cell = ws.data?.rows.first?.cells.first
XCTAssertEqual(cell?.font(in: styles)?.size, nil)
}

func testMultiline() throws {
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
steps:
- bash: ./test_xcodebuild.sh Xcode_11.4
env:
IOS_DEVICE: "platform=iOS Simulator,OS=13.4,name=iPhone 8"
IOS_DEVICE: "platform=iOS Simulator,OS=13.4.1,name=iPhone 8"
TVOS_DEVICE: "platform=tvOS Simulator,OS=13.4,name=Apple TV 4K"
- job: test_xcodebuild_11_5
pool:
Expand Down