Data validation

Validate and show errors to users if they enter invalid data

Validation rules can be added as part of your cellData. Below is an example of using getDataValidation with CanvasGrid to dynamically validate cells.

import React, { useState } from "react";
import { SpreadsheetProvider, CanvasGrid } from "@rowsncolumns/spreadsheet";
import { useSpreadsheetState } from "@rowsncolumns/spreadsheet-state";

const MySpreadsheet = () => {
  const [dataValidations, onChangeDataValidations] = useState([
    {
      id: "validation1",
      condition: {
        type: "ONE_OF_LIST",
        values: [
          { userEnteredValue: "Singapore" },
          { userEnteredValue: "USA" },
          { userEnteredValue: "UK" },
        ],
      },
    },
  ]);

  const { getDataValidation } = useSpreadsheetState({
    dataValidations,
    onChangeDataValidations,
  });

  return (
    <CanvasGrid
      getCellData={(sheetId, rowIndex, columnIndex) => {
        if (rowIndex === 2 && columnIndex === 2) {
          return {
            dataValidation: "validation1", // Reference the validation ID
          };
        }
      }}
      getDataValidation={getDataValidation} // Pass the getDataValidation function to CanvasGrid
    />
  );
};

const App = () => (
  <SpreadsheetProvider>
    <MySpreadsheet />
  </SpreadsheetProvider>
);

The following validation types are supported:

Data Validation Editor

Last updated

Was this helpful?