@@ -1484,13 +1484,14 @@ func (f *File) addSheetDrawingChart(drawingXML string, rID int, opts *GraphicOpt
1484
1484
// deleteDrawing provides a function to delete the chart graphic frame and
1485
1485
// returns deleted embed relationships ID (for unique picture cell anchor) by
1486
1486
// given coordinates and graphic type.
1487
- func (f * File ) deleteDrawing (col , row int , drawingXML , drawingType string ) (string , error ) {
1487
+ func (f * File ) deleteDrawing (col , row int , drawingXML , drawingType string ) ([] string , error ) {
1488
1488
var (
1489
- err error
1490
- rID string
1491
- rIDs []string
1492
- wsDr * xlsxWsDr
1493
- deTwoCellAnchor * decodeCellAnchor
1489
+ err error
1490
+ rID string
1491
+ delRID , refRID []string
1492
+ rIDMaps = map [string ]int {}
1493
+ wsDr * xlsxWsDr
1494
+ deCellAnchor * decodeCellAnchor
1494
1495
)
1495
1496
xdrCellAnchorFuncs := map [string ]func (anchor * xdrCellAnchor ) bool {
1496
1497
"Chart" : func (anchor * xdrCellAnchor ) bool { return anchor .Pic == nil },
@@ -1502,54 +1503,70 @@ func (f *File) deleteDrawing(col, row int, drawingXML, drawingType string) (stri
1502
1503
}
1503
1504
onAnchorCell := func (c , r int ) bool { return c == col && r == row }
1504
1505
if wsDr , _ , err = f .drawingParser (drawingXML ); err != nil {
1505
- return rID , err
1506
- }
1507
- for idx := 0 ; idx < len (wsDr .TwoCellAnchor ); idx ++ {
1508
- if err = nil ; wsDr .TwoCellAnchor [idx ].From != nil && xdrCellAnchorFuncs [drawingType ](wsDr .TwoCellAnchor [idx ]) {
1509
- if onAnchorCell (wsDr .TwoCellAnchor [idx ].From .Col , wsDr .TwoCellAnchor [idx ].From .Row ) {
1510
- rID , _ = extractEmbedRID (wsDr .TwoCellAnchor [idx ].Pic , nil , rIDs )
1511
- wsDr .TwoCellAnchor = append (wsDr .TwoCellAnchor [:idx ], wsDr .TwoCellAnchor [idx + 1 :]... )
1512
- idx --
1506
+ return delRID , err
1507
+ }
1508
+ deleteCellAnchor := func (ca []* xdrCellAnchor ) ([]* xdrCellAnchor , error ) {
1509
+ for idx := 0 ; idx < len (ca ); idx ++ {
1510
+ if err = nil ; ca [idx ].From != nil && xdrCellAnchorFuncs [drawingType ](ca [idx ]) {
1511
+ rID = extractEmbedRID (ca [idx ].Pic , nil )
1512
+ rIDMaps [rID ]++
1513
+ if onAnchorCell (ca [idx ].From .Col , ca [idx ].From .Row ) {
1514
+ refRID = append (refRID , rID )
1515
+ ca = append (ca [:idx ], ca [idx + 1 :]... )
1516
+ idx --
1517
+ rIDMaps [rID ]--
1518
+ }
1513
1519
continue
1514
1520
}
1515
- _ , rIDs = extractEmbedRID (wsDr .TwoCellAnchor [idx ].Pic , nil , rIDs )
1516
- }
1517
- }
1518
- for idx := 0 ; idx < len (wsDr .TwoCellAnchor ); idx ++ {
1519
- deTwoCellAnchor = new (decodeCellAnchor )
1520
- if err = f .xmlNewDecoder (strings .NewReader ("<decodeCellAnchor>" + wsDr .TwoCellAnchor [idx ].GraphicFrame + "</decodeCellAnchor>" )).
1521
- Decode (deTwoCellAnchor ); err != nil && err != io .EOF {
1522
- return rID , err
1523
- }
1524
- if err = nil ; deTwoCellAnchor .From != nil && decodeCellAnchorFuncs [drawingType ](deTwoCellAnchor ) {
1525
- if onAnchorCell (deTwoCellAnchor .From .Col , deTwoCellAnchor .From .Row ) {
1526
- rID , _ = extractEmbedRID (nil , deTwoCellAnchor .Pic , rIDs )
1527
- wsDr .TwoCellAnchor = append (wsDr .TwoCellAnchor [:idx ], wsDr .TwoCellAnchor [idx + 1 :]... )
1528
- idx --
1529
- continue
1521
+ deCellAnchor = new (decodeCellAnchor )
1522
+ if err = f .xmlNewDecoder (strings .NewReader ("<decodeCellAnchor>" + ca [idx ].GraphicFrame + "</decodeCellAnchor>" )).
1523
+ Decode (deCellAnchor ); err != nil && err != io .EOF {
1524
+ return ca , err
1525
+ }
1526
+ if err = nil ; deCellAnchor .From != nil && decodeCellAnchorFuncs [drawingType ](deCellAnchor ) {
1527
+ rID = extractEmbedRID (nil , deCellAnchor .Pic )
1528
+ rIDMaps [rID ]++
1529
+ if onAnchorCell (deCellAnchor .From .Col , deCellAnchor .From .Row ) {
1530
+ refRID = append (refRID , rID )
1531
+ ca = append (ca [:idx ], ca [idx + 1 :]... )
1532
+ idx --
1533
+ rIDMaps [rID ]--
1534
+ }
1530
1535
}
1531
- _ , rIDs = extractEmbedRID (nil , deTwoCellAnchor .Pic , rIDs )
1532
1536
}
1537
+ return ca , err
1533
1538
}
1534
- if inStrSlice (rIDs , rID , true ) != - 1 {
1535
- rID = ""
1539
+ if wsDr .OneCellAnchor , err = deleteCellAnchor (wsDr .OneCellAnchor ); err != nil {
1540
+ return delRID , err
1541
+ }
1542
+ if wsDr .TwoCellAnchor , err = deleteCellAnchor (wsDr .TwoCellAnchor ); err != nil {
1543
+ return delRID , err
1536
1544
}
1537
1545
f .Drawings .Store (drawingXML , wsDr )
1538
- return rID , err
1546
+ return getUnusedCellAnchorRID ( delRID , refRID , rIDMaps ) , err
1539
1547
}
1540
1548
1541
- // extractEmbedRID returns embed relationship ID and all relationship ID lists
1542
- // for giving cell anchor.
1543
- func extractEmbedRID ( pic * xlsxPic , decodePic * decodePic , rIDs [] string ) ( string , [] string ) {
1549
+ // extractEmbedRID returns embed relationship ID by giving cell anchor.
1550
+ func extractEmbedRID ( pic * xlsxPic , decodePic * decodePic ) string {
1551
+ var rID string
1544
1552
if pic != nil {
1545
- rIDs = append (rIDs , pic .BlipFill .Blip .Embed )
1546
- return pic .BlipFill .Blip .Embed , rIDs
1553
+ rID = pic .BlipFill .Blip .Embed
1547
1554
}
1548
1555
if decodePic != nil {
1549
- rIDs = append (rIDs , decodePic .BlipFill .Blip .Embed )
1550
- return decodePic .BlipFill .Blip .Embed , rIDs
1556
+ rID = decodePic .BlipFill .Blip .Embed
1557
+ }
1558
+ return rID
1559
+ }
1560
+
1561
+ // getUnusedCellAnchorRID returns relationship ID lists in the cell anchor which
1562
+ // for remove.
1563
+ func getUnusedCellAnchorRID (delRID , refRID []string , rIDMaps map [string ]int ) []string {
1564
+ for _ , rID := range refRID {
1565
+ if rIDMaps [rID ] == 0 && inStrSlice (delRID , rID , false ) == - 1 {
1566
+ delRID = append (delRID , rID )
1567
+ }
1551
1568
}
1552
- return "" , rIDs
1569
+ return delRID
1553
1570
}
1554
1571
1555
1572
// deleteDrawingRels provides a function to delete relationships in
0 commit comments