Spreadsheet state
Use hooks to manage spreadsheet state
useSpreadsheetState hook
yarn add @rowsncolumns/spreadsheet-statenpm install @rowsncolumns/spreadsheet-stateAdding the hook
import React, { useState } from "react"
import {
useSpreadsheetState,
SheeData,
CellXfs
} from "@rowsncolumns/spreadsheet-state"
import { functionDescriptions, functions } from "@rowsncolumns/functions";
import {
SpreadsheetProvider,
CanvasGrid,
CellData,
Sheet,
EmbeddedChart,
EmbeddedObject,
TableView,
PivotTable
} from "@rowsncolumns/spreadsheet"
const MySpreadSheet = () => {
const [sheets, onChangeSheets] = useState<Sheet[]>([]);
const [sheetData, onChangeSheetData] = useState<SheetData<CellData>>({});
const [cellXfs, onChangeCellXfs] = useState<CellXfs | null | undefined>(new Map());
const [scale, onChangeScale] = useState(1);
const [charts, onChangeCharts] = useState<EmbeddedChart[]>([]);
const [embeds, onChangeEmbeds] = useState<EmbeddedObject[]>([]);
const [tables, onChangeTables] = useState<TableView[]>([]);
const [pivotTables, onChangePivotTables] = useState<PivotTable[]>([]);
const [conditionalFormats, onChangeConditionalFormats] = useState<
ConditionalFormatRule[]
>([]);
const [protectedRanges, onChangeProtectedRanges] = useState<ProtectedRange[]>(
[]
);
const {
activeCell,
activeSheetId,
selections,
rowCount,
columnCount,
frozenColumnCount,
frozenRowCount,
spreadsheetColors,
spreadsheetTheme,
rowMetadata,
columnMetadata,
merges,
canRedo,
redo,
canUndo,
undo,
getCellData,
getSheetName,
getSheetId,
getUserEnteredFormat,
onRequestResultPreview,
onChangeActiveCell,
onChangeActiveSheet,
onSelectNextSheet,
onSelectPreviousSheet,
onChangeSelections,
onChange,
onDelete,
onChangeFormatting,
onClearFormatting,
onUnMergeCells,
onMergeCells,
onResize,
onChangeBorder,
onChangeDecimals,
onChangeSheetTabColor,
onRenameSheet,
onDeleteSheet,
onShowSheet,
onHideSheet,
onProtectSheet,
onUnProtectSheet,
onMoveSheet,
onCreateNewSheet,
onDuplicateSheet,
onHideColumn,
onShowColumn,
onHideRow,
onShowRow,
onFill,
onFillRange,
onMoveChart,
onResizeChart,
onMoveEmbed,
onResizeEmbed,
onDeleteRow,
onDeleteColumn,
onDeleteCellsShiftUp,
onDeleteCellsShiftLeft,
onInsertCellsShiftRight,
onInsertCellsShiftDown,
onInsertRow,
onInsertColumn,
onMoveColumns,
onMoveRows,
onMoveSelection,
onSortColumn,
onSortRange,
onFilterRange,
onCopy,
onPaste,
cellXfsRegistry,
getEffectiveFormat
} = useSpreadsheetState({
sheets,
sheetData,
tables,
pivotTables,
functions,
namedRanges,
theme,
colorMode,
conditionalFormats,
cellXfs,
onChangeSheets,
onChangeSheetData,
onChangeEmbeds,
onChangeCharts,
onChangeTables,
onChangePivotTables,
onChangeNamedRanges,
onChangeTheme,
onChangeCellXfs,
onChangeHistory(patches) {
onBroadcastPatch(patches);
},
onChangeProtectedRanges,
onChangeConditionalFormats,
})
return (
<CanvasGrid
{...spreadsheetColors}
stickyEditor={true}
scale={scale}
getEffectiveFormat={getEffectiveFormat}
conditionalFormats={conditionalFormats}
sheetId={activeSheetId}
rowCount={rowCount}
columnCount={columnCount}
frozenColumnCount={frozenColumnCount}
frozenRowCount={frozenRowCount}
rowMetadata={rowMetadata}
columnMetadata={columnMetadata}
activeCell={activeCell}
selections={selections}
theme={spreadsheetTheme}
merges={merges}
charts={charts}
embeds={embeds}
tables={tables}
protectedRanges={protectedRanges}
onChangeActiveCell={onChangeActiveCell}
onChangeSelections={onChangeSelections}
onChangeActiveSheet={onChangeActiveSheet}
onRequestResultPreview={onRequestResultPreview}
onSelectNextSheet={onSelectNextSheet}
onSelectPreviousSheet={onSelectPreviousSheet}
onChangeFormatting={onChangeFormatting}
onHideColumn={onHideColumn}
onShowColumn={onShowColumn}
onHideRow={onHideRow}
onShowRow={onShowRow}
onDelete={onDelete}
onClearContents={onDelete}
onFill={onFill}
onFillRange={onFillRange}
onResize={onResize}
onMoveChart={onMoveChart}
onMoveEmbed={onMoveEmbed}
onResizeChart={onResizeChart}
onResizeEmbed={onResizeEmbed}
onDeleteRow={onDeleteRow}
onDeleteColumn={onDeleteColumn}
onDeleteCellsShiftUp={onDeleteCellsShiftUp}
onDeleteCellsShiftLeft={onDeleteCellsShiftLeft}
onInsertCellsShiftRight={onInsertCellsShiftRight}
onInsertCellsShiftDown={onInsertCellsShiftDown}
onInsertRow={onInsertRow}
onInsertColumn={onInsertColumn}
onMoveColumns={onMoveColumns}
onMoveRows={onMoveRows}
onMoveSelection={onMoveSelection}
onCreateNewSheet={onCreateNewSheet}
functionDescriptions={functionDescriptions}
getSheetName={getSheetName}
getSheetId={getSheetId}
getCellData={getCellData}
onChange={onChange}
onUndo={undo}
onRedo={redo}
onSortColumn={onSortColumn}
onSortRange={onSortRange}
onFilterRange={onFilterRange}
onClearFormatting={onClearFormatting}
onCopy={onCopy}
onPaste={onPaste}
/>
)
}
const App = () => {
return (
<SpreadsheetProvider>
<MySpreadSheet />
</SpreadsheetProvider>
)
}Creating initial row data from primitives
Using CellXfs for Shared Cell Formatting
Setting up cellXfs
Using cellXfs in useSpreadsheetState
Applying cellXfs to cells
Use getEffectiveFormat to pick up cellXfs format
getEffectiveFormat to pick up cellXfs formatLast updated