Skip to content

Commit 7a56a79

Browse files
authored
Fixed issue #1653 and enabled update of the Workbook.MaxFontWidth property. (#1654)
1 parent 6629fbf commit 7a56a79

File tree

4 files changed

+80
-34
lines changed

4 files changed

+80
-34
lines changed

src/EPPlus/ExcelWorkbook.cs

+40-26
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,23 @@ private void GetSharedStrings()
341341
if (xr.LocalName == "t" && xr.NodeType == XmlNodeType.Element)
342342
{
343343
var text = ConvertUtil.ExcelDecodeString(xr.ReadElementContentAsString());
344-
_sharedStringsListNew.Add(new SharedStringTextItem() { Text = text, Position = index });
345-
if (!_sharedStringsLookup.ContainsKey(text))
344+
if(xr.Name=="r" && xr.NodeType == XmlNodeType.Element)
346345
{
347-
_sharedStringsLookup.Add(text, index++);
348-
}
349-
}
350-
else if (xr.LocalName == "r" && xr.NodeType == XmlNodeType.Element)
346+
var item = new SharedStringRichTextItem() { RichText = new ExcelRichTextCollection(xr, this), Position = index++ };
347+
item.RichText.Insert(0, text);
348+
_sharedStringsListNew.Add(item);
349+
text = item.RichText.GetXML();
350+
}
351+
else
352+
{
353+
_sharedStringsListNew.Add(new SharedStringTextItem() { Text = text, Position = index });
354+
if (!_sharedStringsLookup.ContainsKey(text))
355+
{
356+
_sharedStringsLookup.Add(text, index++);
357+
}
358+
}
359+
}
360+
if (xr.LocalName == "r" && xr.NodeType == XmlNodeType.Element)
351361
{
352362
var item = new SharedStringRichTextItem() { RichText = new ExcelRichTextCollection(xr, this), Position = index++ };
353363
_sharedStringsListNew.Add(item);
@@ -734,34 +744,37 @@ public ExcelThreadedCommentPersonCollection ThreadedCommentPersons
734744
}
735745
}
736746
/// <summary>
737-
/// Max font width for the workbook
738-
/// <remarks>This method uses GDI. If you use Azure or another environment that does not support GDI, you have to set this value manually if you don't use the standard Calibri font</remarks>
747+
/// Max font width for the workbook, used in the calculation of column widths.
748+
/// <remarks>This property uses the static <see cref="FontSize.FontWidths"></see> dictionary to get the max font width /></remarks>
739749
/// </summary>
740750
public decimal MaxFontWidth
741751
{
742752
get
743753
{
744-
var ix = Styles.GetNormalStyleIndex();
745-
if (ix >= 0)
754+
if (_standardFontWidth == decimal.MinValue)
746755
{
747-
var font = Styles.NamedStyles[ix].Style.Font;
748-
if (font.Index == int.MinValue) font.Index = 0;
749-
if (_standardFontWidth == decimal.MinValue || _fontID != font.Id)
756+
var ix = Styles.GetNormalStyleIndex();
757+
if (ix >= 0)
750758
{
751-
try
759+
var font = Styles.NamedStyles[ix].Style.Font;
760+
if (font.Index == int.MinValue) font.Index = 0;
761+
if (_standardFontWidth == decimal.MinValue || _fontID != font.Id)
752762
{
753-
_standardFontWidth = FontSize.GetWidthPixels(font.Name, font.Size);
754-
_fontID = Styles.NamedStyles[ix].Style.Font.Id;
755-
}
756-
catch //Error, Font missing and Calibri removed in dictionary
757-
{
758-
_standardFontWidth = (int)(font.Size * (2D / 3D)); //Aprox for Calibri.
763+
try
764+
{
765+
_standardFontWidth = FontSize.GetWidthPixels(font.Name, font.Size);
766+
_fontID = Styles.NamedStyles[ix].Style.Font.Id;
767+
}
768+
catch //Error, Font missing and Calibri removed in dictionary
769+
{
770+
_standardFontWidth = (int)(font.Size * (2D / 3D)); //Aprox for Calibri.
771+
}
759772
}
760773
}
761-
}
762-
else
763-
{
764-
_standardFontWidth = 7; //Calibri 11
774+
else
775+
{
776+
_standardFontWidth = 7; //Calibri 11
777+
}
765778
}
766779
return _standardFontWidth;
767780
}
@@ -1957,13 +1970,14 @@ internal ExcelTable GetTable(string name)
19571970

19581971
internal void ClearDefaultHeightsAndWidths()
19591972
{
1960-
foreach (var ws in Worksheets)
1973+
_standardFontWidth = decimal.MinValue;
1974+
foreach (var ws in Worksheets)
19611975
{
19621976
if (ws.IsChartSheet == false)
19631977
{
19641978
if (ws.CustomHeight == false)
19651979
{
1966-
ws._defaultRowHeight = double.NaN;
1980+
ws._defaultRowHeight = double.NaN;
19671981
}
19681982
}
19691983
}

src/EPPlus/Style/RichText/ExcelRichTextCollection.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public ExcelRichText Insert(int index, string text)
158158
}
159159
else
160160
{
161-
prevIndex = index - 1;
161+
prevIndex = index <= 0 ? 0 : index - 1;
162162
}
163163
if(_list.Count > 0)
164164
{
@@ -175,12 +175,7 @@ public ExcelRichText Insert(int index, string text)
175175
rt.ColorSettings = prevRT.ColorSettings.Clone();
176176
rt.PreserveSpace = prevRT.PreserveSpace;
177177
}
178-
else if(_cells == null)
179-
{
180-
rt.FontName = "Calibri";
181-
rt.Size = 11;
182-
}
183-
else
178+
else if(_cells != null)
184179
{
185180
var style = _cells.Offset(0, 0).Style;
186181
rt.FontName = style.Font.Name;

src/EPPlusTest/Issues/DrawingIssues.cs

+24
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,29 @@ public void i1446()
4343
var sbr = sheet.Drawings[1].As.Control.ScrollBar;
4444
}
4545
}
46+
47+
[TestMethod]
48+
public void i1640()
49+
{
50+
using (var package = OpenTemplatePackage("i1640.xlsx"))
51+
{
52+
package.Workbook.MaxFontWidth = 8;
53+
FontSize.FontWidths.Add("MS Pゴシック", new Dictionary<float, short> { { 11, 8 } });
54+
55+
var sheet = package.Workbook.Worksheets[0];
56+
CopyRows(sheet, 1, 10, 11, 20);
57+
SaveAndCleanup(package);
58+
}
59+
}
60+
public void CopyRows(ExcelWorksheet excelWorksheet, int sourceFrom, int sourceTo, int destFrom, int destTo)
61+
{
62+
for (int i = destFrom; i <= destTo; i++)
63+
{
64+
excelWorksheet.Row(i).Height = excelWorksheet.Row(sourceFrom + i - destFrom).Height;
65+
}
66+
67+
excelWorksheet.Cells[sourceFrom.ToString() + ":" + sourceTo].Copy(
68+
excelWorksheet.Cells[destFrom.ToString() + ":" + destTo]);
69+
}
4670
}
4771
}

src/EPPlusTest/Issues/RangeTextIssues.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using OfficeOpenXml.FormulaParsing;
55
using System.Globalization;
6+
using System.Diagnostics;
67

78
namespace EPPlusTest.Issues
89
{
@@ -40,6 +41,18 @@ public void s667()
4041

4142
}
4243
}
43-
44+
[TestMethod]
45+
public void i1653()
46+
{
47+
using(var package = OpenTemplatePackage("i1653.xlsx"))
48+
{
49+
ExcelWorkbook workbook = package.Workbook;
50+
ExcelWorksheet worksheet = workbook.Worksheets[0];
51+
var cell = worksheet.Cells["C17"].Text;
52+
53+
// Assert failed: `cell.Text` and `cell.Value` are both empty string
54+
Debug.Assert(cell == "{\"any_orange_frag\" : 700, \"any_purple_frag\" : 800,\"any_blue_frag\" : 1200, \"any_green_frag\":2500}");
55+
}
56+
}
4457
}
4558
}

0 commit comments

Comments
 (0)