FortuneSheet Documentation

repository·master·Indexed 24 days ago

https://github.com/ruilisi/fortune-sheet

A TypeScript-based JavaScript spreadsheet library providing Excel-like functionality, including formatting, formulas, and cell manipulation. Optimized for React and Vue, it offers features such as conditional formatting, data validation, and support for Math, Text, Date, Financial, Logical, Lookup, and Dynamic Array formulas. The library includes @fortune-sheet/core and @fortune-sheet/react packages, and provides a Workbook API for programmatic control over cell values, formats, and sheet management.

Tokens
25.1K
Snippets
47
Records
192
Agent score
86%

What's inside FortuneSheet

  1. Overview of FortuneSheet

    master
    FortuneSheet is an open-source spreadsheet library designed to replace Excel functionality with a large number of commonly used spreadsheet functions. It features simple configuration to allow developers to get started with minimal setup.
  2. Overview of FortuneSheet features

    master

    FortuneSheet is a JavaScript spreadsheet library providing features similar to Excel and Google Sheets, including:

    • Formatting: Styling, conditional formatting, text alignment/rotation, and support for various data types (currency, percentages, dates, and custom formats).
    • Cells: Drag-and-drop moving, fill handle (arithmetic/geometric sequences), multiple selection, find and replace, merge cells, and data validation (checkbox, dropdown, datePicker).
    • Rows & Columns: Hide, insert, delete, and freeze rows/columns; split text to columns.
    • Operations: Undo/Redo, Copy/Paste/Cut (with Excel compatibility), and Format Painter.
    • Formulas & Functions: Built-in support for Math, Text, Date, Financial, Logical, Lookup, and Dynamic Array formulas (e.g., SUMIFS, VLOOKUP, SORT, FILTER).
    • Tables: Filtering (color, numerical, date, text) and multi-field sorting.
    • Objects: Insert pictures (JPG, PNG, SVG) and take screenshots of selections.
  3. Configure Filter range and conditions

    master

    Filtering is handled via two properties: filter_select defines the range of the filter, and filter defines the specific conditions for each column within that range.

    Filter Conditions (caljs):

    • cellnull: Is empty
    • cellnonull: Is not empty
    • textinclude: Text contains
    • textnotinclude: Text does not contain
    • textstart: Text starts with
    • textend: Text ends with
    • textequal: Text is exactly
    • dateequal: Date is
    • datelessthan: Date is before
    • datemorethan: Date is after
    • morethan: Greater than
    • moreequalthan: Greater than or equal to
    • lessthan: Less than
    • lessequalthan: Less than or equal to
    • equal: Is equal to
    • noequal: Is not equal to
    • include: Is between (requires value1 and value2)
    • noinclude: Is not between (requires value1 and value2)
    // 1. Define the range
    "filter_select": {
        "row": [ 2, 6 ],
        "column": [ 1, 3 ]
    }
    
    // 2. Define conditions for column index 1 (key '0')
    "filter": {
        "0": {
            "caljs": {
                "value": "cellnull",
                "text": "Is empty",
                "type": "0"
            },
            "rowhidden": { "3": 0, "4": 0 },
            "optionstate": true,
            "cindex": 1,
            "str": 2,
            "edr": 6,
            "stc": 1,
            "edc": 3
        }
    }
  4. Initialize FormulaParser in Node.js or Browser

    master

    To use the parser, instantiate the Parser class.

    Node.js:

    var FormulaParser = require("hot-formula-parser").Parser;
    var parser = new FormulaParser();

    Browser:

    <script src="/node_modules/hot-formula-parser/dist/formula-parser.min.js"></script>
    <script>
      var parser = new formulaParser.Parser();
    </script>
  5. Merge cells in FortuneSheet

    master

    To merge cells, you must update both the cell objects and the sheet configuration.

    1. Update Cell Objects: Set the mc attribute in the main (top-left) cell and in all other cells within the range. The mc object requires r (row), c (column), rs (rowspan), and cs (colspan).
    2. Update Config: Set config.merge using a key format of "r_c" (e.g., "0_0") pointing to the same mc settings.

    Example to merge A1:B2:

    Cell Data:

    [
        [
            {
                "m": "merge cell",
                "mc": { "r": 0, "c": 0, "rs": 2, "cs": 2 }
            },
            { "mc": { "r": 0, "c": 0 } }
        ],
        [
            { "mc": { "r": 0, "c": 0 } },
            { "mc": { "r": 0, "c": 0 } }
        ]
    ]

    Config:

    {
        "0_0": {
            "r": 0,
            "c": 0,
            "rs": 2,
            "cs": 2
        }
    }
  6. Apply cell borders using borderInfo

    master

    Borders are managed via the config.borderInfo property rather than the individual cell objects. To apply a border to a specific range, define the borderType, color, style, and the range (rows and columns).

    Example: Setting a red, solid border for cell A1:

    {
        "rangeType": "range",
        "borderType": "border-all",
        "color": "#000",
        "style": "1",
        "range": [
            {
                "row": [ 0, 0 ],
                "column": [0, 0]
            }
        ]
    }
  7. Implement Backend Storage and Collaboration using Op

    master

    FortuneSheet supports online collaboration and backend synchronization via an onOp callback. This callback emits a list of Op objects whenever a user performs an action. Each Op describes the transformation required to move from the current state to the new state.

    Example of an Op representing a cell formatting change (e.g., setting cell A2 to bold):

    [
        {
            "op": "replace",
            "index": "0",
            "path": ["data", 1, 0, "bl"],
            "value": 1
        }
    ]

    These operations are useful for updating backend databases and synchronizing state across multiple clients.

  8. Migrate data from Luckysheet to FortuneSheet

    master

    FortuneSheet is mostly compatible with Luckysheet's data structure, but requires the following naming updates:

    1. Change sheet.index to sheet.id
    2. Change sheet.calcChain[].id to sheet.calcChain[].id (Note: The README implies a structural change or specific mapping for calcChain IDs).
  9. Render a FortuneSheet workbook in React

    master

    To render a spreadsheet, ensure your container (e.g., #root) has a defined width and height (setting them to auto may prevent the table from appearing). Import the Workbook component and its associated CSS, then pass a data array containing sheet objects.

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Workbook } from "@fortune-sheet/react";
    import "@fortune-sheet/react/dist/index.css"
    
    ReactDOM.render(
      <Workbook data={[{ name: "Sheet1" }]} />,
      document.getElementById('root')
    );