Skip to content

Commit

Permalink
Fix parsing root relationships worksheet paths (#123)
Browse files Browse the repository at this point in the history
Resolve #121
  • Loading branch information
MaxDesiatov authored Jul 9, 2020
1 parent 1f5b7bf commit 7594263
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sources/CoreXLSX/Relationships.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ public struct Relationship: Codable, Equatable {
public let id: String
public let type: SchemaType
public let target: String

func path(from root: String) -> String {
return Path(target).isRoot ? target : "\(root)/\(target)"
}
}
4 changes: 2 additions & 2 deletions Sources/CoreXLSX/XLSXFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public class XLSXFile {
sheet.name.flatMap { (sheet.relationship, $0) }
})

return worksheets.map { (name: sheetIDs[$0.id], path: "\(pathPrefix)/\($0.target)") }
return worksheets.map { (name: sheetIDs[$0.id], path: $0.path(from: pathPrefix)) }
}
}

Expand All @@ -265,7 +265,7 @@ public class XLSXFile {
// storing that path in `pathPrefix`
let pathPrefix = path.components.dropLast().joined(separator: "/")

return worksheets.map { "\(pathPrefix)/\($0.target)" }
return worksheets.map { $0.path(from: pathPrefix) }
}
}

Expand Down
11 changes: 11 additions & 0 deletions Tests/CoreXLSXTests/XLSXFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,15 @@ final class CoreXLSXTests: XCTestCase {
try file.cellsInWorksheet(at: sheetPath, columns: columnReferences)
XCTAssertEqual(allCells, cellsFromAllColumns)
}

func testRootRelationships() throws {
guard let file =
XLSXFile(filepath: "\(currentWorkingPath)/root_relationships.xlsx") else {
XCTFail("failed to open the file")
return
}

XCTAssertEqual(try file.parseDocumentPaths(), ["xl/workbook.xml"])
XCTAssertEqual(try file.parseWorksheetPaths(), ["/\(sheetPath)"])
}
}
Binary file added Tests/CoreXLSXTests/root_relationships.xlsx
Binary file not shown.

0 comments on commit 7594263

Please sign in to comment.