Class TwSpreadsheet
- Namespace
- TwBlazor.Components
- Assembly
- TwBlazor.dll
Changing cells: editing, the clipboard, undo and redo, and formatting.
public class TwSpreadsheet : TwBlazorComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, IAsyncDisposable
- Inheritance
-
TwSpreadsheet
- Implements
- Inherited Members
Remarks
Pass the workbook as Sheets: a list of Sheet, each holding its cells by address. The
spreadsheet edits the list and its cells in place, and reports every change through OnCellsChange, so
the workbook can be saved as it changes. Formulas are worked out in .NET, with no other package, and can refer to
cells on other sheets, as in =Budget!B4. The workbook exports as an Excel file that keeps the formulas and the
formatting, or as CSV, through ExportFormats or TwSpreadsheetExport.
The grid follows the ARIA grid pattern: it is a single Tab stop, the arrow keys move the active cell, and Shift with an arrow key extends the selection. Typing replaces the active cell, Enter or F2 edits it, and the toolbar, formula bar and sheet tabs are reached with Tab.
Constructors
TwSpreadsheet()
public TwSpreadsheet()
Properties
ActiveCell
Gets the address of the active cell, the one typing goes into, such as B3.
public string ActiveCell { get; }
Property Value
ActiveSheet
Gets the sheet being shown.
public Sheet ActiveSheet { get; }
Property Value
ActiveSheetIndex
Gets or sets the index of the sheet being shown. Defaults to the first.
[Parameter]
public int ActiveSheetIndex { get; set; }
Property Value
ActiveSheetIndexChanged
Gets or sets the callback invoked with the index of the sheet being shown after the user switches sheet.
[Parameter]
public EventCallback<int> ActiveSheetIndexChanged { get; set; }
Property Value
AllowAddSheet
Gets or sets whether the user can add sheets with the button after the tabs. Defaults to true.
[Parameter]
public bool AllowAddSheet { get; set; }
Property Value
AllowFullScreen
Gets or sets whether the toolbar has a button that turns full screen mode on and off. Defaults to true.
[Parameter]
public bool AllowFullScreen { get; set; }
Property Value
ColumnWidth
Gets or sets the width of every column, in pixels. Defaults to 104.
[Parameter]
public int ColumnWidth { get; set; }
Property Value
Columns
Gets or sets the number of columns shown. Defaults to 26, A to Z.
[Parameter]
public int Columns { get; set; }
Property Value
ExportFileName
Gets or sets the name exported files are saved with, without the extension. Defaults to workbook.
[Parameter]
public string ExportFileName { get; set; }
Property Value
ExportFormats
Gets or sets the formats offered by an export menu at the end of the toolbar, in the order listed. Empty (the default) shows no export menu.
[Parameter]
public IReadOnlyList<SpreadsheetExportFormat> ExportFormats { get; set; }
Property Value
FillColors
Gets or sets the colours offered by the toolbar's fill color menu. null (the default) offers nine light colours with names in the app's language.
[Parameter]
public IReadOnlyList<SpreadsheetColor>? FillColors { get; set; }
Property Value
Remarks
A filled cell without its own text colour gets black or white text, whichever reads better on the fill.
FilterOperators
Gets or sets the comparisons the table filters offer, in order, or null (the default) for the ones that suit each column: text comparisons such as "contains", or number comparisons such as "is greater than" for columns of numbers.
[Parameter]
public IReadOnlyList<FilterOperator>? FilterOperators { get; set; }
Property Value
FontSizes
Gets or sets the text sizes offered by the toolbar's size list, in points. null (the default) offers 8 to 24.
[Parameter]
public IReadOnlyList<double>? FontSizes { get; set; }
Property Value
Remarks
Rows keep their height, so text much larger than 18 points is cut off.
Fonts
Gets or sets the fonts offered by the toolbar's font list, after a "Default" option that uses the theme's font. null (the default) offers Arial, Calibri, Cambria, Courier New, Georgia, Segoe UI, Times New Roman and Verdana.
[Parameter]
public IReadOnlyList<string>? Fonts { get; set; }
Property Value
Remarks
Each font is shown in its own typeface in the list, and falls back to the browser's when it is not installed.
FullScreen
Gets or sets whether the spreadsheet covers the whole window. Bind it with @bind-FullScreen to know when
the user toggles it.
[Parameter]
public bool FullScreen { get; set; }
Property Value
Remarks
The page behind stops scrolling while it is on, and Escape turns it off.
FullScreenChanged
Gets or sets the callback invoked when the user turns full screen mode on or off.
[Parameter]
public EventCallback<bool> FullScreenChanged { get; set; }
Property Value
Height
Gets or sets the height of the grid's scrolling area, as a CSS length. Defaults to 24rem.
[Parameter]
public string Height { get; set; }
Property Value
OnCellsChange
Gets or sets the callback invoked after cells changed, through typing, pasting, clearing, formatting, undo or redo.
[Parameter]
public EventCallback<SheetChangeEventArgs> OnCellsChange { get; set; }
Property Value
OnSelectionChange
Gets or sets the callback invoked when the active cell or the selected range changes.
[Parameter]
public EventCallback<SheetSelectionEventArgs> OnSelectionChange { get; set; }
Property Value
ReadOnly
Gets or sets whether the cells can only be read, selected and copied. The toolbar's formatting controls are disabled, and sheets cannot be added or renamed.
[Parameter]
public bool ReadOnly { get; set; }
Property Value
Rows
Gets or sets the number of rows shown. Defaults to 100.
[Parameter]
public int Rows { get; set; }
Property Value
SelectedRange
Gets the selected range, such as B3:D8, or the active cell's address when only one cell is selected.
public string SelectedRange { get; }
Property Value
Sheets
Gets or sets the workbook: the sheets in tab order. An empty list gets one empty sheet.
[Parameter]
public List<Sheet> Sheets { get; set; }
Property Value
Remarks
The spreadsheet edits the list and the cells in place. After changing them from code, call Refresh() so the formulas are worked out again.
ShowFilterConditions
Gets or sets whether the table filters offer conditions, such as "is greater than 100". Defaults to true.
[Parameter]
public bool ShowFilterConditions { get; set; }
Property Value
ShowFilterValues
Gets or sets whether the table filters list the column's values with a checkbox for each. Defaults to true.
[Parameter]
public bool ShowFilterValues { get; set; }
Property Value
ShowFormulaBar
Gets or sets whether the formula bar, with the cell reference and the active cell's contents, is shown. Defaults to true.
[Parameter]
public bool ShowFormulaBar { get; set; }
Property Value
ShowSheetTabs
Gets or sets whether the sheet tabs are shown under the grid. Defaults to true.
[Parameter]
public bool ShowSheetTabs { get; set; }
Property Value
ShowStatusBar
Gets or sets whether the zoom controls and the summary of the selected numbers are shown under the grid. Defaults to true.
[Parameter]
public bool ShowStatusBar { get; set; }
Property Value
ShowToolbar
Gets or sets whether the formatting toolbar is shown. Defaults to true.
[Parameter]
public bool ShowToolbar { get; set; }
Property Value
Remarks
The keyboard shortcuts, such as Ctrl + B, work without it.
TextColors
Gets or sets the colours offered by the toolbar's text color menu. null (the default) offers ten dark colours with names in the app's language.
[Parameter]
public IReadOnlyList<SpreadsheetColor>? TextColors { get; set; }
Property Value
Zoom
Gets or sets the zoom level of the grid, in percent, between 25 and 400. Defaults to 100.
[Parameter]
public int Zoom { get; set; }
Property Value
ZoomChanged
Gets or sets the callback invoked with the new zoom level after the user zooms.
[Parameter]
public EventCallback<int> ZoomChanged { get; set; }
Property Value
Methods
AddSheetAsync(string?)
Adds a sheet after the last one and shows it.
public Task<Sheet> AddSheetAsync(string? name = null)
Parameters
namestringThe sheet's name, or null for the next free name such as
Sheet2. A name already used gets a number added.
Returns
AddTableAsync(string, bool)
Turns a range of the sheet being shown into a table, with a heading row, banded rows and a filter on each
column. Without headings, a row is inserted above the range for them, named Column1, Column2 and
so on.
public Task<SheetTable?> AddTableAsync(string range, bool hasHeaders = true)
Parameters
rangestringThe range, such as
A1:D20.hasHeadersboolWhether the first row of the range holds the column headings.
Returns
- Task<SheetTable>
The new table, or null when the range is not valid or overlaps another table.
BeginTypingAsync()
Starts editing the active cell, after the user typed a character into the grid. Called by
twSpreadsheet.attach, which keeps the characters typed until the cell's field is ready and then puts them in.
[JSInvokable("BeginTyping")]
public Task BeginTypingAsync()
Returns
- Task
A task that completes once the cell's field is shown.
BuildRenderTree(RenderTreeBuilder)
Renders the component to the supplied RenderTreeBuilder.
protected override void BuildRenderTree(RenderTreeBuilder __builder)
Parameters
__builderRenderTreeBuilder
CloseDialogAsync()
Closes the dialog without doing anything, and puts focus back where it was. Called by the dialog's Escape
handling in twSpreadsheet.openDialog, by Cancel and by a press on the backdrop.
[JSInvokable("CloseDialog")]
public Task CloseDialogAsync()
Returns
- Task
A task that completes once the dialog is closed.
DeleteColumnsAsync(int, int)
Deletes columns on the sheet being shown. Cells after them move back, and formulas that referred to a deleted
cell show #REF!.
public Task DeleteColumnsAsync(int column, int count = 1)
Parameters
Returns
- Task
A task that completes once the columns are gone.
DeleteRowsAsync(int, int)
Deletes rows on the sheet being shown. Cells below move up, and formulas that referred to a deleted cell show
#REF!.
public Task DeleteRowsAsync(int row, int count = 1)
Parameters
Returns
- Task
A task that completes once the rows are gone.
DisposeAsync()
public ValueTask DisposeAsync()
Returns
ExitFullScreenAsync()
Leaves full screen mode, called by twSpreadsheet.attach when Escape is pressed outside the
grid, such as on the toolbar.
[JSInvokable("ExitFullScreen")]
public Task ExitFullScreenAsync()
Returns
- Task
A task that completes once full screen mode is off.
ExportAsync(SpreadsheetExportFormat, string?)
Exports the workbook as a file and has the browser save it. The export is announced to screen readers.
public Task<ExportFile> ExportAsync(SpreadsheetExportFormat format, string? fileName = null)
Parameters
formatSpreadsheetExportFormatThe file format. CSV holds the sheet being shown.
fileNamestringThe file name without its extension, or null to use ExportFileName.
Returns
- Task<ExportFile>
The exported file, for example to send it somewhere else as well.
FocusAsync()
Moves focus to the grid, on the active cell.
public Task FocusAsync()
Returns
- Task
A task that completes once focus has moved.
FreezePanes(int, int)
Keeps rows at the top and columns at the start of the sheet being shown in view while the rest scrolls.
public void FreezePanes(int rows, int columns)
Parameters
rowsintThe number of rows to freeze, or 0 for none.
columnsintThe number of columns to freeze, or 0 for none.
GetDisplayText(string, int?)
Gets the text a cell shows: the result of its formula, formatted with its number format.
public string GetDisplayText(string address, int? sheetIndex = null)
Parameters
addressstringThe cell, such as
B3.sheetIndexint?The index of the sheet, or null for the sheet being shown.
Returns
- string
The text, or an empty string for an empty cell or an address that is not valid.
InsertColumnsAsync(int, int)
Inserts empty columns on the sheet being shown. Cells after them move along, and formulas on every sheet follow them.
public Task InsertColumnsAsync(int column, int count = 1)
Parameters
Returns
- Task
A task that completes once the columns are in.
InsertRowsAsync(int, int)
Inserts empty rows on the sheet being shown. Cells below move down, and formulas on every sheet follow them.
public Task InsertRowsAsync(int row, int count = 1)
Parameters
Returns
- Task
A task that completes once the rows are in.
OnAfterRenderAsync(bool)
Method invoked after each time the component has been rendered interactively and the UI has finished updating (for example, after elements have been added to the browser DOM). Any ElementReference fields will be populated by the time this runs.
This method is not invoked during prerendering or server-side rendering, because those processes are not attached to any live browser DOM and are already complete before the DOM is updated.
Note that the component does not automatically re-render after the completion of any returned Task, because that would cause an infinite render loop.
protected override Task OnAfterRenderAsync(bool firstRender)
Parameters
firstRenderboolSet to
trueif this is the first time OnAfterRender(bool) has been invoked on this component instance; otherwisefalse.
Returns
Remarks
The OnAfterRender(bool) and OnAfterRenderAsync(bool) lifecycle methods
are useful for performing interop, or interacting with values received from @ref.
Use the firstRender parameter to ensure that initialization work is only performed
once.
OnParametersSet()
Invoked when the component's parameters have been set. Ensures that the component has a unique identifier by generating one if none is provided.
protected override void OnParametersSet()
Remarks
If the component's identifier is not set, a unique ID is generated based on the component's type name (with a 'Tw' prefix removed if present) and a GUID. This helps ensure that each component instance can be uniquely identified, which is useful for scenarios such as associating labels with form elements or targeting elements in client-side scripts.
PasteTextAsync(string?)
Pastes text from the clipboard at the active cell, called by twSpreadsheet.attach. Cells copied from this
spreadsheet keep their formulas, moved to their new place, and their formatting; other text is split into cells
at tabs and line breaks. A single value fills the whole selection.
[JSInvokable("PasteText")]
public Task PasteTextAsync(string? text)
Parameters
textstringThe pasted text.
Returns
- Task
A task that completes once the cells have changed.
PointerContextAsync(int, int)
Selects the cell under the pointer before the context menu opens, unless it is already in the selection,
called by twSpreadsheet.attach.
[JSInvokable("PointerContext")]
public Task PointerContextAsync(int row, int column)
Parameters
rowintThe zero based row, or -1 on a column heading.
columnintThe zero based column, or -1 on a row heading.
Returns
- Task
A task that completes once the selection has moved.
PointerDoubleClickAsync(int, int)
Starts editing a cell, called by twSpreadsheet.attach when it is double-clicked.
[JSInvokable("PointerDoubleClick")]
public Task PointerDoubleClickAsync(int row, int column)
Parameters
Returns
- Task
A task that completes once the cell is being edited.
PointerDownAsync(int, int, bool)
Handles a press of the primary pointer button on the grid, called by twSpreadsheet.attach. A row of -1 is a
column heading, a column of -1 is a row heading, and both -1 is the corner, which selects every cell. While a
formula is being typed, pressing a cell puts its reference into the formula instead.
[JSInvokable("PointerDown")]
public Task PointerDownAsync(int row, int column, bool shift)
Parameters
rowintThe zero based row, or -1.
columnintThe zero based column, or -1.
shiftboolWhether Shift was held, which extends the selection.
Returns
- Task
A task that completes once the selection has moved.
PointerExtendAsync(int, int)
Extends the selection while the pointer is dragged across the grid, called by twSpreadsheet.attach.
[JSInvokable("PointerExtend")]
public Task PointerExtendAsync(int row, int column)
Parameters
rowintThe zero based row under the pointer, or -1 over a column heading.
columnintThe zero based column under the pointer, or -1 over a row heading.
Returns
- Task
A task that completes once the selection has moved.
RedoAsync()
Redoes the last change that was undone.
public Task RedoAsync()
Returns
- Task
A task that completes once the change has been redone.
Refresh()
Works the formulas out again and redraws the grid, after the cells or the sheets were changed from code.
public void Refresh()
RemoveSheetAsync(int)
Removes a sheet, which can be undone. The last sheet cannot be removed. Formulas that referred to it show
#REF! until it is back.
public Task<bool> RemoveSheetAsync(int index)
Parameters
indexintThe index of the sheet.
Returns
RemoveTable(SheetTable)
Turns a table back into plain cells, keeping their contents, and shows every row its filters hid.
public bool RemoveTable(SheetTable table)
Parameters
tableSheetTableThe table, on the sheet being shown.
Returns
RenameSheetAsync(int, string)
Renames a sheet, and changes the formulas on every sheet that refer to it by name.
public Task<bool> RenameSheetAsync(int index, string name)
Parameters
Returns
- Task<bool>
true when the sheet was renamed; false when the name is blank, or already used by another sheet.
ResizeColumn(int, int)
Sets a column's width after the user dragged its heading's edge, called by twSpreadsheet.attach.
[JSInvokable("ResizeColumn")]
public void ResizeColumn(int column, int width)
Parameters
ResizeRow(int, int)
Sets a row's height after the user dragged its heading's edge, called by twSpreadsheet.attach.
[JSInvokable("ResizeRow")]
public void ResizeRow(int row, int height)
Parameters
SelectAsync(string)
Selects a cell or a range on the sheet being shown and scrolls it into view.
public Task<bool> SelectAsync(string range)
Parameters
rangestringA cell, such as
B3, or a range, such asB3:D8, whose first corner becomes the active cell.
Returns
SetCellAsync(string, string?, int?)
Sets what a cell holds, as if the user had typed it, so it can be undone and is reported through OnCellsChange.
public Task<bool> SetCellAsync(string address, string? value, int? sheetIndex = null)
Parameters
addressstringThe cell, such as
B3.valuestringA number, text or a formula starting with
=, or null to empty the cell.sheetIndexint?The index of the sheet, or null for the sheet being shown.
Returns
SetColumnWidth(string, int)
Sets the width of a column on the sheet being shown.
public bool SetColumnWidth(string column, int width)
Parameters
columnstringThe column's letters, such as
B.widthintThe width in pixels, kept between 24 and 1000.
Returns
SetFullScreenAsync(bool)
Turns full screen mode on or off, announces it and reports it through FullScreenChanged.
public Task SetFullScreenAsync(bool on)
Parameters
onboolWhether the spreadsheet should cover the window.
Returns
- Task
A task that completes once the mode has changed.
SetRowHeight(int, int)
Sets the height of a row on the sheet being shown.
public bool SetRowHeight(int row, int height)
Parameters
Returns
SetZoomAsync(int)
Sets the zoom level and reports it through ZoomChanged.
public Task SetZoomAsync(int zoom)
Parameters
zoomintThe zoom level in percent, kept between 25 and 400.
Returns
- Task
A task that completes once the zoom has changed.
SortAsync(bool)
Sorts the rows of the selection by the active cell's column. With a single cell selected, the block of filled
cells from A1 is sorted, leaving out a first row of headings. Empty cells go last either way, and
formulas move with their rows.
public Task SortAsync(bool descending = false)
Parameters
descendingboolWhether to sort largest first.
Returns
- Task
A task that completes once the rows are sorted.
UndoAsync()
Undoes the last change.
public Task UndoAsync()
Returns
- Task
A task that completes once the change has been undone.