Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removing hyperlinks #1128

Merged
merged 4 commits into from
Aug 12, 2023
Merged
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
78 changes: 39 additions & 39 deletions ooxml/XSSF/UserModel/XSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,28 +1436,25 @@ internal virtual void Write(Stream stream, bool leaveOpen = false)
}

// Now re-generate our CT_Hyperlinks, if needed
if (hyperlinks.Count > 0)
if (worksheet.hyperlinks == null)
{
if (worksheet.hyperlinks == null)
{
worksheet.AddNewHyperlinks();
}

CT_Hyperlink[] ctHls
= new CT_Hyperlink[hyperlinks.Count];
for (int i = 0; i < ctHls.Length; i++)
{
// If our sheet has hyperlinks, have them add
// any relationships that they might need
XSSFHyperlink hyperlink = hyperlinks[i];
hyperlink.GenerateRelationIfNeeded(GetPackagePart());
// Now grab their underling object
ctHls[i] = hyperlink.GetCTHyperlink();
}
worksheet.AddNewHyperlinks();
}

worksheet.hyperlinks.SetHyperlinkArray(ctHls);
CT_Hyperlink[] ctHls
= new CT_Hyperlink[hyperlinks.Count];
for (int i = 0; i < ctHls.Length; i++)
{
// If our sheet has hyperlinks, have them add
// any relationships that they might need
XSSFHyperlink hyperlink = hyperlinks[i];
hyperlink.GenerateRelationIfNeeded(GetPackagePart());
// Now grab their underling object
ctHls[i] = hyperlink.GetCTHyperlink();
}

worksheet.hyperlinks.SetHyperlinkArray(ctHls);

foreach (XSSFRow row in _rows.Values)
{
row.OnDocumentWrite();
Expand Down Expand Up @@ -5961,15 +5958,15 @@ public double GetDefaultColWidthInPixel()
double width_px;

var width = worksheet.sheetFormatPr.defaultColWidth; //string length with padding
if(width != 0.0 )
if (width != 0.0)
{
width_px = width * MaximumDigitWidth;
}
else
{
double MDW = MaximumDigitWidth;
var length = worksheet.sheetFormatPr.baseColWidth; //string length with out padding
fontwidth = Math.Truncate(( length * MDW + 5 ) / MDW * 256) / 256;
fontwidth = Math.Truncate((length * MDW + 5) / MDW * 256) / 256;
double tmp = 256 * fontwidth + Math.Truncate(128 / MDW);
width_px = Math.Truncate((tmp / 256) * MDW) + 3; // +3 ???
}
Expand All @@ -5981,16 +5978,17 @@ int dx1
, int dy1
, int dx2
, int dy2
) {
int left = Math.Min(dx1, dx2);
int right = Math.Max(dx1, dx2);
int top = Math.Min(dy1, dy2);
int bottom = Math.Max(dy1, dy2);
)
{
int left = Math.Min(dx1, dx2);
int right = Math.Max(dx1, dx2);
int top = Math.Min(dy1, dy2);
int bottom = Math.Max(dy1, dy2);

CT_Marker mk1 = EMUtoMarker(left, top);
CT_Marker mk2 = EMUtoMarker(right, bottom);

if(mk1.colOff >= 0 && mk1.rowOff >= 0 && mk2.colOff >= 0 && mk2.rowOff >= 0)
if (mk1.colOff >= 0 && mk1.rowOff >= 0 && mk2.colOff >= 0 && mk2.rowOff >= 0)
{
return new XSSFClientAnchor(mk1, mk2, left, top, right, bottom);
}
Expand All @@ -6000,24 +5998,25 @@ int dx1
private CT_Marker EMUtoMarker(int x, int y)
{
CT_Marker mkr = new CT_Marker();
mkr.colOff = EMUtoColOff(x, out int col);
mkr.colOff = EMUtoColOff(x, out int col);
mkr.col = col;
mkr.rowOff = EMUtoRowOff(y, out int row);
mkr.rowOff = EMUtoRowOff(y, out int row);
mkr.row = row;
return mkr;
}

public int EMUtoRowOff(
int y
, out int cell
) {
)
{
double height;
cell = 0;
for(int iRow = 0; iRow < SpreadsheetVersion.EXCEL2007.MaxRows; iRow++)
for (int iRow = 0; iRow < SpreadsheetVersion.EXCEL2007.MaxRows; iRow++)
{
height = _rows.ContainsKey(iRow) ? _rows[iRow].HeightInPoints
: DefaultRowHeightInPoints;
if(y >= Units.ToEMU(height))
if (y >= Units.ToEMU(height))
{
y -= Units.ToEMU(height);
cell++;
Expand All @@ -6034,27 +6033,28 @@ int y
public int EMUtoColOff(
int x
, out int cell
) {
)
{

double width_px;
cell = 0;
for(int iCol = 0; iCol < SpreadsheetVersion.EXCEL2007.MaxColumns; iCol++)
for (int iCol = 0; iCol < SpreadsheetVersion.EXCEL2007.MaxColumns; iCol++)
{
width_px = GetDefaultColWidthInPixel();
foreach(var cols in worksheet.cols)
foreach (var cols in worksheet.cols)
{
foreach(var col in cols.col)
foreach (var col in cols.col)
{
if(col.min <= iCol + 1 && iCol + 1 <= col.max)
if (col.min <= iCol + 1 && iCol + 1 <= col.max)
{
width_px = col.width * MaximumDigitWidth;
goto lblforbreak;
}
}
}
lblforbreak:
int EMUwidth = Units.PixelToEMU((int)Math.Round(width_px,1));
if(x >= EMUwidth)
lblforbreak:
int EMUwidth = Units.PixelToEMU((int)Math.Round(width_px, 1));
if (x >= EMUwidth)
{
x -= EMUwidth;
cell++;
Expand Down
41 changes: 41 additions & 0 deletions testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3383,6 +3383,47 @@ public void Bug61063()
Assert.AreEqual(2.0, cv.NumberValue, 0.00001);
wb.Close();
}

[Test]
public void TestBug690()
{
using (var workbook = new XSSFWorkbook())
{
XSSFSheet sheet = workbook.CreateSheet() as XSSFSheet;
XSSFCreationHelper creationHelper = workbook.GetCreationHelper() as XSSFCreationHelper;
XSSFHyperlink hyperlink;

hyperlink = creationHelper.CreateHyperlink(HyperlinkType.Url) as XSSFHyperlink;
sheet.AddHyperlink(hyperlink);

string address = "http://myurl";
hyperlink.Address = address;
hyperlink.SetCellReference("A1");

var cellAddress = new CellAddress("A1");

var comment = sheet.GetHyperlink(cellAddress);
Assert.IsNotNull(comment);
Assert.IsTrue(comment.Address.Equals(address));

using (var wbCopy = XSSFTestDataSamples.WriteOutAndReadBack(workbook))
{
sheet = wbCopy.GetSheetAt(0) as XSSFSheet;
var comment2 = sheet.GetHyperlink(cellAddress);
Assert.IsNotNull(comment2);
Assert.IsTrue(comment2.Address.Equals(address));

sheet.RemoveHyperlink(cellAddress.Row, cellAddress.Column);

using (var wbCopy2 = XSSFTestDataSamples.WriteOutAndReadBack(wbCopy))
{
sheet = wbCopy2.GetSheetAt(0) as XSSFSheet;
var comment3 = sheet.GetHyperlink(cellAddress);
Assert.IsNull(comment3);
}
}
}
}
}

}
Loading