Delete a note from a specific cell programmatically or automaticly if empty

Hi friends,

I am reaching out to inquire about the note functionality within the Spreadsheet JS component. 

I would like to know if there is a way to programmatically delete a note from a specific cell, using either the cell's address (such as 'A1') or the cell indices (for example, { row: 0, col: 0 }).


This capability would be highly beneficial for dynamic note management, particularly in scenarios where users add a note but do not enter any content, resulting in the red note symbol remaining visible even though the note is empty.

Image_4014_1720113692113

I would appreciate any information you can provide regarding whether this functionality is available, or any way to do it.


Thank you for your attention, and I look forward to your prompt response.


4 Replies 1 reply marked as answer

JS Janakiraman Sakthivel Syncfusion Team July 5, 2024 03:02 PM UTC

Hi Tomas De arco,

We have validated your reported requirement, and unfortunately, we would like to inform you that currently, we don't have a direct option to remove notes from a specific cell in our spreadsheet. However, you can achieve your requirement with a workaround at the sample level.

If the notes property of the cell is empty during initial rendering, you can prevent adding the red indicator to the cell with empty notes by assigning null to the notes property of args.cell in the beforeCellRender event, as shown in the shared code snippet below.

Additionally, while adding notes with an empty value via UI action, you can remove the red indicator element from the cell in our actionComplete event using the shared code snippet below.

For your reference, we have provided a sample below. In this example, we have fulfilled your requirement for cell 'B5'. If you add empty notes to cell 'B5', the red indicator will be removed, as per your request

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

CODE SNIPPET:


beforeCellRender: function (args: CellRenderEventArgs) {

        // You can add your condition here for the required cell.

        if ((args.address === 'B5') && 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];

            if (cellAddress === 'B5') { // You can add your condition here for the required cell.

                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.

                }

            }

        }

    }


Kindly check the details shared above on your end and get back to us if you need further clarifications regarding this.


Marked as answer

BA Bem Addun July 6, 2024 01:45 PM UTC

To delete a note from a specific cell programmatically or automatically if it is empty, you can use the ClearContents method in Google Apps Script. Here's an example of how you can achieve this:

javascript
function deleteNoteIfEmpty() { var sheet = SpreadsheetApp.getActiveSheet(); var cell = sheet.getRange("A1"); // Replace "A1" with the desired cell reference if (cell.getNotes() === "") { cell.clearNotes(); } }

Here's how the code works:

  1. The getActiveSheet() method is used to get the currently active sheet in the Google Spreadsheet.
  2. The getRange() method is used to specify the cell reference from which you want to delete the note. In this example, it's set to "A1", but you can replace it with the desired cell reference.
  3. The getNotes() method is used to retrieve the note content of the specified cell. If the note is empty, it will return an empty string.
  4. An if statement checks if the note content is an empty string. If it is, the clearNotes() method is called to delete the note from the cell.

To run this script, you can follow these steps:

  1. Open the Google Spreadsheet where you want to delete the note.
  2. Go to "Tools" > "Script editor" in the Google Spreadsheet menu.
  3. Replace the existing code in the script editor with the code provided above.
  4. Save the script by clicking on the disk icon or pressing Ctrl+S (Windows) or Cmd+S (Mac).
  5. Close the script editor.
  6. Go back to the Google Spreadsheet.
  7. The script will automatically run and delete the note from the specified cell if it is empty.

Note: If you want to run the script manually, you can click on the "Run" button in the script editor or create a custom menu item that triggers the script.Remember to replace "A1" with the desired cell reference where you want to delete the note if it is empty.






Temu, an online shopping platform, is offering significant discounts through two prominent coupon codes: ACT200019 and ACP856709. 

  1. Download the Temu app, available for both iPhone and Android devices.

  2. Browse through the wide range of products available on the platform.

  3. Add desired items to your cart.

  4. During the checkout process, enter either ACT200019 or ACP856709 in the coupon code field.

  5. Apply the code to see the discount reflected in your total.



TD Tomas De arco July 6, 2024 02:20 PM UTC

Thank you so much @Janakiraman Sakthivel



FL Florence Lilian Awino Syncfusion Team July 8, 2024 05:48 AM UTC

Hi Tomas,

We're glad your issue was resolved. Please get back to us for further assistance in the future.

Regards,

Florence L.



Loader.
Up arrow icon