diff --git a/Sources/CoreXLSX/Comments.swift b/Sources/CoreXLSX/Comments.swift index 2a1f3d43..8d1036ca 100644 --- a/Sources/CoreXLSX/Comments.swift +++ b/Sources/CoreXLSX/Comments.swift @@ -36,12 +36,25 @@ public struct Comment: Codable, Equatable { case reference = "ref" case text } + + public var plain: String? { + return self.text.r?.t + } } public struct Text: Codable, Equatable { - public let plain: String? + public let r: R? enum CodingKeys: String, CodingKey { - case plain = "t" + case r = "r" } } + +public struct R: Codable, Equatable { + public let t: String? + + enum CodingKeys: String, CodingKey { + case t = "t" + } +} + diff --git a/Sources/CoreXLSX/Relationships.swift b/Sources/CoreXLSX/Relationships.swift index 99efa2fe..6f98ebb2 100644 --- a/Sources/CoreXLSX/Relationships.swift +++ b/Sources/CoreXLSX/Relationships.swift @@ -130,6 +130,14 @@ public struct Relationship: Codable, Equatable { """ http://purl.oclc.org/ooxml/officeDocument/relationships/extendedProperties """ + case sheetComment = + """ + http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments + """ + case sheetVmlDrawing = + """ + http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing + """ } /// The identifier for this entity. diff --git a/Sources/CoreXLSX/XLSXFile.swift b/Sources/CoreXLSX/XLSXFile.swift index b594a2b7..fdb8f4a7 100644 --- a/Sources/CoreXLSX/XLSXFile.swift +++ b/Sources/CoreXLSX/XLSXFile.swift @@ -157,7 +157,13 @@ public class XLSXFile { let worksheetIdRange = Range(match.range(at: 1), in: path) { let worksheetId = path[worksheetIdRange] - return "xl/comments\(worksheetId).xml" + decoder.keyDecodingStrategy = .convertFromCapitalized + if let relationships = try? parseEntry("xl/worksheets/_rels/sheet\(worksheetId).xml.rels", Relationships.self) { + if let commentRelationship = relationships.items.filter({ $0.type == .sheetComment }).first { + return "xl/\(commentRelationship.target.replacingOccurrences(of: "../", with: ""))" + } + } + return ""//"xl/comments\(worksheetId).xml" } throw CoreXLSXError.unsupportedWorksheetPath @@ -165,7 +171,9 @@ public class XLSXFile { public func parseComments(forWorksheet path: String) throws -> Comments { let commentsPath = try buildCommentsPath(forWorksheet: path) - + if commentsPath.isEmpty { + return Comments(commentList: CommentList(items: [])) + } decoder.keyDecodingStrategy = .useDefaultKeys return try parseEntry(commentsPath, Comments.self) diff --git a/Tests/CoreXLSXTests/Comments.swift b/Tests/CoreXLSXTests/Comments.swift index 2e61d139..c198c57a 100644 --- a/Tests/CoreXLSXTests/Comments.swift +++ b/Tests/CoreXLSXTests/Comments.swift @@ -25,7 +25,7 @@ final class CommentsTests: XCTestCase { } let paths = try file.parseWorksheetPaths() let comments = try file.parseComments(forWorksheet: paths[0]) - XCTAssertEqual(comments.commentList.itemsByReference["A1"]?.text.plain, + XCTAssertEqual(comments.commentList.itemsByReference["A1"]?.plain, "my note") } }