Ribbon in React Spreadsheet component

14 Apr 202324 minutes to read

It helps to organize a spreadsheet’s features into a series of tabs. By clicking the expand or collapse icon, you can expand or collapse the ribbon toolbar dynamically.

Ribbon Customization

You can customize the ribbon using the following methods,

Method Action
hideRibbonTabs Used to show or hide the existing ribbon tabs.
enableRibbonTabs Used to enable or disable the existing ribbon tabs.
addRibbonTabs Used to add custom ribbon tabs.
hideToolbarItems Used to show or hide the existing ribbon toolbar items.
enableToolbarItems Used to enable or disable the specified toolbar items.
addToolbarItems Used to add the custom items in ribbon toolbar.
hideFileMenuItems Used to show or hide the ribbon file menu items.
enableFileMenuItems Used to enable or disable file menu items.
addFileMenuItems Used to add custom file menu items.

The following code example shows the usage of ribbon customization.

import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent, SheetsDirective, SheetDirective, RangesDirective, RangeDirective, ColumnsDirective, ColumnDirective } from '@syncfusion/ej2-react-spreadsheet';
import { DropDownButton } from '@syncfusion/ej2-splitbuttons/src/drop-down-button/drop-down-button';
import { select, createElement } from '@syncfusion/ej2-base';
import { data } from './datasource';

