@@ -330,15 +330,37 @@ protected function adjustMergeCells(Worksheet $worksheet): void
330330 protected function adjustProtectedCells (Worksheet $ worksheet , int $ numberOfColumns , int $ numberOfRows ): void
331331 {
332332 $ aProtectedCells = $ worksheet ->getProtectedCellRanges ();
333- ($ numberOfColumns > 0 || $ numberOfRows > 0 )
334- ? uksort ($ aProtectedCells , [self ::class, 'cellReverseSort ' ])
335- : uksort ($ aProtectedCells , [self ::class, 'cellSort ' ]);
336- foreach ($ aProtectedCells as $ cellAddress => $ protectedRange ) {
337- $ newReference = $ this ->updateCellReference ($ cellAddress );
338- if ($ cellAddress !== $ newReference ) {
339- $ worksheet ->unprotectCells ($ cellAddress );
340- if ($ newReference ) {
341- $ worksheet ->protectCells ($ newReference , $ protectedRange ->getPassword (), true );
333+ /** @var CellReferenceHelper */
334+ $ cellReferenceHelper = $ this ->cellReferenceHelper ;
335+ if ($ numberOfRows >= 0 && $ numberOfColumns >= 0 ) {
336+ foreach ($ aProtectedCells as $ key2 => $ value ) {
337+ $ ranges = $ value ->allRanges ();
338+ $ newKey = $ separator = '' ;
339+ foreach ($ ranges as $ key => $ range ) {
340+ $ oldKey = $ range [0 ] . (array_key_exists (1 , $ range ) ? (': ' . $ range [1 ]) : '' );
341+ $ newKey .= $ separator . $ this ->updateCellReference ($ oldKey );
342+ $ separator = ' ' ;
343+ }
344+ if ($ key2 !== $ newKey ) {
345+ $ worksheet ->unprotectCells ($ key2 );
346+ $ worksheet ->protectCells ($ newKey , $ value ->getPassword (), true , $ value ->getName (), $ value ->getSecurityDescriptor ());
347+ }
348+ }
349+ } else {
350+ foreach ($ aProtectedCells as $ key2 => $ value ) {
351+ $ range = str_replace ([' ' , ', ' , "\0" ], ["\0" , ' ' , ', ' ], $ key2 );
352+ $ extracted = Coordinate::extractAllCellReferencesInRange ($ range );
353+ $ outArray = [];
354+ foreach ($ extracted as $ cellAddress ) {
355+ if (!$ cellReferenceHelper ->cellAddressInDeleteRange ($ cellAddress )) {
356+ $ outArray [$ this ->updateCellReference ($ cellAddress )] = 'x ' ;
357+ }
358+ }
359+ $ outArray2 = Coordinate::mergeRangesInCollection ($ outArray );
360+ $ newKey = implode (' ' , array_keys ($ outArray2 ));
361+ if ($ key2 !== $ newKey ) {
362+ $ worksheet ->unprotectCells ($ key2 );
363+ $ worksheet ->protectCells ($ newKey , $ value ->getPassword (), true , $ value ->getName (), $ value ->getSecurityDescriptor ());
342364 }
343365 }
344366 }
0 commit comments