Bug 66612

Summary: Macrosheet not found in xslm sheets
Product: POI Reporter: 1umos
Component: XSSFAssignee: POI Developers List <dev>
Status: NEEDINFO ---    
Severity: major    
Priority: P2    
Version: 5.2.3-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: xlsm file containing a empty macrosheet that reproduces the problem

Description 1umos 2023-05-22 15:01:04 UTC
Created attachment 38565 [details]
xlsm file containing a empty macrosheet that reproduces the problem

In XSSFWorkbook, the macro sheet (http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet) is not added to the sheets-shIdMap. Example document contains Macro1, Sheet1, Sheet2.

workbook.getNumberOfSheets() returns 2 (Sheet1, Sheet2 not Macro1)

I can't find relationships with :
workbook.getPackage.getRelationshipsByType("application/vnd.ms-excel.macrosheet+xml")

or with this:

wbpart = workbook.getPackagePart()
wbrelcollection = wbpart.getRelationshipsByType("http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet")

I need to get xlm macro sheet index and remove it from the document completely.
Comment 1 PJ Fanning 2023-05-22 15:12:56 UTC
Have you tried getRelationshipsByType("http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet") - from a quick check of the POI code, getRelationshipsByType expects strings like this - not 'content types'
Comment 2 PJ Fanning 2023-05-22 15:28:08 UTC
1umos - is this you? https://stackoverflow.com/questions/76306947/how-to-get-xlm-macro-sheet-from-excel-with-apache-poi-xssfworkbook

Seems similar but seems to suggest that you can get relationships of this type - while maybe running into issues removing them.
Comment 3 PJ Fanning 2023-05-22 16:02:41 UTC
I tested your xlsm file and this code seems to get the macrosheet part

            String relType = "http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet";
            PackageRelationshipCollection prc = workbook.getPackagePart().getRelationships();
            assertNotNull(prc);
            assertEquals(6, prc.size());
            PackageRelationshipCollection prc2 = prc.getRelationships(relType);
            assertNotNull(prc2);
            assertEquals(1, prc2.size());

workbook.getPackage() checks the wrong relationships - you need workbook.getPackagePart().getRelationships()
Comment 4 1umos 2023-05-23 07:46:16 UTC
(In reply to PJ Fanning from comment #3)
> I tested your xlsm file and this code seems to get the macrosheet part
> 
>             String relType =
> "http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet";
>             PackageRelationshipCollection prc =
> workbook.getPackagePart().getRelationships();
>             assertNotNull(prc);
>             assertEquals(6, prc.size());
>             PackageRelationshipCollection prc2 =
> prc.getRelationships(relType);
>             assertNotNull(prc2);
>             assertEquals(1, prc2.size());
> 
> workbook.getPackage() checks the wrong relationships - you need
> workbook.getPackagePart().getRelationships()

Thanks, I get macrosheets part like this, but if I try to removeRelationship then document doesn't open. Do you have a solution for that? And yes stackoverflow question is mine.