function App() {
    const spreadsheetRef = React.useRef(null);
    React.useEffect(() => {
        let spreadsheet = spreadsheetRef.current;
        if (spreadsheet) {
            spreadsheet.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:F1');
            // Hiding the `Insert` from ribbon.
            spreadsheet.hideRibbonTabs(['Insert']);
            // Set disabled state to `View` ribbon tab.
            spreadsheet.enableRibbonTabs(['View'], false);
            // Adding the `Help` ribbon tab at the last index.
            // Specify the ribbon tab header text in last optional argument(`insertBefore`) for inserting new tab before any existing tab.
            spreadsheet.addRibbonTabs([{
                header: { text: 'Help' }, content: [{
                    text: 'Feedback', tooltipText: 'Feedback',
                    click: () => { /* Any click action for this toolbar item will come here. */ }
                }]
            }]);
            // Hiding the unwanted toolbar items from `Home` by specifying its index.
            spreadsheet.hideToolbarItems('Home', [0, 1, 2, 4, 14, 15, 21, 24]);
            // Set disable state to `Underline`, 'Wrap text` toolbar items from `Home` by specifying the item id.
            spreadsheet.enableToolbarItems('Home', [`${spreadsheet.element.id}_underline`, `${spreadsheet.element.id}_wrap`], false);
            // Set disable state to `Protect Sheet` toolbar item from `Data` by mentioning its index.
            spreadsheet.enableToolbarItems('Data', [0], false);
            // Adding the new `Custom Formulas` toolbar item under `Formulas` tab for adding custom formulas.
            spreadsheet.addToolbarItems('Formulas', [{ type: 'Separator' }, {
                text: 'Custom Formulas', tooltipText: 'Custom Formulas',
                // You can set click handler for each new custom toolbar item
                click: () => {
                    // You can add custom formulas here.
                }
            }]);
            // Adding new custom item `Import` after the existing `Open` item. By default, new item will add after the specified item.
            spreadsheet.addFileMenuItems([{ text: 'Import', iconCss: 'e-open e-icons' }], 'Open');
            // Adding new custom item `Export As` after the existing `Save As` item.
            // Set `insertAfter` optional argument as `false` for adding new item before the specified item.
            spreadsheet.addFileMenuItems([{
                text: 'Export As', iconCss: 'e-save e-icons', items: [{ text: 'XLSX', iconCss: 'e-xlsx e-icons' },
                { text: 'XLS', iconCss: 'e-xls e-icons' }, { text: 'CSV', iconCss: 'e-csv e-icons' }]
            }], 'Save As', false);
            // Adding the new `custom dropdown button` in the ribbon toolbar item under the `Data` tab for adding a custom dropdown button using the addToolbarItems method in the spreadsheet ribbon.
            spreadsheet.addToolbarItems('Data', [{ type: 'Separator' }, { id: 'custombtn', tooltipText: 'Custom Btn', template: appendDropdownBtn('custombtn') }], 7);
        }
    }, []);
    const fileMenuBeforeOpen = () => {
        // Because the file menu items are created dynamically, you need to perform the hide or show and enable/disable operations
        // under filemenu before open event.
        // Hiding the `Save As` and `Open` item.
        let spreadsheet = spreadsheetRef.current;
        if (spreadsheet) {
            spreadsheet.hideFileMenuItems(['Save As', 'Open']);
            // Set disable state to `New` item. You can perform any file menu items customization by specifying the item id,
            // if it has more than one same item text. Set the last argument `isUniqueId` as true for using the item id.
            spreadsheet.enableFileMenuItems([`${spreadsheet.element.id}_New`], false, true);
        }
    };
    const fileMenuItemSelect = (args) => {
        let spreadsheet = spreadsheetRef.current;
        // Custom file menu items select handler
        switch (args.item.text) {
            case 'Import': select(`#${spreadsheet.element.id}_fileUpload`, spreadsheet.element).click();
                break;
            case 'XLSX': if (spreadsheet) { spreadsheet.save({ saveType: 'Xlsx' }) };
                break;
            case 'XLS': if (spreadsheet) { spreadsheet.save({ saveType: 'Xls' }) };
                break;
            case 'CSV': if (spreadsheet) { spreadsheet.save({ saveType: 'Csv' }) };
                break;
            default:
                break;
        }
    };
    const appendDropdownBtn = (id) => {
        let ddlItems = [
            {
                text: 'Download Excel',
            },
            {
                text: 'Download CSV',
            },
        ];
        let btnObj = new DropDownButton({
            items: ddlItems,
            content: 'Download',
            iconCss: 'e-icons e-download',
            select: (args) => {
                alert(args.item.text + ' clicked');
            },
        });
        btnObj.appendTo(createElement('button', { id: id }));
        return btnObj.element;
    }

    return (
        <div>
            <SpreadsheetComponent ref={spreadsheetRef} fileMenuBeforeOpen={fileMenuBeforeOpen} fileMenuItemSelect={fileMenuItemSelect}
                showFormulaBar={false} showSheetTabs={false} allowInsert={false} allowDelete={false} allowMerge={false}
                openUrl="https://services.syncfusion.com/react/production/api/spreadsheet/open"
                saveUrl="https://services.syncfusion.com/react/production/api/spreadsheet/save">
                <SheetsDirective>
                    <SheetDirective>
                        <RangesDirective>
                            <RangeDirective dataSource={data}></RangeDirective>
                        </RangesDirective>
                        <ColumnsDirective>
                            <ColumnDirective width={180}></ColumnDirective>
                            <ColumnDirective width={130}></ColumnDirective>
                            <ColumnDirective width={130}></ColumnDirective>
                            <ColumnDirective width={180}></ColumnDirective>
                            <ColumnDirective width={130}></ColumnDirective>
                            <ColumnDirective width={120}></ColumnDirective>
                        </ColumnsDirective>
                    </SheetDirective>
                </SheetsDirective>
            </SpreadsheetComponent>
        </div>
    );
};
export default App;

const root = createRoot(document.getElementById('root'));
root.render(<App />);
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent, SheetsDirective, SheetDirective, RangesDirective, RangeDirective, ColumnsDirective, ColumnDirective, MenuSelectEventArgs } from '@syncfusion/ej2-react-spreadsheet';
import { ItemModel } from '@syncfusion/ej2-navigations/src/toolbar';
import { DropDownButton } from '@syncfusion/ej2-splitbuttons/src/drop-down-button/drop-down-button';
import { MenuEventArgs } from '@syncfusion/ej2-splitbuttons';
import { select, createElement } from '@syncfusion/ej2-base';
import { data } from './datasource';

