Checking and setting Bold font property on active cell

Posted by: kevin.keller on 13 May 2019, 11:38 pm EST

    • Post Options:
    • Link

    Posted 13 May 2019, 11:38 pm EST

    Trying to determine if a current Cell has Bold font on it. I thought it would be as simple as:

    if FpSpread.ActiveSheet.ActiveCell.Font.Bold then

    ’ do stuff

    end if

    but getting the below error… it just doesn’t make sense that I can’t check the bold property of the current cell.

    FpSpread.ActiveSheet.ActiveCell.Font.Bold = ‘FpSpread.ActiveSheet.ActiveCell.Font.Bold’ threw an exception of type ‘System.NullReferenceException’

    I eventually want to be able to set the font on specific cells to bold programmatically as well but I need to be able to check the Bold Property first.

    Thanks for any guidance.

    Kevin

  • Posted 14 May 2019, 10:25 pm EST

    Hi,

    The old API used different styles for cell, rows, column, etc. When a cell needs to be drawn, all these styles are combined. When we use fpSpread.ActiveSheet.ActiveCell.Font, a null object would be returned unless we had set a Font to that specific cell.

    So, even if a cell appears Bold (because of the inherited Font from Row or column, ActiveCell.Font would still give null).

    The new API directly gives the composited style, so you can use this:

    Cell activeCell = fpSpread1.ActiveSheet.ActiveCell;
    IWorksheet worksheet = fpSpread1.AsWorkbook().ActiveSheet;
    bool isBold = worksheet.Cells[activeCell.Row.Index, activeCell.Column.Index].Font.Bold;
    

    Regards,

    Jitender

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels