Spreadsheet Comments

Adding comments in the spreadsheet cells is part of the JSS native methods. This section covers the related methods and events for interacting with the spreadsheet comments.

Documentation

Methods

The following methods can be used to get or set comments in one or multiple cells.
MethodDescription
getComments Get comments from a cell or from the whole table.
spreadsheet.getComments(cellName: String | null)
@Param mixed - cell identification or null for the whole table.
setComments Add comment to one or multiple cells.
spreadsheet.setComments(cellName: Mixed | Array, comments: String=)
@param {string|Object} - Identification of a cell or an array of objects with multiple comments
@param {string=} comments - Comments to be added to the cell


Events

The onbeforecomments can be used to intercept, change or cancel the result of any comments applied by the user.
EventDescription
onbeforecommentsonbeforecomments(worksheet: Object, newValue: Array) : Array | null | false
The method should return an array with overwritten values, false to cancel the action or null to continue with the user action.
oncommentsoncomments(worksheet: Object, newValue: Array, oldValue: Array) : void


Initial Settings

The following properties are available through the initialization of the online spreadsheet.
PropertyDescription
allowComments: booleanEnable the user to enter new comments on cells.
comments: arrayArray with the initial comments.



Examples

The following example shows how to start the spreadsheet with a few initial comments and allow users to edit or insert new comments using the context menu.




Interact with the cell comments programmatically

To apply comments via JavaScript, you can use the methods setComments or getComments, as follow:




Source code

<html>
<script src="https://jspreadsheet.com/v9/jspreadsheet.js"></script>
<link rel="stylesheet" href="https://jspreadsheet.com/v9/jspreadsheet.css" type="text/css" />
<script src="https://jsuites.net/v4/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v4/jsuites.css" type="text/css" />

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />

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

<script>
// Set your JSS license key (The following key only works for one day)
jspreadsheet.setLicense('MGU1NDUxYjUwZjY4NjEyZGY0MjI0OWVlNWU5MTZjMzFjOGViNWM4OTUwNDllNDQ1YjlkNWFkOWI0ZGQ2MGEwM2RmZGQ2YTRiMTY3Yzk4MDhiNjNhZjI3ZmQzOTFlMGI3YjI0ZjY4MjY1NjUxYWM0ZjA5Y2UxMjUxZmQyZjA2ZmIsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UWTJNREk0TURZMk5pd2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWJHOWpZV3hvYjNOMElsMHNJbkJzWVc0aU9pSXpJaXdpYzJOdmNHVWlPbHNpZGpjaUxDSjJPQ0lzSW5ZNUlpd2lZMmhoY25Seklpd2labTl5YlhNaUxDSm1iM0p0ZFd4aElpd2ljR0Z5YzJWeUlpd2ljbVZ1WkdWeUlpd2lZMjl0YldWdWRITWlMQ0pwYlhCdmNuUWlMQ0ppWVhJaUxDSjJZV3hwWkdGMGFXOXVjeUlzSW5ObFlYSmphQ0pkTENKa1pXMXZJanAwY25WbGZRPT0=');

// Create the spreadsheet
var worksheets = jspreadsheet(document.getElementById('spreadsheet'), {
    worksheets: [
        {
            data: [
                ['US', 'Cheese', '2019-02-12'],
                ['CA', 'Apples', '2019-03-01'],
                ['CA', 'Carrots', '2018-11-10'],
                ['BR', 'Oranges', '2019-01-12'],
            ],
            columns: [{
                width: '300px',
            }],
            allowComments: true,
            comments: {
                B1:'Initial comments on B1',
                C1:'Iniatial comments on C1'
            },
        }
    ],
    oncomments: function() {
        console.log(arguments);
    }
});
</script>

<button type="button" onclick="worksheets[0].setComments('A1', 'Test');">
Set A1 comments
</button>

<button type="button" onclick="alert(worksheets[0].getComments('A1'));">
Get A1 comments
</button>

<button type="button" onclick="worksheets[0].setComments('A1', '');">
Reset A1 comments
</button>

</html>



Related content

Advanced comments extension

It is possible to create multiple comments in a cell with the user identification using the advance comments extension available in the Premium edition.

Click here to understand more about that.