!!!###!!!title=sheet getting started——VisActor/VTable tutorial documents!!!###!!!!!!###!!!description=This chapter introduces how to quickly get started with the VTable-Sheet component, including installation, basic configuration, and creating your first spreadsheet.!!!###!!!

Getting Started

This chapter introduces how to quickly get started with the VTable-Sheet component, including installation, basic configuration, and creating your first spreadsheet.

Installation

Install the VTable-Sheet component using npm or yarn:

# Using npm
npm install @visactor/vtable-sheet

# Using yarn
yarn add @visactor/vtable-sheet

Basic Usage

Importing the Component

// ES module import
import { VTableSheet } from '@visactor/vtable-sheet';

// Or using CommonJS
const { VTableSheet } = require('@visactor/vtable-sheet');

cdn import

If only the vtable-sheet umd package is imported, the underlying vrender environment restriction cannot work properly! ! !

Both the vrender and vtable umd packages need to be imported.

The vtable umd package cannot also use the unpkg platform, the user needs to fork the vtable source code first, and then package a vtable umd package themselves! ! !

Before packaging, it is necessary to release the commented code about vrender in the packaging configuration. Run the command cd packages/vtable && rushx build to get the vtable.js file in the dist directory.

The specific reference method is as follows:
<script src="https://unpkg.com/@visactor/vrender@latest/dist/index.js"></script>
<script src="vtable.js"></script>
<script src="https://unpkg.com/@visactor/vtable-sheet@latest/dist/vtable-sheet.js"></script>

If you also need to import the umd package of the vtable-plugins plugin package, you can add:

<script src="https://unpkg.com/@visactor/vrender@latest/dist/index.js"></script>
<script src="vtable.js"></script>
<script src="https://unpkg.com/@visactor/vtable-plugins@latest/dist/vtable-plugins.js"></script>
<script src="https://unpkg.com/@visactor/vtable-sheet@latest/dist/vtable-sheet.js"></script>

Creating a Basic Table

Here's an example of creating a simple table:

// Create a table instance
const sheetInstance = new VTableSheet(document.getElementById('container'), {
  showFormulaBar: true,  // Show formula bar
  showSheetTab: true,    // Show sheet tabs at the bottom
  defaultRowHeight: 25,  // Default row height
  defaultColWidth: 100,  // Default column width
  sheets: [
    {
      sheetKey: 'sheet1',   // Unique sheet identifier
      sheetTitle: 'Sheet 1',  // Display name for the sheet
      columns: [            // Column definitions
        { title: 'Name', width: 100 },
        { title: 'Age', width: 80 },
        { title: 'Department', width: 120 }
      ],
      data: [               // Table data
        ['John', 28, 'Engineering'],
        ['Lisa', 32, 'Marketing'],
        ['Mike', 25, 'HR']
      ],
      active: true          // Set as the currently active sheet
    }
  ]
});

Configuration Options

The VTableSheet component supports a rich set of configuration options:

Top-level Configuration

OptionTypeDefaultDescription
showFormulaBarbooleantrueWhether to display the formula bar
showSheetTabbooleantrueWhether to display sheet tabs at the bottom
defaultRowHeightnumber25Default row height
defaultColWidthnumber100Default column width
sheetsISheetDefine[][]Array of sheet definitions
themeITheme-Table theme configuration
VTablePluginModulesArray[]Plugin module configuration. You can configure VTable-supported plugins for VTableSheet or disable some built-in plugins. For plugin configuration options, refer to VTable-Plugins
mainMenuIMainMenu-Main menu configuration
undoRedo{ show?: boolean }{ show: true }Undo/redo UI configuration
dragOrderObject-Drag column order and row order configuration, if configured in ISheetDefine, this configuration will be ignored

Worksheet Configuration (ISheetDefine)

OptionTypeDefaultDescription
sheetKeystring-Unique worksheet identifier
sheetTitlestring-Display name for the worksheet
columnsIColumnDefine[][]Column definition array. If columns are not configured, the actual titles are in the data. You can use firstRowAsHeader to use the first row as the header. If you don't want the first row as the header, you can set showHeader to false.
dataany[][][]Table data, currently only supports two-dimensional arrays, not JSON format data
activebooleanfalseWhether this is the currently active worksheet
showHeaderbooleantrueWhether to display the header
firstRowAsHeaderbooleanfalseWhether to use the first row as the header
filterboolean | objectfalseWhether to enable filtering
columnCountnumber-Number of columns (used when columns are not specified)
rowCountnumber-Number of rows
frozenRowCountnumber0Number of frozen rows
frozenColCountnumber0Number of frozen columns
cellMergeICellMerge[][]Cell merge configuration
dragOrderObject-Drag column order and row order configuration
columnWidthConfigObject[][]Column width configuration
rowHeightConfigObject[][]Row height configuration
themeIThemeDefine-Worksheet theme configuration

Note:

The columns field is optional. When columns are set, the table will use them as the header with all the features of a VTable header.

If columns are not set or are an empty array, the table will have an empty header row. If you don't want to show this empty header, set showHeader to false, and only the data will be displayed as the table body.

If you want to use the first row of data as the header, you can set firstRowAsHeader. You can also use the setFirstRowAsHeader method.

Instance Methods

The VTableSheet instance provides the following common methods:

MethodParametersReturn ValueDescription
getActiveSheet-WorkSheetGet the currently active worksheet
getSheetByKeysheetKey: stringWorkSheetGet a worksheet by its key
addSheetsheetDefine: ISheetDefineWorkSheetAdd a new worksheet
removeSheetsheetKey: stringbooleanRemove the specified worksheet
getFilterManager-FilterManagerGet the filter manager
getFormulaManager-FormulaManagerGet the formula manager
getWorkbookHistoryManager-WorkbookHistoryManagerGet the workbook history manager
undo-voidUndo the latest workbook transaction
redo-voidRedo the latest workbook transaction
startHistoryTransaction-voidStart a workbook-level history transaction
endHistoryTransaction-voidEnd the current workbook-level history transaction
saveToConfig-IVTableSheetOptionsSave the current state as a configuration object
exportSheetToFilefileType: 'csv' | 'xlsx'voidExport the current worksheet to a file
importFileToSheet-voidImport a file to the current worksheet
release-voidDestroy the table instance

Simple Example

Below is a complete HTML example showing how to create and use the VTable-Sheet component:

This basic example shows how to create a simple table. In subsequent chapters, we will delve into more advanced features and how to use them.