Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions src/EPPlus/Drawing/ExcelDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Date Author Change
using OfficeOpenXml.Drawing.OleObject;
using OfficeOpenXml.Drawing.Slicer;
using OfficeOpenXml.FormulaParsing.Excel.Functions.MathFunctions;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using OfficeOpenXml.Packaging;
using OfficeOpenXml.Utils.EnumUtils;
using OfficeOpenXml.Utils.FileUtils;
Expand Down Expand Up @@ -731,8 +732,8 @@ internal void GetFromBounds(out int fromRow, out int fromRowOff, out int fromCol
{
if (CellAnchor == eEditAs.Absolute)
{
GetToRowFromPixels(Position.Y, out fromRow, out fromRowOff);
GetToColumnFromPixels(Position.X, out fromCol, out fromColOff);
GetToRowFromPixels(Position.Y / (double)EMU_PER_PIXEL, out fromRow, out fromRowOff);
GetToColumnFromPixels(Position.X / (double)EMU_PER_PIXEL, out fromCol, out fromColOff);
}
else
{
Expand All @@ -747,7 +748,7 @@ internal void GetToBounds(out int toRow, out int toRowOff, out int toCol, out in
if (CellAnchor == eEditAs.Absolute)
{
GetToRowFromPixels((Position.Y + Size.Height) / EMU_PER_PIXEL, out toRow, out toRowOff);
GetToColumnFromPixels(Position.X + Size.Width / EMU_PER_PIXEL, out toCol, out toColOff);
GetToColumnFromPixels((Position.X + Size.Width) / EMU_PER_PIXEL, out toCol, out toColOff);
}
else
{
Expand Down Expand Up @@ -1033,6 +1034,26 @@ internal void SetPixelHeight(double pixels)

internal void GetToRowFromPixels(double pixels, out int toRow, out int rowOff, int fromRow = -1, int fromRowOff = -1)
{
if (From == null && this is not ExcelControl)
{
// Absolute anchor path
double remaining = pixels;
int currentRow = 1;

while (true)
{
double rowPix = _drawings.Worksheet.GetRowHeight(currentRow) / 0.75;
if (remaining < rowPix)
break;

remaining -= rowPix;
currentRow++;
}

toRow = currentRow - 1;
rowOff = (int)(remaining);
return;
}
if (fromRow < 0)
{
fromRow = From.Row;
Expand Down Expand Up @@ -1087,7 +1108,27 @@ internal void GetToColumnFromPixels(double pixels, out int col, out int colOff,
{
ExcelWorksheet ws = _drawings.Worksheet;
double mdw = ws.Workbook.MaxFontWidth;
if (fromColumn < 0)
if (From == null && this is not ExcelControl)
{
// Absolute anchor path
double remaining = pixels;
int currentCol = 1;

while (true)
{
double colPix = (ws.GetColumnWidth(fromColumn) * mdw + 0.75d);
if (remaining < colPix)
break;

remaining -= colPix;
currentCol++;
}

col = currentCol-1;
colOff = (int)(remaining);
return;
}
if (From != null && fromColumn < 0)
{
fromColumn = From.Column;
fromColumnOff = From.ColumnOff;
Expand Down
13 changes: 13 additions & 0 deletions src/EPPlusTest/Drawing/CopyDrawingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -799,5 +799,18 @@ public void s814CopySameImageTwiceToEmptyNamedRanges()
SaveAndCleanup(targetPackage);
}
}


[TestMethod]
public void CopyDrawingWithAbsolutePosition ()
{
using var p = OpenTemplatePackage("Test-file.xlsx");
var sourceSheet = p.Workbook.Worksheets[1];
using var destPackage = new ExcelPackage();
var destSheet = destPackage.Workbook.Worksheets.Add("Dest");
sourceSheet.Cells[1, 1, sourceSheet.Dimension.Rows, sourceSheet.Dimension.Columns].Copy(destSheet.Cells[1, 1], ExcelRangeCopyOptionFlags.ExcludeFormulas);
//Assert.AreEqual(1, destSheet.Drawings.Count);
destPackage.SaveAs($"C:\\epplusTest\\Testoutput\\AbsoluteDrawingCopy.xlsx");
}
}
}
Loading