Articles in this section
Category / Section

Add a custom font family dropdown in the JavaScript Spreadsheet.

3 mins read

This knowledge base explains how to add custom font family dropdown in the
JavaScript Spreadsheet Toolbar. To achieve this requirement, use the hideToolbarItems and addToolbarItems method to hide and add the custom font family dropdown in the created event. And 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:

https://stackblitz.com/edit/27g1dc-ntk7hm?file=index.ts

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
I 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 know about its other groundbreaking feature representations and documentation, and how to quickly get started for 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, Direct-Trac, 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