using ClosedXML.Excel;
using JdeScoping.ExcelIO.Attributes;
namespace JdeScoping.ExcelIO.Formatting;
///
/// Column width and number format utilities.
///
public static class ColumnFormatter
{
///
/// Applies column formatting based on attribute settings.
///
/// The column to format.
/// The output column attribute with settings.
public static void ApplyColumnFormat(IXLColumn column, OutputColumnAttribute attr)
{
// Set number format
column.Style.NumberFormat.Format = attr.Format;
// Handle wrap text
if (attr.WrapText)
{
column.Style.Alignment.WrapText = true;
}
// Handle width
if (attr.WrapText && !attr.AutoWidth)
{
// Wrapped columns with fixed width skip auto-fit
column.Width = attr.Width;
}
else if (attr.AutoWidth)
{
column.AdjustToContents();
column.Width *= ExcelFormats.DataPaddingFactor;
}
else
{
column.Width = attr.Width;
}
}
///
/// Auto-fits a column with the specified padding factor.
///
/// The column to auto-fit.
/// The padding factor to apply (e.g., 1.15 for 15% padding).
public static void AutoFitWithPadding(IXLColumn column, double paddingFactor)
{
column.AdjustToContents();
column.Width *= paddingFactor;
}
}