Skip to content

Commit

Permalink
Change Formula attribute to ColumnType (Value, Formula)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eulises Vargas committed Oct 15, 2024
1 parent 892600b commit c707ed4
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/MiniExcel/Attributes/ExcelColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ExcelColumnAttribute : Attribute

public bool Ignore { get; set; }

public bool Formula { get; set; } = false;
public ColumnType Type { get; set; } = ColumnType.Value;

public int Index
{
Expand Down Expand Up @@ -52,6 +52,7 @@ private void Init(int index, string columnName = null)
}
}

public enum ColumnType { Value, Formula }

public class DynamicExcelColumn : ExcelColumnAttribute
{
Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel/OpenXml/Constants/WorksheetXml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using MiniExcelLibs.Attributes;

namespace MiniExcelLibs.OpenXml.Constants
{
Expand Down Expand Up @@ -52,14 +53,13 @@ internal static string Column(int? colIndex, double? columnWidth)
internal static string EmptyCell(string cellReference, string styleIndex)
=> $"<x:c r=\"{cellReference}\" s=\"{styleIndex}\"></x:c>";
//t check avoid format error ![image](https://user-images.githubusercontent.com/12729184/118770190-9eee3480-b8b3-11eb-9f5a-87a439f5e320.png)
internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false, bool formula = false)
=> $"<x:c r=\"{cellReference}\"{(cellType == null ? string.Empty : $" t=\"{cellType}\"")} s=\"{styleIndex}\"{(preserveSpace ? " xml:space=\"preserve\"" : string.Empty)}><x:{(formula ? "f" : "v")}>{cellValue}</x:{(formula ? "f" : "v")}></x:c>";
internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false, ColumnType columnType = ColumnType.Value)
=> $"<x:c r=\"{cellReference}\"{(cellType == null ? string.Empty : $" t=\"{cellType}\"")} s=\"{styleIndex}\"{(preserveSpace ? " xml:space=\"preserve\"" : string.Empty)}><x:{(columnType == ColumnType.Formula ? "f" : "v")}>{cellValue}</x:{(columnType == ColumnType.Formula ? "f" : "v")}></x:c>";

internal static string Autofilter(string dimensionRef)
=> $"<x:autoFilter ref=\"{dimensionRef}\" />";

internal static string Drawing(int sheetIndex)
=> $"<x:drawing r:id=\"drawing{sheetIndex}\" />";

}
}
6 changes: 4 additions & 2 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniExcelLibs.OpenXml.Constants;
using MiniExcelLibs.Attributes;
using MiniExcelLibs.OpenXml.Constants;
using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip;
using System;
Expand Down Expand Up @@ -459,10 +460,11 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde
var styleIndex = tuple.Item1;
var dataType = tuple.Item2;
var cellValue = tuple.Item3;
var columnType = p.ExcelColumnType;

/*Prefix and suffix blank space will lost after SaveAs #294*/
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: p.ExcelFormula));
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType));
}

private async Task GenerateEndXmlAsync(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName

prop.Nullable = true;
prop.ExcelIgnore = dynamicColumn.Ignore;
prop.ExcelFormula = dynamicColumn.Formula;
prop.ExcelColumnType = dynamicColumn.Type;
prop.ExcelColumnIndex = dynamicColumn.Index;
prop.ExcelColumnWidth = dynamicColumn.Width;
//prop.ExcludeNullableType = item2[key]?.GetType();
Expand Down
7 changes: 4 additions & 3 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniExcelLibs.OpenXml.Constants;
using MiniExcelLibs.Attributes;
using MiniExcelLibs.OpenXml.Constants;
using MiniExcelLibs.OpenXml.Models;
using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip;
Expand Down Expand Up @@ -579,11 +580,11 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
var styleIndex = tuple.Item1; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cell?view=openxml-3.0.1
var dataType = tuple.Item2; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cellvalues?view=openxml-3.0.1
var cellValue = tuple.Item3;
var columnType = columnInfo?.ExcelColumnType ?? ColumnType.Value;

/*Prefix and suffix blank space will lost after SaveAs #294*/
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
var isFormula = columnInfo?.ExcelFormula ?? false;
writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: isFormula));
writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType));
}

private static void WriteCell(MiniExcelStreamWriter writer, string cellReference, string columnName)
Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel/Utils/CustomPropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class ExcelColumnInfo
public string ExcelIndexName { get; internal set; }
public bool ExcelIgnore { get; internal set; }
public int ExcelFormatId { get; internal set; }
public bool ExcelFormula { get; internal set; }
public ColumnType ExcelColumnType { get; internal set; }
}

internal class ExcellSheetInfo
Expand Down Expand Up @@ -208,7 +208,7 @@ private static IEnumerable<ExcelColumnInfo> ConvertToExcelCustomPropertyInfo(Pro
ExcelColumnWidth = p.GetAttribute<ExcelColumnWidthAttribute>()?.ExcelColumnWidth ?? excelColumn?.Width,
ExcelFormat = excelFormat ?? excelColumn?.Format,
ExcelFormatId = excelColumn?.FormatId ?? -1,
ExcelFormula = excelColumn?.Formula ?? false
ExcelColumnType = excelColumn?.Type ?? ColumnType.Value
};
}).Where(_ => _ != null);
}
Expand Down Expand Up @@ -309,7 +309,7 @@ internal static void SetDictionaryColumnInfo(List<ExcelColumnInfo> _props, objec
p.ExcelColumnName = dynamicColumn.Name;
isIgnore = dynamicColumn.Ignore;
p.ExcelColumnWidth = dynamicColumn.Width;
p.ExcelFormula = dynamicColumn.Formula;
p.ExcelColumnType = dynamicColumn.Type;
}
}
if (!isIgnore)
Expand Down

0 comments on commit c707ed4

Please sign in to comment.