Google Apps Script Spreadsheet Comment Automation
Using Google Drive API in Google Apps Script, we could create Comments in a Google Document.
And after a test, I confirm you can also do it in a Spreadsheet (which is normal because Comments depends only on Drive API)
function insertDriveComment(fileId, comment, context) {
var driveComment = {
content: comment,
context: {
type: 'text/html',
value: context
}
};
Drive.Comments.insert(driveComment, fileId);
}
Keep in mind that you cannot attach programmatically the comment to a cell (or to words in Google Document), because anchors for Document and Spreadsheet Comments are proprietary (check the video at the bottom of the page here)
Hope it could help.
Updated answer for dic 2018:
Now Google supports the setNote(String) and setNotes(Object[]) method in the Range class.
the getComment() and setComment() methods no longer exist.
There is no way to manipulate Comments via Spreadsheet Services - see Issue 36756650. (Star it if you wish.)
Instead, all the "Comments" methods work on Notes.
The following method will append a new "Modified" timestamp to any existing one - not quite as nice looking as an actual Comment would be, unfortunately.
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var cell = sheet.getActiveCell();
var comments = cell.getComment();
// Newline works in msgBox, but not in Note.
comments = comments + "\\nModified: " + (new Date());
//Browser.msgBox(comments);
cell.setComment(comments);
}
Now you can use setNote() and getNote()
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var cell = sheet.getActiveCell();
var comments = cell.getNote();
cell.setNote(comments);