function App() {
    const spreadsheetRef = React.useRef<SpreadsheetComponent>(null);
    React.useEffect(() => {
        let spreadsheet = spreadsheetRef.current;
        if (spreadsheet) {
            spreadsheet.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:F1');
            // Hiding the `Insert` from ribbon.
            spreadsheet.hideRibbonTabs(['Insert']);
            // Set disabled state to `View` ribbon tab.
            spreadsheet.enableRibbonTabs(['View'], false);
            // Adding the `Help` ribbon tab at the last index.
            // Specify the ribbon tab header text in last optional argument(`insertBefore`) for inserting new tab before any existing tab.
            spreadsheet.addRibbonTabs([{
                header: { text: 'Help' }, content: [{
                    text: 'Feedback', tooltipText: 'Feedback',
                    click: (): void => { /* Any click action for this toolbar item will come here. */ }
                }]
            }]);
            // Hiding the unwanted toolbar items from `Home` by specifying its index.
            spreadsheet.hideToolbarItems('Home', [0, 1, 2, 4, 14, 15, 21, 24]);
            // Set disable state to `Underline`, 'Wrap text` toolbar items from `Home` by specifying the item id.
            spreadsheet.enableToolbarItems('Home', [`${spreadsheet.element.id}_underline`, `${spreadsheet.element.id}_wrap`], false);
            // Set disable state to `Protect Sheet` toolbar item from `Data` by mentioning its index.
            spreadsheet.enableToolbarItems('Data', [0], false);
            // Adding the new `Custom Formulas` toolbar item under `Formulas` tab for adding custom formulas.
            spreadsheet.addToolbarItems('Formulas', [{ type: 'Separator' }, {
                text: 'Custom Formulas', tooltipText: 'Custom Formulas',
                // You can set click handler for each new custom toolbar item
                click: (): void => {
                    // You can add custom formulas here.
                }
            }]);
            // Adding new custom item `Import` after the existing `Open` item. By default, new item will add after the specified item.
            spreadsheet.addFileMenuItems([{ text: 'Import', iconCss: 'e-open e-icons' }], 'Open');
            // Adding new custom item `Export As` after the existing `Save As` item.
            // Set `insertAfter` optional argument as `false` for adding new item before the specified item.
            spreadsheet.addFileMenuItems([{
                text: 'Export As', iconCss: 'e-save e-icons', items: [{ text: 'XLSX', iconCss: 'e-xlsx e-icons' },
                { text: 'XLS', iconCss: 'e-xls e-icons' }, { text: 'CSV', iconCss: 'e-csv e-icons' }]
            }], 'Save As', false);
            // Adding the new `custom dropdown button` in the ribbon toolbar item under the `Data` tab for adding a custom dropdown button using the addToolbarItems method in the spreadsheet ribbon.
            spreadsheet.addToolbarItems('Data', [{ type: 'Separator' }, { id: 'custombtn', tooltipText: 'Custom Btn', template: appendDropdownBtn('custombtn') }], 7);
        }
    }, []);
    const fileMenuBeforeOpen = (): void => {
        // Because the file menu items are created dynamically, you need to perform the hide or show and enable/disable operations
        // under filemenu before open event.
        // Hiding the `Save As` and `Open` item.
        let spreadsheet = spreadsheetRef.current;
        if (spreadsheet) {
            spreadsheet.hideFileMenuItems(['Save As', 'Open']);
            // Set disable state to `New` item. You can perform any file menu items customization by specifying the item id,
            // if it has more than one same item text. Set the last argument `isUniqueId` as true for using the item id.
            spreadsheet.enableFileMenuItems([`${spreadsheet.element.id}_New`], false, true);
        }
    };
    const fileMenuItemSelect = (args: MenuSelectEventArgs): void => {
        let spreadsheet = spreadsheetRef.current;
        // Custom file menu items select handler
        switch (args.item.text) {
            case 'Import': select(`#${spreadsheet.element.id}_fileUpload`, spreadsheet.element).click();
                break;
            case 'XLSX': if (spreadsheet) { spreadsheet.save({ saveType: 'Xlsx' }) };
                break;
            case 'XLS': if (spreadsheet) { spreadsheet.save({ saveType: 'Xls' }) };
                break;
            case 'CSV': if (spreadsheet) { spreadsheet.save({ saveType: 'Csv' }) };
                break;
            default:
                break;
        }
    };

    const appendDropdownBtn = (id: string): HTMLElement => {
        let ddlItems: ItemModel[] = [
            {
                text: 'Download Excel',
            },
            {
                text: 'Download CSV',
            },
        ];
        let btnObj: DropDownButton = new DropDownButton({
            items: ddlItems,
            content: 'Download',
            iconCss: 'e-icons e-download',
            select: (args: MenuEventArgs): void => {
                alert(args.item.text + ' clicked');
            },
        });
        btnObj.appendTo(createElement('button', { id: id }));
        return btnObj.element;
    }

    return (
        <div>
            <SpreadsheetComponent ref={spreadsheetRef} fileMenuBeforeOpen={fileMenuBeforeOpen} fileMenuItemSelect={fileMenuItemSelect}
                showFormulaBar={false} showSheetTabs={false} allowInsert={false} allowDelete={false} allowMerge={false}
                openUrl="https://services.syncfusion.com/react/production/api/spreadsheet/open"
                saveUrl="https://services.syncfusion.com/react/production/api/spreadsheet/save">
                <SheetsDirective>
                    <SheetDirective>
                        <RangesDirective>
                            <RangeDirective dataSource={data}></RangeDirective>
                        </RangesDirective>
                        <ColumnsDirective>
                            <ColumnDirective width={180}></ColumnDirective>
                            <ColumnDirective width={130}></ColumnDirective>
                            <ColumnDirective width={130}></ColumnDirective>
                            <ColumnDirective width={180}></ColumnDirective>
                            <ColumnDirective width={130}></ColumnDirective>
                            <ColumnDirective width={120}></ColumnDirective>
                        </ColumnsDirective>
                    </SheetDirective>
                </SheetsDirective>
            </SpreadsheetComponent>
        </div>
    );
};
export default App;

