pjfanning commented on code in PR #1602:
URL: https://github.com/apache/poi/pull/1602#discussion_r4726480251


##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java:
##########
@@ -701,6 +701,12 @@ public XSSFSheet cloneSheet(int sheetNum, String newName) {
 
         XSSFSheet clonedSheet = createSheet(newName);
 
+        // a hyperlink added since the sheet was read gets its relationship 
only when the sheet is
+        // written, which happens below after the relationships are copied, so 
add them now
+        for (XSSFHyperlink hyperlink : srcSheet.getHyperlinkList()) {

Review Comment:
   Two small things:
   
   1. Could this loop go above `createSheet(newName)`? It only touches the 
source sheet, and `generateRelationIfNeeded` can throw (e.g. a URL hyperlink 
with no address or an address that is not a valid URI). Here that would leave 
the empty new sheet in the workbook. That could already happen with the later 
`srcSheet.write`, but it is easy to avoid for this part.
   2. Not new, and maybe not worth handling: `generateRelationIfNeeded` returns 
early when the relationship belongs to another sheet, so one `XSSFHyperlink` 
object that has been added to two sheets could still end up with an `r:id` that 
does not resolve on the source sheet's part.



##########
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java:
##########
@@ -766,6 +766,36 @@ void testSetAddressOfRemovedHyperlink() throws 
IOException, InvalidFormatExcepti
         }
     }
 
+    @Test
+    void testCloneSheetWithNewHyperlink() throws IOException, 
InvalidFormatException {

Review Comment:
   Nit: this covers a brand new hyperlink. If it is easy, cases for a hyperlink 
that was read from a file and moved to the source sheet, and for one that was 
removed and added again, would cover the other branches of 
`generateRelationIfNeeded` that now run before the relationships are copied.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to