From eeb5bb9e7ba66d06173b91008e80e2a30995485d Mon Sep 17 00:00:00 2001 From: obimelo Date: Mon, 25 Sep 2023 10:07:13 +0100 Subject: [PATCH 1/5] When shift a row, the row of comments anchors (ct_shape) need to be also shifted. --- ooxml/XSSF/UserModel/XSSFSheet.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ooxml/XSSF/UserModel/XSSFSheet.cs b/ooxml/XSSF/UserModel/XSSFSheet.cs index 1e3322583..8a19fa7b7 100644 --- a/ooxml/XSSF/UserModel/XSSFSheet.cs +++ b/ooxml/XSSF/UserModel/XSSFSheet.cs @@ -5101,6 +5101,8 @@ private void ShiftCommentsAndRows(int startRow, int endRow, int n, bool copyRowH SortedDictionary commentsToShift = new SortedDictionary(new ShiftCommentComparator(n)); + IEnumerable ctShapes = GetVMLDrawing(false).GetItems().OfType(); + foreach (KeyValuePair rowDict in _rows) { XSSFRow row = rowDict.Value; @@ -5123,11 +5125,22 @@ private void ShiftCommentsAndRows(int startRow, int endRow, int n, bool copyRowH .FindCellComment(cellAddress); if (oldComment != null) { + var ctShape = oldComment.GetCTShape(); + + if (ctShape == null) + { + ctShape = ctShapes.FirstOrDefault + (x => + x.ClientData[0].row[0] == cellAddress.Row && + x.ClientData[0].column[0] == cellAddress.Column + ); + } + XSSFComment xssfComment = new XSSFComment( sheetComments, oldComment.GetCTComment(), - oldComment.GetCTShape()); + ctShape); if (commentsToShift.ContainsKey(xssfComment)) { commentsToShift[xssfComment] = newrownum; From 2310902a9af5f5090f7f2c492465c14ac27f697a Mon Sep 17 00:00:00 2001 From: obimelo Date: Wed, 11 Oct 2023 15:05:45 +0100 Subject: [PATCH 2/5] Fix shift rows when there is no comments anchors --- ooxml/XSSF/UserModel/XSSFSheet.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ooxml/XSSF/UserModel/XSSFSheet.cs b/ooxml/XSSF/UserModel/XSSFSheet.cs index 8a19fa7b7..fe6531659 100644 --- a/ooxml/XSSF/UserModel/XSSFSheet.cs +++ b/ooxml/XSSF/UserModel/XSSFSheet.cs @@ -5101,7 +5101,7 @@ private void ShiftCommentsAndRows(int startRow, int endRow, int n, bool copyRowH SortedDictionary commentsToShift = new SortedDictionary(new ShiftCommentComparator(n)); - IEnumerable ctShapes = GetVMLDrawing(false).GetItems().OfType(); + IEnumerable ctShapes = GetVMLDrawing(false)?.GetItems().OfType(); foreach (KeyValuePair rowDict in _rows) { @@ -5127,7 +5127,7 @@ private void ShiftCommentsAndRows(int startRow, int endRow, int n, bool copyRowH { var ctShape = oldComment.GetCTShape(); - if (ctShape == null) + if (ctShape == null && ctShapes != null) { ctShape = ctShapes.FirstOrDefault (x => From 63ba4cf2fcc19d07f4e2576470a83b64ad4266f9 Mon Sep 17 00:00:00 2001 From: obimelo Date: Thu, 9 Nov 2023 11:30:36 +0000 Subject: [PATCH 3/5] Error inserting pictures, after removing some pictures --- ooxml/POIXMLRelation.cs | 2 +- ooxml/XSSF/UserModel/XSSFWorkbook.cs | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ooxml/POIXMLRelation.cs b/ooxml/POIXMLRelation.cs index 863fd5ebc..32f795126 100644 --- a/ooxml/POIXMLRelation.cs +++ b/ooxml/POIXMLRelation.cs @@ -140,7 +140,7 @@ public String GetFileName(int index) public int GetFileNameIndex(POIXMLDocumentPart part) { Regex regex = new Regex(_defaultName.Replace("#", "(\\d+)")); - return int.Parse(regex.Match(part.GetPackagePart().PartName.Name).Value); + return int.Parse(regex.Match(part.GetPackagePart().PartName.Name).Groups[1].Value); //return Integer.valueOf(part.getPackagePart().getPartName().getName().replaceAll(regex, "$1")); } /** diff --git a/ooxml/XSSF/UserModel/XSSFWorkbook.cs b/ooxml/XSSF/UserModel/XSSFWorkbook.cs index 58518cc62..ba4c5c156 100644 --- a/ooxml/XSSF/UserModel/XSSFWorkbook.cs +++ b/ooxml/XSSF/UserModel/XSSFWorkbook.cs @@ -2415,7 +2415,29 @@ public bool CellFormulaValidation public int AddPicture(byte[] pictureData, PictureType format) { - int imageNumber = GetAllPictures().Count + 1; + int imageNumber = 1; + List allPics = (List)GetAllPictures(); + + if (allPics.Any()) + { + var sortedIndexs = + allPics + .Select(pic => XSSFPictureData.RELATIONS[(int)pic.PictureType].GetFileNameIndex(pic)) + .OrderBy(i => i) + .ToList(); + + int previous = sortedIndexs[0]; + for (int index = 1; index < sortedIndexs.Count; index++) + { + if (sortedIndexs[index] > previous + 1) + break; + + previous = sortedIndexs[index]; + } + + imageNumber = previous + 1; + } + XSSFPictureData img = (XSSFPictureData)CreateRelationship(XSSFPictureData.RELATIONS[(int)format], XSSFFactory.GetInstance(), imageNumber, true).DocumentPart; try { From 359263b8c37ff2d4721571f934cd8ade09fe2713 Mon Sep 17 00:00:00 2001 From: obimelo Date: Thu, 9 Nov 2023 15:28:27 +0000 Subject: [PATCH 4/5] Fix AddPicture for first index --- ooxml/XSSF/UserModel/XSSFWorkbook.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ooxml/XSSF/UserModel/XSSFWorkbook.cs b/ooxml/XSSF/UserModel/XSSFWorkbook.cs index ba4c5c156..d2fbb00d9 100644 --- a/ooxml/XSSF/UserModel/XSSFWorkbook.cs +++ b/ooxml/XSSF/UserModel/XSSFWorkbook.cs @@ -2420,11 +2420,15 @@ public int AddPicture(byte[] pictureData, PictureType format) if (allPics.Any()) { - var sortedIndexs = - allPics - .Select(pic => XSSFPictureData.RELATIONS[(int)pic.PictureType].GetFileNameIndex(pic)) - .OrderBy(i => i) - .ToList(); + List sortedIndexs = new List { 0 }; + + sortedIndexs.AddRange + ( + allPics + .Select(pic => XSSFPictureData.RELATIONS[(int)pic.PictureType].GetFileNameIndex(pic)) + .OrderBy(i => i) + .ToList() + ); int previous = sortedIndexs[0]; for (int index = 1; index < sortedIndexs.Count; index++) From 36e2b9986ee420d99c6fba711d4a8c385304e54b Mon Sep 17 00:00:00 2001 From: obimelo Date: Tue, 28 Nov 2023 11:42:00 +0000 Subject: [PATCH 5/5] Fix AddPicture - Returns image index --- ooxml/XSSF/UserModel/XSSFWorkbook.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ooxml/XSSF/UserModel/XSSFWorkbook.cs b/ooxml/XSSF/UserModel/XSSFWorkbook.cs index d2fbb00d9..ee96985fd 100644 --- a/ooxml/XSSF/UserModel/XSSFWorkbook.cs +++ b/ooxml/XSSF/UserModel/XSSFWorkbook.cs @@ -2454,8 +2454,9 @@ public int AddPicture(byte[] pictureData, PictureType format) throw new POIXMLException(e); } pictures.Add(img); - return imageNumber - 1; + // returns image Index + return allPics.Count - 1; } public XSSFWorkbookType WorkbookType