const root = createRoot(document.getElementById('root')!);
root.render(<App />);
/**
 * Ribbon customization data source
 */
export let data = [
    {
        'Customer Name': 'Romona Heaslip',
        'Model': 'Taurus',
        'Color': 'Aquamarine',
        'Payment Mode': 'Debit Card',
        'Delivery Date': '07/11/2015',
        'Amount': '8529.22'
    },
    {
        'Customer Name': 'Clare Batterton',
        'Model': 'Sparrow',
        'Color': 'Pink',
        'Payment Mode': 'Cash On Delivery',
        'Delivery Date': '7/13/2016',
        'Amount': '17866.19'
    },
    {
        'Customer Name': 'Eamon Traise',
        'Model': 'Grand Cherokee',
        'Color': 'Blue',
        'Payment Mode': 'Net Banking',
        'Delivery Date': '09/04/2015',
        'Amount': '13853.09'
    },
    {
        'Customer Name': 'Julius Gorner',
        'Model': 'GTO',
        'Color': 'Aquamarine',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '12/15/2017',
        'Amount': '2338.74'
    },
    {
        'Customer Name': 'Jenna Schoolfield',
        'Model': 'LX',
        'Color': 'Yellow',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '10/08/2014',
        'Amount': '9578.45'
    },
    {
        'Customer Name': 'Marylynne Harring',
        'Model': 'Catera',
        'Color': 'Green',
        'Payment Mode': 'Cash On Delivery',
        'Delivery Date': '7/01/2017',
        'Amount': '19141.62'
    },
    {
        'Customer Name': 'Vilhelmina Leipelt',
        'Model': '7 Series',
        'Color': 'Goldenrod',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '12/20/2015',
        'Amount': '6543.30'
    },
    {
        'Customer Name': 'Barby Heisler',
        'Model': 'Corvette',
        'Color': 'Red',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '11/24/2014',
        'Amount': '13035.06'
    },
    {
        'Customer Name': 'Karyn Boik',
        'Model': 'Regal',
        'Color': 'Indigo',
        'Payment Mode': 'Debit Card',
        'Delivery Date': '05/12/2014',
        'Amount': '18488.80'
    },
    {
        'Customer Name': 'Jeanette Pamplin',
        'Model': 'S4',
        'Color': 'Fuscia',
        'Payment Mode': 'Net Banking',
        'Delivery Date': '12/30/2014',
        'Amount': '12317.04'
    },
    {
        'Customer Name': 'Cristi Espinos',
        'Model': 'TL',
        'Color': 'Aquamarine',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '12/18/2013',
        'Amount': '6230.13'
    }
];
/**
 * Ribbon customization data source
 */
