Multi-Instance Spreadsheets

Run multiple spreadsheet instances in the same application

The SpreadsheetMultiInstanceProvider component allows you to render multiple spreadsheet instances within the same application while maintaining isolated state and preventing conflicts between instances.

Overview

By default, each spreadsheet instance shares global context through SpreadsheetProvider. When you need to render multiple spreadsheets simultaneously (e.g., side-by-side comparison, multi-document interface), you need to wrap them in SpreadsheetMultiInstanceProvider to ensure proper isolation.

Basic Usage

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

function MultiSpreadsheetApp() {
  return (
    <SpreadsheetMultiInstanceProvider>
      <SpreadsheetProvider>
        <SpreadsheetInstance instanceId="spreadsheet-1" />
      </SpreadsheetProvider>

      <SpreadsheetProvider>
        <SpreadsheetInstance instanceId="spreadsheet-2" />
      </SpreadsheetProvider>
    </SpreadsheetMultiInstanceProvider>
  );
}

function SpreadsheetInstance({ instanceId }: { instanceId: string }) {
  const {
    activeCell,
    activeSheetId,
    selections,
    // ... other state
  } = useSpreadsheetState({
    // configuration
  });

  return (
    <CanvasGrid
      instanceId={instanceId}
      sheetId={activeSheetId}
      activeCell={activeCell}
      selections={selections}
      // ... other props
    />
  );
}

Instance ID

Each CanvasGrid instance should have a unique instanceId prop when rendered within SpreadsheetMultiInstanceProvider:

Why Instance IDs Matter

Instance IDs ensure that:

  • Keyboard events are routed to the correct spreadsheet

  • Editor state is isolated between instances

  • Context menus and dialogs appear in the correct location

  • Formula evaluation doesn't cross instance boundaries

Use Cases

Side-by-Side Comparison

Compare two spreadsheets side by side:

Multi-Document Interface

Create a tabbed or windowed interface with multiple spreadsheet documents:

Master-Detail View

Display a master spreadsheet with a detail view:

Complete Example

Best Practices

  1. Always Use Unique Instance IDs: Ensure each spreadsheet has a unique instanceId to prevent conflicts

  2. Wrap Each Instance: Each spreadsheet should have its own SpreadsheetProvider wrapper

  3. Isolate State: Use separate state management for each spreadsheet instance

  4. Performance Considerations: Be mindful of rendering multiple large spreadsheets simultaneously - consider lazy loading or virtualization

  5. Memory Management: Clean up instances when they're no longer needed to free up resources

Limitations

  • Each instance maintains its own calculation engine and state

  • Cross-instance formulas are not supported (formulas cannot reference cells from other instances)

  • Each instance requires separate data management

Performance Tips

When rendering multiple instances:

Troubleshooting

Keyboard Events Not Working

If keyboard events aren't working properly, ensure:

  • Each CanvasGrid has a unique instanceId

  • The SpreadsheetMultiInstanceProvider wraps all instances

  • Only one spreadsheet has focus at a time

State Conflicts

If you experience state conflicts between instances:

  • Verify each instance has its own SpreadsheetProvider

  • Check that state management is properly isolated

  • Ensure instance IDs are unique and don't change during component lifecycle

Last updated

Was this helpful?