Search results

Workbook API in React Spreadsheet API component

Represents the Workbook.

Properties

activeSheetIndex

number

Specifies the active sheet index in the workbook.

    <div id="spreadsheet"></div>
import React from 'react';
import { createRoot } from 'react-dom/client';
import {
  SheetDirective,
  SheetsDirective,
  SpreadsheetComponent,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  return (
    /* Set 'Car Stock Report' sheet as active sheet. */
    <SpreadsheetComponent activeSheetIndex={1}>
      <SheetsDirective>
        <SheetDirective name="Car Sales Report" />
        <SheetDirective name="Car Stock Report" />
      </SheetsDirective>
    </SpreadsheetComponent>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

Defaults to 0

allowAutoFill

boolean

It allows to enable/disable AutoFill functionalities.

Defaults to true

allowCellFormatting

boolean

It allows you to apply styles (font size, font weight, font family, fill color, and more) to the spreadsheet cells.

Defaults to true

allowChart

boolean

It allows you to insert the chart in a spreadsheet.

Defaults to true

allowConditionalFormat

boolean

It allows you to apply conditional formatting to the sheet.

Defaults to true

allowDataValidation

boolean

It allows you to apply data validation to the spreadsheet cells.

Defaults to true

allowDelete

boolean

It allows you to delete rows, columns, and sheets from a spreadsheet.

Defaults to true

allowEditing

boolean

It allows you to add new data or update existing cell data. If it is false, it will act as read only mode.

Defaults to true

allowFiltering

boolean

It allows to enable/disable filter and its functionalities.

Defaults to true

allowFindAndReplace

boolean

It allows to enable/disable find and replace with its functionalities.

Defaults to true

allowFreezePane

boolean

It allows to enable/disable freeze pane functionality in spreadsheet.

Defaults to true

It allows to enable/disable Hyperlink and its functionalities.

Defaults to true

allowImage

boolean

It allows you to insert the image in a spreadsheet.

Defaults to true

allowInsert

boolean

It allows you to insert rows, columns, and sheets into the spreadsheet.

Defaults to true

allowMerge

boolean

It allows you to merge the range of cells.

Defaults to true

allowNumberFormatting

boolean

It allows formatting a raw number into different types of formats (number, currency, accounting, percentage, short date, long date, time, fraction, scientific, and text) with built-in format codes.

Defaults to true

allowOpen

boolean

It allows you to open an Excel file (.xlsx, .xls, and .csv) in Spreadsheet.

Defaults to true

allowSave

boolean

It allows you to save Spreadsheet with all data as Excel file (.xlsx, .xls, and .csv).

Defaults to true

allowSorting

boolean

It allows to enable/disable sort and its functionalities.

Defaults to true

autoFillSettings

AutoFillSettingsModel

Configures the auto fill settings. The autoFillSettings fillType property has FOUR types and it is described below:

  • CopyCells: To update the copied cells for the selected range.
  • FillSeries: To update the filled series for the selected range.
  • FillFormattingOnly: To fill the formats only for the selected range.
  • FillWithoutFormatting: To fill without the format for the selected range.
    <div id="spreadsheet"></div>
import React from 'react';
import { createRoot } from 'react-dom/client';
import {
  AutoFillSettingsModel,
  SpreadsheetComponent,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const autoFillSettings: AutoFillSettingsModel = {
    fillType: 'CopyCells',
    showFillOptions: true,
  };

  return (
    <SpreadsheetComponent
      autoFillSettings={autoFillSettings}
    />
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

The allowAutoFill property should be true.

Defaults to { fillType: ‘FillSeries’, showFillOptions: true }

cellStyle

CellStyleModel

Specifies the cell style options.

    <div id="spreadsheet"></div>
import React from 'react';
import { createRoot } from 'react-dom/client';
import {
  CellStyleModel,
  SpreadsheetComponent,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const cellStyle: CellStyleModel = {
    fontWeight: 'bold',
    fontSize: '12pt',
    fontStyle: 'italic',
    textIndent: '2pt',
    backgroundColor: '#4b5366',
    color: '#ffffff',
  };
  return <SpreadsheetComponent cellStyle={cellStyle} />;
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

Defaults to {}

definedNames

DefineNameModel[]

Specifies the name of a range and uses it in a formula for calculation.

    <div id="spreadsheet"></div>
import React from 'react';
import { createRoot } from 'react-dom/client';
import {
  DefineNameModel,
  SpreadsheetComponent,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const definedNames: DefineNameModel[] = [
    { name: 'Group1', refersTo: 'Sheet1!A1:B5' },
  ];

  return (
    <SpreadsheetComponent definedNames={definedNames} />
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

Defaults to []

enablePersistence

boolean

Enable or disable persisting component’s state between page reloads.

Defaults to false

enableRtl

boolean

Enable or disable rendering component in right to left direction.

Defaults to false

height

string | number

Defines the height of the Spreadsheet. It accepts height as pixels, number, and percentage.

    <div id="spreadsheet"></div>
import React from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  return (
    <SpreadsheetComponent
      height="600px"
    ></SpreadsheetComponent>
  );
};

export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

Defaults to ‘100%’

isProtected

boolean

Specifies to protect the workbook.

Defaults to false

listSeparator

string

Specifies the list separator which is used as the formula argument separator.

Defaults to ’,’

locale

string

Overrides the global culture and localization value for this component. Default global culture is ‘en-US’.

Defaults to

openUrl

string

Specifies the service URL to open excel file in spreadsheet.

Defaults to

password

string

Specifies the password.

Defaults to

saveUrl

string

Specifies the service URL to save spreadsheet as Excel file.

Defaults to

sheets

SheetModel[]

Configures sheets and its options.

    <div id="spreadsheet"></div>
import React from 'react';
import { createRoot } from 'react-dom/client';
import { salesData } from './data';
import {
  SpreadsheetComponent,
  SheetsDirective,
  SheetDirective,
  RangesDirective,
  RangeDirective,
  RowsDirective,
  RowDirective,
  CellsDirective,
  CellDirective,
  ColumnsDirective,
  ColumnDirective,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  return (
    <SpreadsheetComponent>
      <SheetsDirective>
        <SheetDirective name="First Sheet">
          <RangesDirective>
            <RangeDirective dataSource={salesData} />
          </RangesDirective>
          <RowsDirective>
            <RowDirective index={30}>
              <CellsDirective>
                <CellDirective index={4} value="Total Amount:" />
                <CellDirective formula="=SUM(F2:F30)" />
              </CellsDirective>
            </RowDirective>
          </RowsDirective>
        </SheetDirective>
        <SheetDirective name="Second Sheet">
          <ColumnsDirective>
            <ColumnDirective width={180} />
            <ColumnDirective width={130} />
            <ColumnDirective width={130} />
          </ColumnsDirective>
        </SheetDirective>
      </SheetsDirective>
    </SpreadsheetComponent>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

Defaults to []

showFormulaBar

boolean

It shows or hides the formula bar and its features.

Defaults to true

showRibbon

boolean

It shows or hides the ribbon in spreadsheet.

Defaults to true

showSheetTabs

boolean

It shows or hides the sheets tabs, this is used to navigate among the sheets and create or delete sheets by UI interaction.

Defaults to true

width

string | number

Defines the width of the Spreadsheet. It accepts width as pixels, number, and percentage.

    <div id="spreadsheet"></div>
import React from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  return (
    <SpreadsheetComponent
      /**
       * Specifies the width.
       */
      width="600px"
    />
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

Defaults to ‘100%’

Methods

Unfreeze

This method is used to unfreeze the frozen rows and columns from the active sheet.

Parameter Type Description
sheet (optional) number | string Specifies the sheet name or index in which the unfreeze operation will perform. By default,
active sheet will be considered.

Returns void

addCustomFunction

To add custom library function.

Returns void

addDefinedName

Adds the defined name to the Spreadsheet.

Parameter Type Description
definedName DefineNameModel Specifies the name.

Returns boolean

addEventListener

Adds the handler to the given event listener.

Parameter Type Description
eventName string A String that specifies the name of the event
handler Function Specifies the call to run when the event occurs.

Returns void

attachUnloadEvent

Adding unload event to persist data when enable persistence true

Returns void

autoFill

Used to perform autofill action based on the specified range in spreadsheet.

Parameter Type Description
fillRange string Specifies the fill range.
dataRange (optional) string Specifies the data range.
direction (optional) AutoFillDirection Specifies the direction(“Down”,“Right”,“Up”,“Left”) to be filled.
fillType (optional) AutoFillType Specifies the fill type(“FillSeries”,“CopyCells”,“FillFormattingOnly”,“FillWithoutFormatting”) for autofill action.

Returns void

cellFormat

Applies the style (font family, font weight, background color, etc…) to the specified range of cells.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Apply the styles to the cells in the specified range.
    spreadsheetRef.current?.cellFormat(
      {
        fontWeight: 'bold',
        fontSize: '12pt',
        backgroundColor: '#279377',
        color: '#ffffff',
      },
      'A2:E2'
    );
    spreadsheetRef.current?.cellFormat(
      { verticalAlign: 'middle', fontFamily: 'Axettac Demo' },
      'A2:E12'
    );
    spreadsheetRef.current?.cellFormat({ textAlign: 'center' }, 'A2:A12');
    // Apply text-indent to 2nd & 4th columns.
    const style: { textAlign: string; textIndent: string } = {
      textAlign: 'left',
      textIndent: '8pt',
    };
    spreadsheetRef.current?.cellFormat(style, 'B2:B12');
    spreadsheetRef.current?.cellFormat(style, 'D2:D12');
    spreadsheetRef.current?.cellFormat(
      { fontStyle: 'italic', textAlign: 'right' },
      'C3:C12'
    );
    spreadsheetRef.current?.cellFormat({ textAlign: 'center' }, 'E2:E12');
  };

  return (
    <div>
      <button onClick={onClick}>Apply Cell Format</button>
      <SpreadsheetComponent ref={spreadsheetRef} />
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
style CellStyleModel Specifies the cell style.
range (optional) string Specifies the address for the range of cells.

Returns void

clear

This method is used to Clear contents, formats and hyperlinks in spreadsheet.

Parameter Type Description
options ClearOptions Options for clearing the content, formats and hyperlinks in spreadsheet.

Returns void

computeExpression

Used to compute the specified expression/formula.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Calculate the result of an arithmetic expression or formula.
    spreadsheetRef.current?.computeExpression('522+1');
    spreadsheetRef.current?.computeExpression('=SUM(F2:F3)');
  };

  return (
    <div>
      <button onClick={onClick}>To Compute Expression</button>
      <SpreadsheetComponent ref={spreadsheetRef} />
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
formula string Specifies the formula(=SUM(A1:A3)) or expression(2+3).

Returns string | number

dataBind

When invoked, applies the pending property changes immediately to the component.

Returns void

delete

Used to delete rows, columns and sheets from the spreadsheet.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    spreadsheetRef.current?.delete(1, 1, 'Row', 'Sheet1');
    spreadsheetRef.current?.delete(2, 2, 'Column', 'Sheet1');
  };

  return (
    <div>
      <button onClick={onClick}>Delete Row & Column</button>
      <SpreadsheetComponent ref={spreadsheetRef} />
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
startIndex (optional) number Specifies the start sheet / row / column index.
endIndex (optional) number Specifies the end sheet / row / column index.
model (optional) ModelType Specifies the delete model type. By default, the model is considered as Sheet. The possible values are,
- Row: To delete rows.
- Column: To delete columns.
- Sheet: To delete sheets.
sheet (optional) number | string Specifies the sheet name or index in which the delete operation will perform. By default,
active sheet will be considered. It is applicable only for model type Row and Column.

Returns void

deleteChart

Used to delete the chart from spreadsheet.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);

  const onClick = (): void => {
    spreadsheetRef.current?.deleteChart('Chart');
  };

  const onCreated = (): void => {
    spreadsheetRef.current?.insertChart([
      {
        type: 'Line',
        theme: 'Material',
        isSeriesInRows: false,
        range: 'A1:B5',
        id: 'Chart',
      },
    ]);
  };

  return (
    <div>
      <button onClick={onClick}>Delete Chart</button>
      <SpreadsheetComponent ref={spreadsheetRef} created={onCreated} />
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
id (optional) string Specifies the chart element id.

Returns void

destroy

Destroys the Workbook library.

Returns void

detachUnloadEvent

Removing unload event to persist data when enable persistence true

Returns void

duplicateSheet

Used to make a duplicate/copy of the sheet in the spreadsheet.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    spreadsheetRef.current?.duplicateSheet();
    spreadsheetRef.current?.duplicateSheet(0);
  };

  return (
    <div>
      <button onClick={onClick}>Make Duplicate Sheet</button>
      <SpreadsheetComponent ref={spreadsheetRef} />
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
sheetIndex (optional) number Specifies the index of the sheet to be duplicated. By default, the active sheet will be duplicated.

Returns void

filter

Filters the range of cells in the sheet.

Parameter Type Description
filterOptions (optional) FilterOptions Specifies the filterOptions
range (optional) string Specifies the range

Returns Promise

freezePanes

This method is used to freeze rows and columns after the specified cell in the Spreadsheet.

Parameter Type Description
row number Specifies the freezed row count.
column number Specifies the freezed column count.
sheet (optional) number | string Specifies the sheet name or index in which the freeze operation will perform. By default,
active sheet will be considered.

Returns void

getData

Gets the range of data as JSON from the specified address.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { salesData } from './data';
import {
  SpreadsheetComponent,
  SheetsDirective,
  SheetDirective,
  RangesDirective,
  RangeDirective,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Return the specified cell address data as JSON.
    spreadsheetRef.current
      ?.getData('Sheet1!A1:B2')
      .then((data) => console.log(data));
    spreadsheetRef.current?.getData('A1:B2').then((data) => console.log(data));
  };

  return (
    <div>
      <button onClick={onClick}>Get Data</button>
      <SpreadsheetComponent ref={spreadsheetRef}>
        <SheetsDirective>
          <SheetDirective>
            <RangesDirective>
              <RangeDirective dataSource={salesData} />
            </RangesDirective>
          </SheetDirective>
        </SheetsDirective>
      </SpreadsheetComponent>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
address string Specifies the address for range of cells.

Returns Promise

getDisplayText

Gets the formatted text of the cell.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { salesData } from './data';
import {
  SpreadsheetComponent,
  SheetsDirective,
  SheetDirective,
  RangesDirective,
  RangeDirective,
  getCell,
  CellModel,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Get the cell model for the given row, column, and sheet indexes.
    const cell: CellModel = getCell(
      0,
      0,
      spreadsheetRef.current?.getActiveSheet()!
    );
    // To get the formatted cell value, specify the cell model.
    const displayText: string = spreadsheetRef.current?.getDisplayText(cell);
    console.log(displayText);
  };

  return (
    <div>
      <button onClick={onClick}>Get Display Text</button>
      <SpreadsheetComponent ref={spreadsheetRef}>
        <SheetsDirective>
          <SheetDirective>
            <RangesDirective>
              <RangeDirective dataSource={salesData} />
            </RangesDirective>
          </SheetDirective>
        </SheetsDirective>
      </SpreadsheetComponent>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
cell CellModel Specifies the cell.

Returns string

getLocalData

Returns the persistence data for component

Returns any

getRootElement

Returns the route element of the component

Returns HTMLElement

getRowData

Used to get a row data from the data source with updated cell value.

Parameter Type Description
index (optional) number Specifies the row index.
sheetIndex (optional) number Specifies the sheet index. By default, it consider the active sheet index.

Returns Object[]

handleUnload

Handling unload event to persist data when enable persistence true

Returns void

hideColumn

Used to hide/show the columns in spreadsheet.

Parameter Type Description
startIndex number Specifies the start column index.
endIndex number Specifies the end column index.
hide boolean Set true / false to hide / show the columns.

Returns void

hideRow

Used to hide/show the rows in spreadsheet.

Parameter Type Description
startIndex number Specifies the start row index.
endIndex number Specifies the end row index.
hide boolean To hide/show the rows in specified range.

Returns void

insertChart

Used to set the chart in spreadsheet.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Insert a chart into a spreadsheet using the chart options specified.
    spreadsheetRef.current?.insertChart([
      {
        type: 'Line',
        theme: 'Material',
        isSeriesInRows: false,
        range: 'A1',
        id: 'Chart',
      },
    ]);
  };

  return (
    <div>
      <button onClick={onClick}>Insert Chart</button>
      <SpreadsheetComponent ref={spreadsheetRef}></SpreadsheetComponent>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
chart (optional) ChartModel[] Specifies the options to insert chart in spreadsheet

Returns void

insertColumn

Used to insert columns in to the spreadsheet.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Insert the column at the starting column index specified.
    spreadsheetRef.current?.insertColumn([{ index: 1, width: 95 }], 1);
  };

  return (
    <div>
      <button onClick={onClick}>Insert Column</button>
      <SpreadsheetComponent ref={spreadsheetRef}></SpreadsheetComponent>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
startColumn (optional) number | ColumnModel[] Specifies the start column index / column model which needs to be inserted.
endColumn (optional) number Specifies the end column index.
sheet (optional) number | string Specifies the sheet name or index in which the insert operation will perform. By default,
active sheet will be considered.

Returns void

insertImage

Used to set the image in spreadsheet.

Parameter Type Description
images ImageModel[] Specifies the options to insert image in spreadsheet.
range (optional) string Specifies the range in spreadsheet.

Returns void

insertRow

Used to insert rows in to the spreadsheet.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Insert a row at the specified row index.
    spreadsheetRef.current?.insertRow(4, 4);
  };

  return (
    <div>
      <button onClick={onClick}>Insert Row</button>
      <SpreadsheetComponent ref={spreadsheetRef}></SpreadsheetComponent>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
startRow (optional) number | RowModel[] Specifies the start row index / row model which needs to be inserted.
endRow (optional) number Specifies the end row index.
sheet (optional) number | string Specifies the sheet name or index in which the insert operation will perform. By default,
active sheet will be considered.

Returns void

insertSheet

Used to insert sheets in to the spreadsheet.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Insert a sheet at the specified sheet index.
    spreadsheetRef.current?.insertSheet(0, 2);
  };

  return (
    <div>
      <button onClick={onClick}>Insert Sheet</button>
      <SpreadsheetComponent ref={spreadsheetRef}></SpreadsheetComponent>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
startSheet (optional) number | SheetModel[] Specifies the start sheet index / sheet model which needs to be inserted.
endSheet (optional) number Specifies the end sheet index.

Returns void

isValidCell

To determine whether the cell value in a data validation applied cell is valid or not.

Parameter Type Description
cellAddress (optional) string Address of the cell.

Returns boolean

lockCells

Applies cell lock to the specified range of cells.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import {
  ProtectSettingsModel,
  SpreadsheetComponent,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Protect the worksheet.
    const protectSetting: ProtectSettingsModel = {
      selectCells: true,
      formatCells: false,
      formatRows: false,
      formatColumns: false,
      insertLink: false,
    };
    spreadsheetRef.current?.protectSheet('Sheet1', protectSetting);
    // Unlock the A2:AZ100 cell range.
    spreadsheetRef.current?.lockCells('A2:AZ100', false);
    // Lock the A1:Z1 cell range.
    spreadsheetRef.current?.lockCells('A1:Z1', true);
  };

  return (
    <div>
      <button onClick={onClick}>Lock Cells</button>
      <SpreadsheetComponent ref={spreadsheetRef}></SpreadsheetComponent>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
range (optional) string Specifies the address for the range of cells.
isLocked (optional) boolean -Specifies the cell is locked or not.

Returns void

merge

Used to merge the range of cells.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onCreated = (): void => {
    // Merge the cells in the specified range.
    spreadsheetRef.current?.merge('B1:E2');
  };

  return (
    <SpreadsheetComponent
      ref={spreadsheetRef}
      created={onCreated}
    ></SpreadsheetComponent>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
range (optional) string Specifies the range of cells as address.
type (optional) MergeType Specifies the merge type. The possible values are,
- All: Merge all the cells between provided range.
- Horizontally: Merge the cells row-wise.
- Vertically: Merge the cells column-wise.

Returns void

moveSheet

Used to move the sheets to the specified position in the list of sheets.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import {
  SpreadsheetComponent,
  SheetsDirective,
  SheetDirective,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Move the active sheet to the specified position.
    spreadsheetRef.current?.moveSheet(1);
    // Move the list of specified sheets to the specified position.
    spreadsheetRef.current?.moveSheet(0, [1, 2]);
  };

  return (
    <div>
      <button onClick={onClick}>Move Sheets</button>
      <SpreadsheetComponent ref={spreadsheetRef}>
        <SheetsDirective>
          <SheetDirective></SheetDirective>
          <SheetDirective></SheetDirective>
          <SheetDirective></SheetDirective>
        </SheetsDirective>
      </SpreadsheetComponent>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
position number Specifies the position to move a sheet in the list of sheets.
sheetIndexes (optional) number[] Specifies the indexes of the sheet to be moved. By default, the active sheet will be moved.

Returns void

numberFormat

Applies the number format (number, currency, percentage, short date, etc…) to the specified range of cells.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { salesData } from './data';
import {
  SpreadsheetComponent,
  SheetsDirective,
  SheetDirective,
  RangesDirective,
  RangeDirective,
  getFormatFromType,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onCreated = (): void => {
    // Apply the number format to the specified range of cells.
    spreadsheetRef.current?.numberFormat('$#,##0.00', 'E1:E3');
    // Apply the accounting format to the specified range of cells.
    spreadsheetRef.current?.numberFormat(
      getFormatFromType('Accounting'),
      'E4:E10'
    );
    // Apply the percentage format to the specified range of cells.
    spreadsheetRef.current?.numberFormat('0%', 'F3:F10');
  };

  return (
    <SpreadsheetComponent ref={spreadsheetRef} created={onCreated}>
      <SheetsDirective>
        <SheetDirective name="Car Sales Report">
          <RangesDirective>
            <RangeDirective dataSource={salesData} />
          </RangesDirective>
        </SheetDirective>
      </SheetsDirective>
    </SpreadsheetComponent>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
format string Specifies the number format code.
range (optional) string Specifies the address for the range of cells.

Returns void

open

Opens the specified excel file or stream.

Parameter Type Description
options OpenOptions Options for opening the excel file.

Returns void

openFromJson

Opens the specified JSON object.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  let response = Object;
  const save = (): void => {
    // Save the spreadsheet data as JSON.
    spreadsheetRef.current?.saveAsJson().then((Json) => (response = Json));
  };

  const open = (): void => {
    // Load the JSON data to the spreadsheetRef.currrent?.
    spreadsheetRef.current?.openFromJson({ file: response.jsonObject });
  };

  return (
    <div>
      <SpreadsheetComponent ref={spreadsheetRef} />
      <button onClick={save}>Save</button>
      <button onClick={open}>Open</button>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

The available arguments in options are:

  • file: Specifies the spreadsheet model as object or string. And the object contains the jsonObject, which is saved from spreadsheet using saveAsJson method.
  • triggerEvent: Specifies whether to trigger the openComplete event or not.
Parameter Type Description
options Object Options for opening the JSON object.

Returns void

protectSheet

Protect the active sheet based on the protect sheetings.

Parameter Type Description
sheet (optional) number | string Specifies the sheet to protect.
protectSettings (optional) ProtectSettingsModel Specifies the protect settings of the sheet.
password (optional) string Specifies the password to protect

Returns void

refresh

Applies all the pending property changes and render the component again.

Returns void

removeDefinedName

Removes the defined name from the Spreadsheet.

Parameter Type Description
definedName string Specifies the name.
scope string Specifies the scope of the defined name.

Returns boolean

removeEventListener

Removes the handler from the given event listener.

Parameter Type Description
eventName string A String that specifies the name of the event to remove
handler Function Specifies the function to remove

Returns void

save

Saves the Spreadsheet data to Excel file.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { salesData } from './data';
import {
  SpreadsheetComponent,
  SheetsDirective,
  RangesDirective,
  RangeDirective,
  SheetDirective,
} from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    spreadsheetRef.current?.save({
      url: 'https://services.syncfusion.com/react/production/api/spreadsheet/save',
      fileName: 'Worksheet',
      saveType: 'Xlsx',
    });
  };

  return (
    <div>
      <SpreadsheetComponent ref={spreadsheetRef}>
        <SheetsDirective>
          <SheetDirective name="Car Sales Report">
            <RangesDirective>
              <RangeDirective dataSource={salesData} />
            </RangesDirective>
          </SheetDirective>
        </SheetsDirective>
      </SpreadsheetComponent>
      <button onClick={onClick}>Save</button>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

The available arguments in saveOptions are:

  • url: Specifies the save URL.
  • fileName: Specifies the file name.
  • saveType: Specifies the file type need to be saved.
Parameter Type Description
saveOptions SaveOptions Options for saving the excel file.

Returns void

saveAsJson

Saves the Spreadsheet data as JSON object.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  let response = Object;
  const save = (): void => {
    // Save the spreadsheet data as JSON.
    spreadsheetRef.current?.saveAsJson().then((Json) => (response = Json));
  };

  const open = (): void => {
    // Load the JSON data to the spreadsheetRef.currrent?.
    spreadsheetRef.current?.openFromJson({ file: response.jsonObject });
  };

  return (
    <div>
      <SpreadsheetComponent ref={spreadsheetRef} />
      <button onClick={save}>Save</button>
      <button onClick={open}>Open</button>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

Returns Promise

setBorder

Sets the border to specified range of cells.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // Sets the border to the specified cell range.
    spreadsheetRef.current?.setBorder(
      { border: '1px solid #000000' },
      'C6:G8',
      'Outer'
    );
    spreadsheetRef.current?.setBorder({ border: '1px solid #000000' });
  };

  return (
    <div>
      <SpreadsheetComponent ref={spreadsheetRef}></SpreadsheetComponent>
      <button onClick={onClick}>Set Border</button>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);

Returns void

sort

Sorts the range of cells in the active Spreadsheet.

Returns Promise

unMerge

Used to split the merged cell into multiple cells.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // To split the merged cell into multiple cells.
    spreadsheetRef.current?.unMerge('B1:E2');
  };

  const onCreated = (): void => {
    spreadsheetRef.current?.merge('B1:E2');
  };

  return (
    <div>
      <SpreadsheetComponent ref={spreadsheetRef} created={onCreated} />
      <button onClick={onClick}>UnMerge Cells</button>
    </div>
  );
};

export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
range (optional) string Specifies the range of cells as address.

Returns void

unfreezePanes

This method is used to unfreeze the frozen rows and columns from spreadsheet.

Parameter Type Description
sheet (optional) number | string Specifies the sheet name or index in which the unfreeze operation will perform. By default,
active sheet will be considered.

Returns void

unprotectSheet

Unprotect the active sheet.

Parameter Type Description
sheet number | string Specifies the sheet to Unprotect.

Returns void

updateCell

To update a cell properties.

Parameter Type Description
cell CellModel Cell properties.
address (optional) string Address to update.

Returns void

updateRange

This method is used to update the Range property in specified sheetIndex.

Parameter Type Description
range RangeModel Specifies the range properties to update.
sheetIdx (optional) number Specifies the sheetIdx to update.

Returns void

wrap

This method is used to wrap/unwrap the text content of the cell.

    <div id="spreadsheet"></div>
import React, { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';

const App = () => {
  const spreadsheetRef = useRef<SpreadsheetComponent>(null);
  const onClick = (): void => {
    // To wrap/unwrap the cell's text content with the specified address.
    spreadsheetRef.current?.wrap('B5', true);
  };

  return (
    <div>
      <SpreadsheetComponent ref={spreadsheetRef} />
      <button onClick={onClick}>To Wrap</button>
    </div>
  );
};
export default App;

const root = createRoot(document.getElementById('spreadsheet'));
root.render(<App />);
Parameter Type Description
address string Address of the cell to be wrapped.
wrap boolean Set false if the text content of the cell to be unwrapped.

Returns void

Inject

Dynamically injects the required modules to the component.

Parameter Type Description
moduleList Function[] ?

Returns void

Events

beforeCellFormat

EmitType<BeforeCellFormatArgs>

Triggers before the cell format applied to the cell.

<div id='Spreadsheet'></div>
new Spreadsheet({
      beforeCellFormat: (args: BeforeCellFormatArgs) => {
      }
     ...
 }, '#Spreadsheet');

beforeCellUpdate

EmitType<BeforeCellUpdateArgs>

Triggers before changing any cell properties.

<div id='Spreadsheet'></div>
 new Spreadsheet({
     beforeCellUpdate: (args: BeforeCellUpdateArgs) => {
     }
     ...
 }, '#Spreadsheet');

beforeOpen

EmitType<BeforeOpenEventArgs>

Triggers before opening an Excel file.

<div id='Spreadsheet'></div>
new Spreadsheet({
      beforeOpen: (args: BeforeOpenEventArgs) => {
      }
     ...
 }, '#Spreadsheet');

beforeSave

EmitType<BeforeSaveEventArgs>

Triggers before saving the Spreadsheet as Excel file.

<div id='Spreadsheet'></div>
new Spreadsheet({
      beforeSave: (args: BeforeSaveEventArgs) => {
      }
     ...
 }, '#Spreadsheet');

openFailure

EmitType<OpenFailureArgs>

Triggers when the opened Excel file fails to load.

<div id='Spreadsheet'></div>
new Spreadsheet({
      openFailure: (args: OpenFailureArgs) => {
      }
     ...
 }, '#Spreadsheet');

queryCellInfo

EmitType<CellInfoEventArgs>

Triggered every time a request is made to access cell information.

<div id='Spreadsheet'></div>
new Spreadsheet({
     queryCellInfo: (args: CellInfoEventArgs) => {
     }
     ...
 }, '#Spreadsheet');

saveComplete

EmitType<SaveCompleteEventArgs>

Triggers after saving the Spreadsheet as Excel file.

<div id='Spreadsheet'></div>
new Spreadsheet({
      saveComplete: (args: SaveCompleteEventArgs) => {
      }
     ...
 }, '#Spreadsheet');