export let data: Object[] = [
    {
        'Customer Name': 'Romona Heaslip',
        'Model': 'Taurus',
        'Color': 'Aquamarine',
        'Payment Mode': 'Debit Card',
        'Delivery Date': '07/11/2015',
        'Amount': '8529.22'
    },
    {
        'Customer Name': 'Clare Batterton',
        'Model': 'Sparrow',
        'Color': 'Pink',
        'Payment Mode': 'Cash On Delivery',
        'Delivery Date': '7/13/2016',
        'Amount': '17866.19'
    },
    {
        'Customer Name': 'Eamon Traise',
        'Model': 'Grand Cherokee',
        'Color': 'Blue',
        'Payment Mode': 'Net Banking',
        'Delivery Date': '09/04/2015',
        'Amount': '13853.09'
    },
    {
        'Customer Name': 'Julius Gorner',
        'Model': 'GTO',
        'Color': 'Aquamarine',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '12/15/2017',
        'Amount': '2338.74'
    },
    {
        'Customer Name': 'Jenna Schoolfield',
        'Model': 'LX',
        'Color': 'Yellow',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '10/08/2014',
        'Amount': '9578.45'
    },
    {
        'Customer Name': 'Marylynne Harring',
        'Model': 'Catera',
        'Color': 'Green',
        'Payment Mode': 'Cash On Delivery',
        'Delivery Date': '7/01/2017',
        'Amount': '19141.62'
    },
    {
        'Customer Name': 'Vilhelmina Leipelt',
        'Model': '7 Series',
        'Color': 'Goldenrod',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '12/20/2015',
        'Amount': '6543.30'
    },
    {
        'Customer Name': 'Barby Heisler',
        'Model': 'Corvette',
        'Color': 'Red',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '11/24/2014',
        'Amount': '13035.06'
    },
    {
        'Customer Name': 'Karyn Boik',
        'Model': 'Regal',
        'Color': 'Indigo',
        'Payment Mode': 'Debit Card',
        'Delivery Date': '05/12/2014',
        'Amount': '18488.80'
    },
    {
        'Customer Name': 'Jeanette Pamplin',
        'Model': 'S4',
        'Color': 'Fuscia',
        'Payment Mode': 'Net Banking',
        'Delivery Date': '12/30/2014',
        'Amount': '12317.04'
    },
    {
        'Customer Name': 'Cristi Espinos',
        'Model': 'TL',
        'Color': 'Aquamarine',
        'Payment Mode': 'Credit Card',
        'Delivery Date': '12/18/2013',
        'Amount': '6230.13'
    }
];

Note

You can refer to our React Spreadsheet feature tour page for its groundbreaking feature representations. You can also explore our React Spreadsheet example to knows how to present and manipulate data.

See Also