<!-- Generated by tools/SharpCell.Docs from the XML documentation comments. Do not edit; run the tool with 'generate'. -->

# Workbook

```csharp
public sealed class Workbook
```

Namespace `SharpCell` · package `SharpCell`

A workbook: sheets, defined names and calculation settings. Not thread-safe; separate workbooks can be used from different threads.

## Constructors

<a id="ctor"></a>
### Workbook()

```csharp
public Workbook()
```

An empty workbook with no sheets, the invariant culture and the 1900 date system.

## Properties

<a id="culture"></a>
### Culture

```csharp
public CultureInfo Culture { get; set; }
```

Culture for text-to-number conversion, number-to-text conversion and text ordering. The process culture is never used. Defaults to the invariant culture.

<a id="datesystem"></a>
### DateSystem

```csharp
public DateSystem DateSystem { get; set; }
```

Whether serial dates count from 1900 or from 1904, as set in the file.

<a id="defaultstyle"></a>
### DefaultStyle

```csharp
public CellStyle DefaultStyle { get; set; }
```

The style of every cell without one of its own: the Normal style of a file read with SharpCell.Xlsx, [CellStyle.Default](SharpCell.CellStyle.md#default) for a new workbook.

**Exceptions**

- `ArgumentNullException`: The style is null.

<a id="definednames"></a>
### DefinedNames

```csharp
public IReadOnlyList<DefinedName> DefinedNames { get; }
```

The defined names, workbook-wide and sheet-scoped, in the order they were first defined: a snapshot that later definitions do not change.

<a id="diagnostics"></a>
### Diagnostics

```csharp
public IReadOnlyList<CalculationDiagnostic> Diagnostics { get; }
```

Current calculation problems: circular references and failing functions. An entry of a cell disappears when the cell is edited or calculated without the problem; entries from [Workbook.Evaluate](SharpCell.Workbook.md#evaluate-string) last until the next call of it.

<a id="functions"></a>
### Functions

```csharp
public FunctionCollection Functions { get; }
```

Functions of your own that formulas of this workbook can call, besides Excel's.

<a id="isstructureprotected"></a>
### IsStructureProtected

```csharp
public bool IsStructureProtected { get; }
```

Whether the workbook's structure is locked: Excel then refuses adding, deleting, renaming, moving and hiding sheets.

<a id="iswindowsprotected"></a>
### IsWindowsProtected

```csharp
public bool IsWindowsProtected { get; }
```

Whether the workbook's windows are locked: Excel then refuses moving and resizing them.

<a id="iteration"></a>
### Iteration

```csharp
public IterationSettings Iteration { get; set; }
```

Iterative calculation of circular references, off by default as in Excel; the .xlsx reader sets it from the file. Changing it calculates every formula again at the next [Workbook.Recalculate](SharpCell.Workbook.md#recalculate-cancellationtoken).

<a id="sheets"></a>
### Sheets

```csharp
public IReadOnlyList<Worksheet> Sheets { get; }
```

The sheets in order.

<a id="tables"></a>
### Tables

```csharp
public IReadOnlyList<Table> Tables { get; }
```

The tables of all sheets, in the order they were added.

<a id="item-string"></a>
### this[string]

```csharp
public Worksheet this[string name] { get; }
```

Gets a sheet by name, ignoring case.

## Methods

<a id="addsheet-string"></a>
### AddSheet(string)

```csharp
public Worksheet AddSheet(string name)
```

Adds a sheet at the end.

**Exceptions**

- `ArgumentException`: The name is not a valid sheet name, or a sheet with that name exists.

<a id="clone"></a>
### Clone()

```csharp
public Workbook Clone()
```

An independent copy: sheets, cells and their calculated values, formulas still out of date, spills, diagnostics, defined names, tables, hidden rows, settings and custom functions (the same delegates), and the file it was read from, so `XlsxWriter` can save the copy too. Nothing is calculated again. Parsed formulas and the file's bytes are shared, not copied.

Cloning only reads this workbook, so several threads may clone it at the same time as long as nothing changes or calculates it meanwhile: a service can load a template once and clone it for each request. Like any workbook, each copy is for one thread at a time.

**Returns:** The copy; [Workbook.Recalculate](SharpCell.Workbook.md#recalculate-cancellationtoken) on it reports changes from this point on only.

<a id="definename-string-string-worksheet"></a>
### DefineName(string, string, Worksheet)

```csharp
public void DefineName(string name, string formula, Worksheet? scope = null)
```

Defines or replaces a name. A name with a `scope` is visible only on that sheet.

<a id="evaluate-string"></a>
### Evaluate(string)

```csharp
public CellValue Evaluate(string formula)
```

Evaluates a formula that belongs to no cell, as if it were in cell A1 of the first sheet. Out-of-date cells it reads are calculated first. References without a sheet are `#REF!` when the workbook has no sheets. The formula is in no table and has no row of its own: a table reference without a table name is `#REF!` and `[#This Row]` is `#VALUE!`.

<a id="evaluate-string-cancellationtoken"></a>
### Evaluate(string, CancellationToken)

```csharp
public CellValue Evaluate(string formula, CancellationToken cancellationToken)
```

Evaluates a formula that belongs to no cell, as if it were in cell A1 of the first sheet. Out-of-date cells it reads are calculated first. References without a sheet are `#REF!` when the workbook has no sheets. The formula is in no table and has no row of its own: a table reference without a table name is `#REF!` and `[#This Row]` is `#VALUE!`.

**Exceptions**

- `OperationCanceledException`: The token was cancelled.

<a id="protect-string-bool-bool"></a>
### Protect(string, bool, bool)

```csharp
public void Protect(string? password = null, bool structure = true, bool windows = false)
```

Protects the workbook as Excel's Protect Workbook does. SharpCell itself does not enforce protection: code may still add, remove and rename sheets, as Excel's object model may with the password. Protecting a protected workbook replaces its locks and password.

**Parameters**

- `password`: The password Excel asks for to unprotect the workbook, or null (or empty) for none. Only its hash is kept, made as Excel makes it (SHA-512, a random salt, 100,000 spins); it cannot be read back.
- `structure`: Whether to lock the structure: adding, deleting, renaming, moving and hiding sheets.
- `windows`: Whether to lock the windows' position and size.

**Exceptions**

- `ArgumentException`: Both `structure` and `windows` are false.

<a id="recalculate-cancellationtoken"></a>
### Recalculate(CancellationToken)

```csharp
public RecalculationResult Recalculate(CancellationToken cancellationToken = default)
```

Calculates every formula that is out of date: those whose inputs changed since the last calculation and those using volatile functions (NOW, RAND).

**Returns:** The cells whose values calculation changed since the previous call, including what [Workbook.Evaluate](SharpCell.Workbook.md#evaluate-string) calculated in between.

**Exceptions**

- `OperationCanceledException`: The token was cancelled; finished cells keep their new values, and their changes are reported by the next call.

<a id="trygetsheet-string-worksheet"></a>
### TryGetSheet(string, Worksheet)

```csharp
public bool TryGetSheet(string name, out Worksheet? sheet)
```

Finds a sheet by name, ignoring case.

**Returns:** Whether the sheet exists.

<a id="trygettable-string-table"></a>
### TryGetTable(string, Table)

```csharp
public bool TryGetTable(string name, out Table? table)
```

Finds a table by name, ignoring case.

**Returns:** Whether the table exists.

<a id="unprotect"></a>
### Unprotect()

```csharp
public void Unprotect()
```

Removes the workbook's protection and its password, as Excel's Unprotect Workbook does once the password is given.
