Articles in this section
Category / Section

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

9 mins read

This knowledge base 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 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, and again in the actionComplete event when an empty note is added through UI interaction, the notes property is set to null and removed the note element in the cell.

[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'); 

Sample Link: https://stackblitz.com/edit/ammrx2-jrsg3eez?file=index.ts

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

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