pjfanning opened a new pull request, #1555:
URL: https://github.com/apache/poi/pull/1555

   Part of #1554 (item 1), follow-up to #1543.
   
   The shared strings table only grows. After `removeSheetAt`, or after 
changing a cell's text, the old strings stay in `xl/sharedStrings.xml` and so 
in the written file. `XSSFWorkbook.removeUnusedSharedStrings()` scans every 
cell, removes the entries no cell uses, renumbers the remaining string cells, 
and returns the number removed. It is an explicit call, made once before 
writing, rather than part of every removal, because it scans the whole 
workbook. The `removeSheetAt` javadoc now points to it.
   
   Kept entries stay the same XmlBeans objects, so formatting runs and phonetic 
data are unchanged, and the lookup maps are rebuilt so later strings still 
reuse existing entries. Unused entries are deleted through their own 
`XmlCursor`. Deleting with `removeSi(i)` was quadratic: 92 s to remove 100,000 
entries, against about 0.7 s now, including the cell scans. Cells whose value 
isn't a valid index are left alone, since `XSSFCell` already reads them as 
empty strings. A cell pointing past the end of the table throws 
`IllegalStateException`.
   
   The method refuses, with `IllegalStateException`, when string indexes are 
used outside the cells it can rewrite:
   - **SXSSF with a shared strings table**: streamed rows in the temporary 
files hold indexes. `SXSSFWorkbook` marks the table through a new `@Internal` 
`SharedStringsTable.setFixedIndexes()`. Without a shared strings table, SXSSF 
writes inline strings, so the method works.
   - **Shared workbook revision logs**: their cells can refer to shared 
strings, and POI doesn't parse them.
   
   New `@Internal` API on `SharedStringsTable`: `setFixedIndexes()` and 
`removeUnusedEntries(SparseBitSet, int)`.
   
   Tests in `TestXSSFWorkbook`:
   - removing a sheet and a changed cell's old text, keeping a rich text 
string, reusing an entry afterwards, and checking the written file
   - cell values of `sample.xlsx`, `WithVariousData.xlsx` and `Formatting.xlsx` 
unchanged after removing their first sheet, before and after a round trip
   - the refusals for SXSSF and for 
`workbookProtection_workbook_revision_protected.xlsx`
   
   `TestXSSFWorkbook`, `TestSharedStringsTable` and `TestSXSSFWorkbook` pass on 
JDK 17.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


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