Skip to content

Commit

Permalink
Merge pull request #1192 from obimelo/master
Browse files Browse the repository at this point in the history
When shift a row, the row of comments anchors (ct_shape) need to be also shifted
  • Loading branch information
tonyqus committed Mar 9, 2024
2 parents 188d40c + 36e2b99 commit 85cefb7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ooxml/POIXMLRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
/**
Expand Down
15 changes: 14 additions & 1 deletion ooxml/XSSF/UserModel/XSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5121,6 +5121,8 @@ private void ShiftCommentsAndRows(int startRow, int endRow, int n, bool copyRowH
SortedDictionary<XSSFComment, int> commentsToShift =
new SortedDictionary<XSSFComment, int>(new ShiftCommentComparator(n));

IEnumerable<CT_Shape> ctShapes = GetVMLDrawing(false)?.GetItems().OfType<CT_Shape>();

foreach (KeyValuePair<int, XSSFRow> rowDict in _rows)
{
XSSFRow row = rowDict.Value;
Expand All @@ -5143,11 +5145,22 @@ private void ShiftCommentsAndRows(int startRow, int endRow, int n, bool copyRowH
.FindCellComment(cellAddress);
if (oldComment != null)
{
var ctShape = oldComment.GetCTShape();

if (ctShape == null && ctShapes != 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;
Expand Down
31 changes: 29 additions & 2 deletions ooxml/XSSF/UserModel/XSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2421,7 +2421,33 @@ public bool CellFormulaValidation

public int AddPicture(byte[] pictureData, PictureType format)
{
int imageNumber = GetAllPictures().Count + 1;
int imageNumber = 1;
List<XSSFPictureData> allPics = (List<XSSFPictureData>)GetAllPictures();

if (allPics.Any())
{
List<int> sortedIndexs = new List<int> { 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++)
{
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
{
Expand All @@ -2434,8 +2460,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
Expand Down

0 comments on commit 85cefb7

Please sign in to comment.