SharpCell

Cell

public sealed class Cell

Namespace SharpCell · package SharpCell

A handle to one cell position. Creating a handle does not create the cell; storage is allocated only when a value or formula is set.

Properties

Address

public string Address { get; }

The A1 address without the sheet name, such as B2.

Column

public int Column { get; }

The column number, from 1 (A is 1).

ConditionalFormats

public IReadOnlyList<ConditionalFormat> ConditionalFormats { get; }

The conditional formats that apply to the cell now, highest priority first, as Excel works them out from the cell's value and the values of the rules' ranges: up to the first that applies with ConditionalFormat.StopIfTrue. Values are those of the last Workbook.Recalculate; rule formulas are calculated on each read.

Dependents

public IReadOnlyList<Cell> Dependents { get; }

The formula cells that read this cell in their last calculation, alone or in a range; for a cell whose formula spills, also the cells it spilled into. Direct readers only: follow Cell.Dependents again for the cells that read those. In the order of their sheets in the workbook, then by row and column.

Exceptions

DisplayStyle

public CellStyle DisplayStyle { get; }

The cell's style with the conditional formats that apply to it put over it, as Excel shows the cell (its Range.DisplayFormat): where rules set the same thing, the higher one's wins. A rule's number format is not part of it.

Formula

public string? Formula { get; set; }

Formula text starting with '=' (added if missing), or null for no formula. The text is parsed on assignment; invalid text throws FormulaParseException and leaves the cell unchanged.

Exceptions

public Hyperlink? Hyperlink { get; set; }

The cell's hyperlink; null for none, which setting null also gives. The cell's value is what Excel shows: setting a link neither changes the value nor applies the Hyperlink cell style. A link moves with its cell when rows or columns are inserted, and goes with it when they are deleted.

Input

public string Input { get; set; }

The cell as typed in Excel. Setting it does what typing the text and pressing Enter does: = starts a formula; ' keeps the rest as text; TRUE, FALSE and error literals such as #N/A are those values; numbers, percentages, currency and grouped digits, dates and times are read in the workbook's Workbook.Culture, as Excel reads them in arithmetic and DATEVALUE (a date without a year is in the current year); anything else is text; an empty string clears the cell's content. A cell in the General format takes a format from what was typed, as in Excel: 12% gives 0%, 1,250.50 gives #,##0.00, $5 a currency format, a date the culture's short date, a time h:mm with seconds and AM/PM when they were typed. A cell with a format of its own keeps it.

Getting it gives the text Excel shows to edit the cell, which set again gives the same content: the formula, a number in full (a percentage as one, a date or time as the culture writes it), text with an apostrophe in front when it would read as something else.

Exceptions

Note

public Note? Note { get; set; }

The cell's note, or null for none, which setting null also gives. A note moves with its cell when rows, columns or cells are inserted, goes with it when they are deleted, and is copied with it by CellRange.CopyTo.

NumberFormat

public string NumberFormat { get; set; }

The number format Excel shows the cell with, as a format code such as 0.00, #,##0 or yyyy-mm-dd; General when the cell has none. Formats are read from .xlsx files and shape Cell.Text; they play no part in calculation. An empty cell can have a format, and a format alone does not make a cell hold something.

Exceptions

Precedents

public IReadOnlyList<CellRange> Precedents { get; }

The cells and ranges the cell's formula read in its last calculation: references in the formula, in the defined names it uses, and those built while calculating (INDIRECT, OFFSET). A branch of IF that was not taken is not included. A cell filled by another cell's spill has that cell as its precedent; a constant or empty cell has none. The ranges come in the order of their sheets in the workbook, then by their top-left cell.

Exceptions

Row

public int Row { get; }

The row number, from 1.

Style

public CellStyle Style { get; set; }

How the cell looks: font, fill, borders and alignment. A cell without a style of its own has Workbook.DefaultStyle; setting null gives it back. Styles are read from .xlsx files; they play no part in calculation, and a style alone does not make a cell hold something.

Text

public string Text { get; }

The cell as Excel shows it: the value formatted by Cell.NumberFormat with the workbook's culture and date system. Column width plays no part, so a number that fits no width is never cut short; a date format on a number that is no date shows #######. Errors show as #DIV/0! and the like, logical values as TRUE and FALSE, an empty cell as an empty string.

Validation

public DataValidation? Validation { get; }

The data validation rule of the cell, or null for none. Its formulas are relative to the first cell of the rule's first range, not to this cell. Rules are set with Worksheet.AddValidation.

Value

public CellValue Value { get; set; }

The cell's CellValue: a constant, or the formula's result as of the last calculation (values do not change until Workbook.Recalculate). Setting a value removes the formula. Cells hold scalars only: Missing, arrays and lambdas cannot be stored.

Exceptions

Worksheet

public Worksheet Worksheet { get; }

The sheet the cell belongs to.

Methods

ToString()

public override string ToString()

The address with the sheet name, such as Sheet1!B2.