Hooks

Utility hooks for advanced spreadsheet functionality

The spreadsheet provides several utility hooks for common operations beyond the main useSpreadsheetState hook.

useNavigateToSheetRange

Navigate to a specific cell or range in a sheet, optionally with a visual flash effect.

Basic Usage

import { useNavigateToSheetRange } from "@rowsncolumns/spreadsheet";
import { useSpreadsheetState } from "@rowsncolumns/spreadsheet-state";

function MySpreadsheet() {
  const navigateToSheetRange = useNavigateToSheetRange();

  const handleGoToRange = () => {
    navigateToSheetRange?.({
      sheetId: 2,
      startRowIndex: 10,
      endRowIndex: 15,
      startColumnIndex: 5,
      endColumnIndex: 8,
    });
  };

  return (
    <button onClick={handleGoToRange}>
      Go to Sheet 2, Range F11:I16
    </button>
  );
}

Parameters

Features

  • Sheet switching: Automatically switches to the target sheet

  • Cell positioning: Sets active cell to the top-left of the range

  • Scrolling: Scrolls the range into view

  • Visual feedback: Optional flash effect to highlight the range

Example with Custom Flash

Use Cases

  • Jump to errors: Navigate to cells with validation errors

  • Go to formulas: Jump to cells referenced in formulas

  • Search results: Navigate to search result cells

  • Links: Implement cell links to other sheet locations

  • Named ranges: Jump to named range locations

useLoadingIndicator

Display a loading indicator during async operations.

Basic Usage

API

The hook returns a tuple:

  • showLoader(): Shows the loading indicator

  • hideLoader(): Hides the loading indicator

Example with Multiple Operations

Component

The LoadingIndicator component must be rendered in your app (typically at the root level):

Use Cases

  • Data loading: Show loader while fetching data from server

  • File import: Display during Excel/CSV import operations

  • Calculations: Show during long-running calculations

  • Export operations: Display while generating exports

useSpreadsheetApi

Access the imperative API for programmatic control of the spreadsheet.

Basic Usage

See Imperative Spreadsheet API for complete documentation.

Complete Example

Hook Dependencies

useNavigateToSheetRange

Requires SpreadsheetProvider context:

useLoadingIndicator

Requires LoadingIndicator component to be rendered:

useSpreadsheetApi

Requires SpreadsheetProvider and active spreadsheet state:

Best Practices

useNavigateToSheetRange

  1. User feedback: Always provide visual feedback when navigating (use flash effect)

  2. Bounds checking: Ensure the range exists before navigating

  3. Sheet existence: Verify the target sheet exists

useLoadingIndicator

  1. Always hide: Use try-finally to ensure hideLoader is called

  2. Error handling: Show errors to users if operations fail

  3. Timeout: Consider adding timeouts for long operations

  4. Multiple operations: Use separate show/hide pairs for different operations

useSpreadsheetApi

  1. Null checking: Always use optional chaining (api?.)

  2. State management: Prefer declarative state over imperative API when possible

  3. Batch operations: Use batch methods for multiple changes

TypeScript Support

All hooks are fully typed:

Troubleshooting

useNavigateToSheetRange not working

Ensure you're inside SpreadsheetProvider:

LoadingIndicator not showing

Verify the component is rendered:

useSpreadsheetApi returns null

The API is only available after the spreadsheet is mounted:

Last updated

Was this helpful?