Articles in this section
Category / Section

How to programmatically delete notes from an empty cell in JavaScript Spreadsheet?

9 mins read

This article explains how to programmatically delete notes from an empty cell in the JavaScript Spreadsheet. This can be achieved by assigning null to the notes property of the cell, preventing the note indicator from being added to the cell during the initial rendering. This can be handled in the beforeCellRender event, which is triggered before the cell is appended to the DOM in the spreadsheet.

Additionally, you can remove the note indicator from the cell by assigning null to the notes property if an empty note is added through UI interaction. This can be done by verifying if the notes property is empty in the actionComplete event of the spreadsheet.

In the example below, the note indicator is removed from cell B5 as the notes property is empty during the initial rendering in the beforeCellRender event. Again, in the actionComplete event, when an empty note is added through UI interaction, the notes property is set to null, and the note element in the cell is removed.

[HTML]

<div class="control-section">
   <div id="spreadsheet"></div>
</div> 

[JS]

import { CellStyleModel, SheetModel, Spreadsheet, CellRenderEventArgs, getCellIndexes, CellModel, getCell } from '@syncfusion/ej2-spreadsheet';
import * as dataSource from './note-data.json';

let sheet: SheetModel[] = [
   {
       ranges: [{
           dataSource: (dataSource as any).notesData,
           startCell: 'B3'
       },
       ],
       name: 'Cart',
       activeCell: 'A4',
       rows: [
           {
               index: 0,
               cells: [{
                   value: 'Shopping Cart', rowSpan: 2, colSpan: 6, style: {
                       fontSize: '20pt', fontWeight: 'bold',
                       textAlign: 'center', backgroundColor: '#279377', verticalAlign: 'middle', color: '#ffffff'
                   }
               }]
           },
           {
               index: 2,
               cells: [
                   { value: 'Product ID', style: { fontWeight: 'bold', textAlign: 'center' } },
                   { style: { fontWeight: 'bold', textAlign: 'center' } },
                   { style: { fontWeight: 'bold', textAlign: 'center' } },
                   { style: { fontWeight: 'bold', textAlign: 'center' } },
                   { style: { fontWeight: 'bold', textAlign: 'center' } },
                   { style: { fontWeight: 'bold', textAlign: 'center' } }
               ]
           },
           {
               cells: [{ value: '101', style: { textAlign: 'left' } },
               { index: 1, notes: 'This product has been the most profitable this month.' }
               ]
           },
           {
               cells: [{ value: '102', style: { textAlign: 'left' } },
               { index: 1, notes: '' }
               ]
           },
           {
               cells: [{ value: '103', style: { textAlign: 'left' } },
               { index: 1, notes: 'This product has been the least profitable this month.' }
               ]
           },
           {
               cells: [{ value: '104', style: { textAlign: 'left' } }]
           },
           {
               cells: [{ value: '105', style: { textAlign: 'left' } },
               { index: 1, notes: 'This product has had the highest sales in terms of quantity this month.' }
               ]
           },
           {
               cells: [{ value: '106', style: { textAlign: 'left' } }]
           },
           {
               cells: [{ value: '107', style: { textAlign: 'left' } }]
           },
           {
               cells: [{ value: '108', style: { textAlign: 'left' } }]
           },
           {
               cells: [{ value: '109', style: { textAlign: 'left' } }]
           },
           {
               cells: [
                   {
                       index: 4, value: 'Total Amount', style: {
                           border: '1px solid #A6A6A6', fontWeight: 'bold',
                           textAlign: 'center', verticalAlign: 'middle'
                       }
                   },
                   {
                       index: 5, formula: '=Sum(F4:F12)', format: '$#,##0.00', style: {
                           border: '1px solid #A6A6A6',
                           textAlign: 'right', verticalAlign: 'middle', fontWeight: 'bold'
                       }
                   }
               ]
           }
       ],
       columns: [
           { width: 88, }, { width: 120 }, { width: 100 }, { width: 100 }, { width: 100 }, { width: 110 }
       ]
   }
];
let spreadsheet: Spreadsheet = new Spreadsheet({
   sheets: sheet,
   created: function () {
       spreadsheet.numberFormat('$#,##0.00', 'F4:F12');
       spreadsheet.numberFormat('$###', 'E4:E12');
       spreadsheet.freezePanes(3, 0);
   },
   openUrl: 'https://services.syncfusion.com/js/production/api/spreadsheet/open',
   saveUrl: 'https://services.syncfusion.com/js/production/api/spreadsheet/save',
   beforeCellRender: function (args: CellRenderEventArgs) {
       // You can add your condition here for the required cell.
       if (args.cell && args.cell.notes === '') {
           args.cell.notes = null; // To remove the note element from the cell during initial rendering
       }
   },
   actionComplete: function (args) {
       if (args.action === 'addNote' && args.eventArgs && args.eventArgs.notes === '') { //To confirm the note's value is empty.
           let cellAddress: string = args.eventArgs.address.split('!')[1];
               let cellIndexes: number[] = getCellIndexes(cellAddress);
               let rowIdx: number = cellIndexes[0];
               let colIdx: number = cellIndexes[1];
               let cell: CellModel = getCell(rowIdx, colIdx, spreadsheet.getActiveSheet());
               cell.notes = null; // Assigned the note property value as null to prevent adding the note element in future actions.
               let cellEle: HTMLElement = spreadsheet.getCell(rowIdx, colIdx);
               let noteEle: HTMLElement = cellEle.querySelector('.e-addNoteIndicator');
               if (noteEle) {
                   cellEle.removeChild(noteEle); // Removed the added note element from the cell element.
               }
       }
   }
});

spreadsheet.appendTo('#spreadsheet'); 

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

Output:

Delete_notes_in_an_empty_cell.gif

For more information, please refer to the UG link mentioned below.

UG Link: Notes in EJ2 JavaScript Spreadsheet control | Syncfusion

Conclusion

We hope you enjoyed learning how to programmatically delete notes from an empty cell in a JavaScript spreadsheet.

You can refer to our JavaScript spreadsheet feature tour page to learn about its other groundbreaking feature representations and documentation and 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