Skip to content
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
2 changes: 1 addition & 1 deletion src/MiniExcel/Markdown/ExcelMarkdownWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private static Task WriteRowAsync(StreamWriter writer, IDictionary<string, objec
private static string FormatCellValue(IDictionary<string, object> row, string column)
{
var value = FormatValue(row[column]);
if (row is ExcelOpenXmlSheetReader.ExcelRow excelRow && excelRow.Formulas.TryGetValue(column, out var formula))
if (row is ExcelOpenXmlSheetReader.ExcelRow excelRow && excelRow.TryGetFormula(column, out var formula))
return string.IsNullOrEmpty(value) ? $"={formula}" : $"{value} (formula: ={formula})";
return value;
}
Expand Down
46 changes: 40 additions & 6 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private IEnumerable<IDictionary<string, object>> QueryRow(
columnIndex = cellAndColumn.ColumnIndex;

if (!string.IsNullOrEmpty(cellAndColumn.Formula) && cell is ExcelRow excelRow)
excelRow.Formulas[ColumnHelper.GetAlphabetColumnName(columnIndex)] = cellAndColumn.Formula;
excelRow.SetFormula(ColumnHelper.GetAlphabetColumnName(columnIndex), cellAndColumn.Formula);

if (_config.FillMergedCells)
{
Expand Down Expand Up @@ -408,8 +408,26 @@ private ZipArchiveEntry GetSheetEntry(string sheetName)

private static IDictionary<string, object> GetCell(bool useHeaderRow, int maxColumnIndex, Dictionary<int, string> headRows, int startColumnIndex, int rowNumber)
{
var values = useHeaderRow ? CustomPropertyHelper.GetEmptyExpandoObject(headRows) : CustomPropertyHelper.GetEmptyExpandoObject(maxColumnIndex, startColumnIndex);
return new ExcelRow(values, rowNumber);
var capacity = useHeaderRow ? headRows.Count : Math.Max(0, maxColumnIndex - startColumnIndex + 1);
var cell = new ExcelRow(capacity, rowNumber);

if (useHeaderRow)
{
foreach (var header in headRows)
if (!cell.ContainsKey(header.Value))
cell.Add(header.Value, null);
}
else
{
for (var i = startColumnIndex; i <= maxColumnIndex; i++)
{
var key = ColumnHelper.GetAlphabetColumnName(i);
if (!cell.ContainsKey(key))
cell.Add(key, null);
}
}

return cell;
}

private static void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, Dictionary<int, string> headRows, bool isFirstRow, IDictionary<string, object> cell, int columnIndex)
Expand Down Expand Up @@ -573,7 +591,7 @@ internal List<SheetRecord> GetWorkbookRels(ReadOnlyCollection<ZipArchiveEntry> e
return sheetRecords;
}

internal class CellAndColumn
internal readonly struct CellAndColumn
{
public object CellValue { get; }
public int ColumnIndex { get; } = -1;
Expand All @@ -589,13 +607,29 @@ public CellAndColumn(object cellValue, int columnIndex, string formula = null)

internal sealed class ExcelRow : Dictionary<string, object>
{
internal ExcelRow(IDictionary<string, object> values, int rowNumber) : base(values)
private Dictionary<string, string> _formulas;

internal ExcelRow(int capacity, int rowNumber) : base(capacity)
{
RowNumber = rowNumber;
}

internal int RowNumber { get; }
internal Dictionary<string, string> Formulas { get; } = new Dictionary<string, string>();

internal void SetFormula(string column, string formula)
{
if (_formulas == null)
_formulas = new Dictionary<string, string>();
_formulas[column] = formula;
}

internal bool TryGetFormula(string column, out string formula)
{
if (_formulas != null)
return _formulas.TryGetValue(column, out formula);
formula = null;
return false;
}
}

private CellAndColumn ReadCellAndSetColumnIndex(XmlReader reader, int columnIndex, bool withoutCR, int startColumnIndex, string aR, string aT)
Expand Down
22 changes: 18 additions & 4 deletions src/MiniExcel/Utils/ReferenceHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Globalization;

namespace MiniExcelLibs.Utils;
namespace MiniExcelLibs.Utils;

internal static class ReferenceHelper
{
Expand Down Expand Up @@ -91,6 +89,22 @@ public static bool ParseReference(string value, out int column, out int row)
if (position == 0)
return false;

return int.TryParse(value.Substring(position), NumberStyles.None, CultureInfo.InvariantCulture, out row) && row > 0;
for (; position < value.Length; position++)
{
var c = value[position];
if (c < '0' || c > '9')
return false;

var digit = c - '0';
if (row > (int.MaxValue - digit) / 10)
{
row = 0;
return false;
}

row = row * 10 + digit;
}

return row > 0;
}
}
20 changes: 20 additions & 0 deletions tests/MiniExcelTests/MiniExcelOpenXmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ public void QueryCastToIDictionary()
}
}

[Theory]
[InlineData("A1", 1, 1)]
[InlineData("XFD1048576", 16384, 1048576)]
[InlineData("aa42", 27, 42)]
public void ParseValidCellReference(string reference, int expectedColumn, int expectedRow)
{
Assert.True(MiniExcelLibs.Utils.ReferenceHelper.ParseReference(reference, out var column, out var row));
Assert.Equal(expectedColumn, column);
Assert.Equal(expectedRow, row);
}

[Theory]
[InlineData("A0")]
[InlineData("A1x")]
[InlineData("A2147483648")]
public void RejectInvalidCellReference(string reference)
{
Assert.False(MiniExcelLibs.Utils.ReferenceHelper.ParseReference(reference, out _, out _));
}

[Fact]
public void QueryRangeToIDictionary()
{
Expand Down
Loading