Repository: MaxDesiatov/CoreXLSX (swift)

Issue:
API for matching sheet names to sheet paths
> Hi @duodo2412, the value of the cell B14 in this case is not `""`, it's essentially `nil`, because the cell itself is absent in the `.xlsx` file in this case. If you'd like to verify that, you can specify the exact cell:
> 
> ```swift
> let cell = worksheet.cells(atColumns: [ColumnReference("B")!], rows: [4])
> ```
> 
> My undertstanding is that this will return an empty array `[]` in your case. Does that resolve your issue?

Tks for the your answer .
Can i ask you another question. 
![Screen Shot 2020-04-11 at 14 32 09](https://user-images.githubusercontent.com/44747351/79038150-443ecf80-7c01-11ea-8e92-86deb3706354.png)
I have a lot of sheets in xlsx file.
My code: 
let sheets: [String] = try file.parseWorksheetPaths() and print result
- After that i change the order of sheet in file and execute my code again. Result is the same before i change the order of sheet .
How can i know exactly the sheet i want to get ( may be i can query with name of sheet ) . Can you help me?
Tks

_Originally posted by @duodo2412 in https://github.com/MaxDesiatov/CoreXLSX/issues/21#issuecomment-612357393_

Pull request description:
Resolve #105.

Patch:
diff --git a/Sources/CoreXLSX/XLSXFile.swift b/Sources/CoreXLSX/XLSXFile.swift
index 2c63bd01..3a4e08fe 100644
--- a/Sources/CoreXLSX/XLSXFile.swift
+++ b/Sources/CoreXLSX/XLSXFile.swift
@@ -195,6 +195,29 @@ public class XLSXFile {
     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)] {
+    return 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: "\(pathPrefix)/\($0.target)") }
+    }
+  }
+
   /// Parse and return an array of worksheets in this XLSX file.
   public func parseWorksheetPaths() throws -> [String] {
     return try parseDocumentPaths().map {


Test patch:
diff --git a/Tests/CoreXLSXTests/Workbook.swift b/Tests/CoreXLSXTests/Workbook.swift
index e3ad0718..611e6828 100644
--- a/Tests/CoreXLSXTests/Workbook.swift
+++ b/Tests/CoreXLSXTests/Workbook.swift
@@ -49,6 +49,18 @@ final class WorkbookTests: XCTestCase {
     XCTAssertEqual(wbs[0].sheets.items, parsedSheet)
   }
 
+  func testWorksheetPathsAndNames() throws {
+    guard let file =
+      XLSXFile(filepath: "\(currentWorkingPath)/categories.xlsx") else {
+      XCTAssert(false, "failed to open the file")
+      return
+    }
+
+    let wbs = try file.parseWorkbooks()
+    let sheets = try file.parseWorksheetPathsAndNames(workbook: wbs[0])
+    XCTAssertEqual(sheets.map { $0.name }, ["Sheet 1"])
+  }
+
   func testWorkbookNoViews() throws {
     let decoder = XMLDecoder()
     decoder.shouldProcessNamespaces = true
diff --git a/Tests/CoreXLSXTests/XCTestManifests.swift b/Tests/CoreXLSXTests/XCTestManifests.swift
index 635cc5a1..3c1d56f8 100644
--- a/Tests/CoreXLSXTests/XCTestManifests.swift
+++ b/Tests/CoreXLSXTests/XCTestManifests.swift
@@ -40,6 +40,7 @@ extension CoreXLSXTests {
   // to regenerate.
   static let __allTests__CoreXLSXTests = [
     ("testBlank", testBlank),
+    ("testDates", testDates),
     ("testHelloWorld", testHelloWorld),
     ("testLegacyPublicAPI", testLegacyPublicAPI),
     ("testMultiline", testMultiline),
@@ -103,6 +104,7 @@ extension WorkbookTests {
   static let __allTests__WorkbookTests = [
     ("testWorkbook", testWorkbook),
     ("testWorkbookNoViews", testWorkbookNoViews),
+    ("testWorksheetPathsAndNames", testWorksheetPathsAndNames),
   ]
 }