Rows n’ Columns Docs
Visit HomepagePricing
  • Introduction
  • License
  • Demos
  • Getting started
    • Installation
    • Spreadsheet state
    • Headless UI
    • Imperative Spreadsheet API
    • Examples
  • ⚙️Configuration
    • Features
      • Data validation
      • Formula evaluation
      • Real-time data
      • Cell editors
      • Cell renderer
      • Structured Cell Renderer
      • Theming
      • Styling
      • Context menu
      • Localisation
      • Undo/Redo
      • Conditional formatting
      • Named ranges
      • Structured references
        • Schema based tables and columns
        • Calculated columns
      • Basic filter or Excel AutoFilter
      • Charts
      • Embedded content
      • Calculate on-demand
      • Drag and Drop
      • Pivoting and Grouping (Coming soon)
      • Tokenizer
      • Lazy loading/Infinite scrolling
      • OpenAI/Chat GPT Integration
      • Search
      • Formula protection
      • Autofill
      • Export canvas as image
    • Components
      • Canvas Grid
      • Toolbar
      • Sheet Tabs
      • Sheet Switcher
      • Sheet Status
      • Range Selector
      • Formula Input
      • Selection Input
      • SheetSearch
      • NamedRangeEditor
      • DeleteSheetConfirmation
      • TableEditor
      • Cell Format Editor
      • Conditional Format Editor
      • Data Validation Editor
      • Insert Link Editor
      • Insert Image Editor
    • API
      • Cell Data
      • Sheets
      • SpreadsheetProvider
      • useSpreadsheet
      • Modules
      • SheetCell
    • Using Spreadsheet with NextJS
    • Keyboard shortcuts
  • Collaboration
    • Real time collaboration
    • Yjs Collaboration
    • Supabase realtime Collaboration
  • Charts
    • Charts
    • Custom charts
  • Excel and Google sheets
    • CSV
    • Excel
    • Google sheets (Coming soon)
  • Functions
    • Named functions
    • Array formulas
  • Data persistence
    • Server side data persistence
    • React Query integration
  • Specifications
    • Browser support
    • Third party licenses
  • Support
    • Contact support
    • Report bugs
    • Feature requests
Powered by GitBook
On this page

Was this helpful?

  1. Data persistence

Server side data persistence

Completely agnostic to how you manage the back-end

There are various ways to persist data in the back-end, since you can use the Spreasheet in both headless and stateful way (using useSpreasheetState hook, or your own custom hook) .

Persisting using useSpreadsheetState

Spreadsheet gives you granular callbacks for every state change triggered by the user. You can call a REST or websocket API to trigger an API call to the back-end

import React, { useState } from "react";
import {
  CellData,
  EmbeddedChart,
  EmbeddedObject,
  Sheet,
  CanvasGrid,
  SpreadsheetProvider,
  TableView,
} from "@rowsncolumns/spreadsheet";
import {
  SheetData,
  useSpreadsheetState,
} from "@rowsncolumns/spreadsheet-state";
import { functions } from "@rowsncolumns/functions";

export const Editor = () => {
  const App = () => {
    const [sheets, onChangeSheets] = useState<Sheet[]>([]);
    const [sheetData, onChangeSheetData] = useState<SheetData<CellData>>({});
    const [scale, onChangeScale] = useState(1);
    const [charts, onChangeCharts] = useState<EmbeddedChart[]>([]);
    const [embeds, onChangeEmbeds] = useState<EmbeddedObject[]>([]);
    const [tables, onChangeTables] = useState<TableView[]>([]);

    const {
      onChange,
      onInsertRow,
    } = useSpreadsheetState({
      sheets,
      sheetData,
      tables,
      functions,
      onChangeSheets,
      onChangeSheetData,
      onChangeEmbeds,
      onChangeCharts,
      onChangeTables,
      initialColorMode: "light",
    });
    return (
      <CanvasGrid
        sheetId={1}
        rowCount={100}
        columnCount={100}
        onChange={(sheetId, cell, value) => {
          // Persit data to server
          fetch("/post", {
            method: "POST",
            body: JSON.stringify({ action: "SET", value, cell, sheetId }),
          });

          // Optimistically update local UI state
          onChange(sheetId, cell, value);
        }}
        onInsertRow={(sheetId, rowIndex) => {
          // Update server
          fetch("/post", {
            method: "POST",
            body: JSON.stringify({ action: "INSERT_ROW", sheetId, rowIndex }),
          });
          // Optimistic update
          onInsertRow(sheetId, rowIndex);
        }}
      />
    );
  };

  return (
    <SpreadsheetProvider>
      <App />
    </SpreadsheetProvider>
  );
};
PreviousArray formulasNextReact Query integration

Last updated 11 months ago

Was this helpful?