Skip to content
Open
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
17 changes: 15 additions & 2 deletions Sources/CoreXLSX/Comments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@ public struct Comment: Codable, Equatable {
case reference = "ref"
case text
}

public var plain: String? {
return self.text.r?.t
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the comment on plain:

Suggested change
return self.text.r?.t
return self.text.r?.t ?? self.text.plain

}
}

public struct Text: Codable, Equatable {
public let plain: String?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, breaking API is not something we can merge. Could you re-introduce plain here?

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"
}
}

8 changes: 8 additions & 0 deletions Sources/CoreXLSX/Relationships.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions Sources/CoreXLSX/XLSXFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,23 @@ 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
}

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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreXLSXTests/Comments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}