0.3.4 • Published 2 years ago

virtual-spreadsheet v0.3.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

virtual-spreadsheet

A lightweight, virtualized spreadsheet capable of rendering millions of cells of data.

NPM JavaScript Style Guide

Install

npm install virtual-spreadsheet

Usage

import React, { Component } from 'react'
import {Spreadsheet} from 'virtual-spreadsheet'

function SpreadsheetExample () {
  /*
   *  The csv prop passed to spreadsheet should use commas to delimit columns and
   *  newline to delimit rows. (TODO: add custom delimiters)
   */
  const [csv, setCSV] = useState("hi,mom\ni'm,on tv!")

  /* Used to demonstrate the CellSelector. Not necessary for operation */
  const [selectedData, setData] = useState([['']])

  const getSelectedData = (coordinate, cells) => {
    /*
     * CellSelector retrieves the row, column or cell selected in a 2-d array
     * Can be used for any operations you need to perform when data is selected
     */
    setData(CellSelector(coordinate, cells))
  }

  /* Do any state updates you need by passing a function to the onUpdate prop */
  const processUpdate = (coordinate, newVal, cells) => {
    cells[coordinate.row][coordinate.col] = newVal
    setData(CellSelector(coordinate, cells))
  }

  fetch('/big_sample.csv')
    .then(csv => csv.text())
    .then(txt => setCSV(txt))

  return (
    <React.Fragment>
      <Spreadsheet
        csv={csv}
        height='70vh'
        width='95vw'

        notARealProp='You can attach some extra event handlers if needed'
        onCellSelect={getSelectedData}
        onCellUpdate={processUpdate}

        notARealProp2='Cell size and font cannot be set with cellStyle'
        cellWidth='auto'
        cellHeight={25 /* number */}
        cellFont='18px Arial'

        notARealProp3='Style the cells using the props below'
        rowHeaderStyle={{ color: '#ffffff', background: '#0077cc' }}
        columnHeaderStyle={{ color: '#ffffff', background: '#0077cc' }}
        cellStyle={{ color: '#000000', background: '#ffffff' }}
        activeCellStyle={{ color: '#ffffff', background: '#33aaff' }}
      />

      {JSON.stringify(selectedData) /* Demonstration of CellSelector data */}
    </React.Fragment>
  )
}

Props

NameTypeDefaultRequiredDescription
csvstringfalseShould use commas to delimit columns and newline to delimit rows.Values cannot currently have commas (TODO: add custom delimiters, improve value parsing)
cells(string | number) [][]falseA 2-d array of values to be entered into the table
firstRowHeadersbooleanfalsefalseIf true, the first row will be used as column headers instead of data
firstColumnHeadersbooleanfalsefalseIf true, the first column will be used as row headers instead of data
rowFilter(row, rowIndex, columnIndices) => booleanfalseApplied to rows in the table. Will filter out the row if boolean is false.If using the draggableColumns prop, use the index = columnIndicesUIColIndex to index the row inside of yourrowFilter function. This will ensure the correct column is used in the filter criteria if user has moved the columns.If you are using the rowFilter, you should also keep your source data in sync with changes made in the Spreadsheet.If you don't keep your source data in sync with the Spreadsheet, changeswill be lost when you apply a new rowFilter.@param row@param rowIndex@param columnIndices
draggableColumnsbooleanfalsefalseIf true, the column headers can be dragged to re-order them
sortableColumnsbooleanfalsefalseIf true, the columns will be sorted when they are clicked while already selected.They will first be sorted in normal (alphabetical or numerical order), then reverse,then back to its default order. You can override the 'normal' sort function withthe sortFunction prop.
sortFunction(val1, val2) => numberfalseWill be used as the 'normal' sort for your columns (if sortableColumns is enabled).@param val1@param val2
fixedColumnCountnumberfalseThe number of left-side columns that will remain visible when horizontally scrolling
fixedRowCountnumberfalseThe number of top-side rows that will remain visible when vertically scrolling
widthstring | numberfalseSpreadsheet will dynamically size to its container by default. If the container's size is 0 or undefined, the width will default to 900px
heightstring | numberfalseSpreadsheet will dynamically size to its container by default. If the container's size is 0 or undefined, the height will default to 400px
onCellSelect(coordinate?, cells?) => voidfalseCalled when a cell, row, or column is clicked. You can pass the coordinate and cells to the CellSelector function to get a 2-D array with the selected data@param coordinate@param cells
onCellUpdate(coordinate? , newValue?, cells?) => voidfalseCalled when a cell value is changed. You can do any state updates you need in this method@param coordinate: {row:number, col:number}@param newValue: string@param cells: (stringnumber)
readOnlybooleanfalsefalseIf present with no value or set to true, the data cells cannot be edited.
cellWidthnumber | 'auto' | 'auto-deep' | 'auto-${number}'false'auto' calculates the width required for the each column. Limited to 1000 rows by default.'auto-' followed by a number overrides the depth limit for the width calculation(e.g. 'auto-5000').'auto-deep' will use every row in its width calculation.Override cautiously as too high of depth limit can cause the page to go unresponsive for 100K+ rows
cellWidthModifer(calculatedWidth, isActive?, isHighlighted?, isRightBoundary?, isBottomBoundary?)=> number | stringfalseUsed to modify the width calculated by auto-width if it miscalculates where the border should be@param calculatedWidth@param isActive@param isHighlighted@param isRightBoundary@param isBottomBoundary
cellHeightModifer(calculatedHeight, isActive?, isHighlighted?, isRightBoundary?, isBottomBoundary?)=> number | stringfalseUsed to modify the width calculated by auto-width if it miscalculates where the border should be@param calculatedHeight@param isActive@param isHighlighted@param isRightBoundary@param isBottomBoundary
cellHeightnumberfalseHeight of the data and header cells
cellFontstringfalseMust include size and typeset
rowHeaderStyleCSSPropertiesfalseStyle the row number column. Avoid using height, width, font, and position.
columnHeaderStyleCSSPropertiesfalseStyle the column number row. Avoid using height, width, font, and position.
cellStyleCSSPropertiesfalseStyle spreadsheet's data cells. Avoid using height, width, font, and position.
activeCellStyleCSSPropertiesfalseStyle data cells (not headers) when hovered or clicked on. Avoid using height, width, font, and position.
highlightedCellStyleCSSPropertiesfalseStyle data cells (not headers) when its row or column header is clicked on. Avoid using height, width, font, and position.
0.3.2-npm

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.1.9

2 years ago

0.3.2

2 years ago

0.2.3

2 years ago

0.3.1

2 years ago

0.2.2

2 years ago

0.3.4

2 years ago

0.2.5

2 years ago

0.3.3

2 years ago

0.2.4

2 years ago

0.1.8

2 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago