Articles in this section
Category / Section

Add a custom font family dropdown in the JavaScript Spreadsheet.

3 mins read

This article explains how to add a custom font family dropdown in the JavaScript Spreadsheet Toolbar. To achieve this requirement, use the hideToolbarItems and addToolbarItems methods to hide and add the custom font family dropdown in the created event. Apply the selected font family using the cellFormat method in the dropdown select event.

[HTML]

<div id="spreadsheet"></div> 

[TS]

import { createElement, getComponent } from '@syncfusion/ej2-base';
import {
 CellModel,
 FontFamily,
 getCell,
 getCellIndexes,
 Spreadsheet,
} from '@syncfusion/ej2-spreadsheet';
import { DropDownButton } from '@syncfusion/ej2-splitbuttons';
import { MenuEventArgs } from '@syncfusion/ej2-navigations';

function appendDropdownBtn(id: string): HTMLButtonElement {
 let btnObj: DropDownButton = new DropDownButton({
   //Initializing the custom dropdown button
   items: [
     { text: 'Arial' },
     { text: 'Arial Black' },
     { text: 'Book Antiqua' },
     { text: 'Calibri' },
     { text: 'Courier' },
     { text: 'Din Condensed' },
     { text: 'Times New Roman' },
     { text: 'Verdana' },
     { text: 'Monaco' },
     { text: 'Garamond' },
   ],
   content: 'Calibri',
   createPopupOnClick: true,
   select: (args: MenuEventArgs) => {
     let cell: string = spreadsheet.getActiveSheet().activeCell;
     btnObj.content = args.item.text;
     spreadsheet.cellFormat(
       { fontFamily: args.item.text as FontFamily },
       cell
     ); //Update the cell style using cellFormat method.
   },
 });
 btnObj.appendTo(createElement('button', { id: id }));
 return btnObj.element;
}

let spreadsheet: Spreadsheet = new Spreadsheet({
 sheets: [
   {
     ranges: [{ dataSource: [{ Item: 'Bike', Colour: 'Black' }] }],
   },
 ],
 created: (): void => {
   spreadsheet.cellFormat(
     { textAlign: 'center', fontWeight: 'bold' },
     'A1:B1'
   );
   spreadsheet.hideToolbarItems('Home', [9]); //Hide the existing the toolbar item using hideToolbarItems method.
   spreadsheet.addToolbarItems(
     //Add the custom toolbar item using addToolbarItems method.
     'Home',
     [
       { type: 'Separator' },
       {
         id: 'fontStyle',
         tooltipText: 'Custom Font Style',
         template: appendDropdownBtn('fontStyle'),
       },
     ],
     9
   );
 },
 select: (args: { range: string }): void => {
   //Change the content of dropdown button on changing the cells using select event
   let dropDown: DropDownButton = getComponent(
     spreadsheet.element.querySelector('#fontStyle') as HTMLElement,
     'dropdown-btn'
   );
   let cellIndexs: number[] = getCellIndexes(args.range);
   let cell: CellModel = getCell(
     cellIndexs[0],
     cellIndexs[1],
     spreadsheet.getActiveSheet()
   );
   if (cell && cell.style && cell.style.fontFamily) {
     dropDown.content = cell.style.fontFamily;
   } else {
     dropDown.content = 'Calibri';
   }
 },
});

spreadsheet.appendTo('#spreadsheet');

Sample Link:

Refer to the working sample for additional details and implementation: Sample

Output:

Custom_font_family_dropdown.gif

Documentation link:

https://ej2.syncfusion.com/documentation/spreadsheet/ribbon

https://ej2.syncfusion.com/documentation/drop-down-button/getting-started

Conclusion

We hope you enjoyed learning about Adding a custom font family dropdown in the JavaScript Spreadsheet.

You can refer to our JavaScript Spreadsheet feature tour page to learn about its other groundbreaking feature representations and documentation, as well as how to quickly get started with configuration specifications. You can also explore our JavaScript Spreadsheet example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page.
If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied