Export canvas as image

Export the entire visible canvas or part of the canvas as an image

The spreadsheet provides functionality to export regions of the canvas as images, useful for generating reports, sharing data visualizations, or creating documentation.

Exporting a Sheet Region

You can export specific regions of your spreadsheet as PNG or JPEG images using the exportRegion function from the useSpreadsheet hook.

Basic Usage

import { SpreadsheetProvider, CanvasGrid, useSpreadsheet } from "@rowsncolumns/spreadsheet";

const MySpreadsheet = () => {
  const { exportRegion } = useSpreadsheet()

  const handleExport = () => {
    exportRegion?.(
      {
        startRowIndex: 1,
        endRowIndex: 10,
        startColumnIndex: 1,
        endColumnIndex: 5,
      },
      "my-spreadsheet-export",  // filename (without extension)
      "image/png"                // MIME type
    );
  };
  
  return (
    <>
      <button onClick={handleExport}>
        Export Region as Image
      </button>
      <CanvasGrid />
    </>
  );
};

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

Export Options

Image Format

You can export in two formats:

  • PNG (default): "image/png" - Lossless, supports transparency

  • JPEG: "image/jpeg" - Compressed, smaller file size

Range Selection

The range object specifies which cells to include in the export:

Export Current Selection

You can export the currently selected range:

Export with Custom Filename

Generate dynamic filenames based on date, sheet name, or other criteria:

Exporting Entire Canvas

While there's no direct "export entire canvas" function, you can export the entire visible sheet by specifying the full range:

Complete Example

Use Cases

Report Generation

Export specific data ranges for inclusion in reports:

Data Visualization Sharing

Export charts and formatted data for sharing:

Documentation

Create screenshots for documentation or tutorials:

Limitations

  • Only the visible/rendered portion of the canvas can be exported

  • Hidden rows and columns are not included in the export

  • The export captures the current visual state (colors, formatting, etc.)

  • Very large ranges may take longer to export

  • Export quality depends on the canvas resolution

Best Practices

  1. Use descriptive filenames: Include dates, sheet names, or identifiers

  2. Choose appropriate format: Use PNG for detailed data, JPEG for photographs

  3. Limit range size: Export only necessary cells for better performance

  4. Provide user feedback: Show loading states during export

  5. Validate ranges: Ensure row/column indices are within bounds

Browser Compatibility

The export functionality works in all modern browsers that support:

  • Canvas API

  • Blob API

  • Download attribute on anchor elements

For older browsers, consider providing a fallback or polyfill.

Last updated

Was this helpful?