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 issue when pulling from cells with some types of formulas is them #23

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 Excel_Adapter/CRUD/Read/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private List<IBHoMObject> ReadExcel(XLWorkbook workbook, string worksheet, strin
foreach (IXLRangeColumn column in ixlRange.Columns())
{
if (valuesOnly)
dataRow.Add(ixlWorksheet.Cell(row.RowNumber(), column.ColumnNumber()).GetValue<object>());
dataRow.Add(ixlWorksheet.Cell(row.RowNumber(), column.ColumnNumber()).CellValueOrCachedValue());
else
dataRow.Add((ixlWorksheet.Cell(row.RowNumber(), column.ColumnNumber())).FromExcel());
}
Expand Down
23 changes: 22 additions & 1 deletion Excel_Adapter/Convert/FromExcel/CellContents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,37 @@ public static CellContents FromExcel(this IXLCell xLCell)
return new CellContents()
{
Comment = xLCell.HasComment ? xLCell.Comment.Text : "",
Value = xLCell.Value,
Value = xLCell.CellValueOrCachedValue(),
Address = BH.Engine.Excel.Create.CellAddress(xLCell.Address.ToString()),
DataType = xLCell.DataType.SystemType(),
FormulaA1 = xLCell.FormulaA1,
FormulaR1C1 = xLCell.FormulaR1C1,
HyperLink = xLCell.HasHyperlink ? xLCell.Hyperlink.ExternalAddress.ToString() : "",
RichText = xLCell.HasRichText ? xLCell.RichText.Text : ""
};


}

/*******************************************/

[Description("Gets the value of the cell, or cached value if the TryGetValue method fails. Raises a warning if the cached value is used, and ClosedXML beleives the cell needs to be recalculated.")]
[Input("xLCell", "IXLCell to get the (cached) value from.")]
[Input("value", "Value or cached value of the cell.")]
public static object CellValueOrCachedValue(this IXLCell xLCell)
{
object value;
if (!xLCell.TryGetValue(out value))
{
//If not able to just get the value, then get the cached value
//If cell is flagged as needing recalculation, raise warning.
if (xLCell.NeedsRecalculation)
BH.Engine.Base.Compute.RecordWarning($"Cell {xLCell?.Address?.ToString() ?? "unknown"} is flagged as needing to be recalculated, but this is not able to be done. The cached value for this cell is returned, which for most cases is correct, but please check the validity of the value.");

value = xLCell.CachedValue;
}
return value;
}

/*******************************************/
/**** Private Methods ****/
Expand Down