Sub topic 14
Automating Notes and Listing Comments with Scripts
This chapter assumes that you know basic Google Sheets navigation, can open the Apps Script editor, and understand the difference between a spreadsheet file and a cell range. Apps Script is Google’s JavaScript-based automation environment for Google Workspace.
Notes and comments use different APIs
A note is text attached to a cell. It is useful for permanent background information, such as a data source or a calculation reminder. Apps Script accesses notes through a cell’s Range object.
A comment is a discussion thread attached to a file or cell. People can reply, mention collaborators, resolve the thread, and sometimes assign a task. The standard SpreadsheetApp service can work with notes, but it does not provide methods for threaded comments.
Use the Drive advanced service when a script needs to read comments. This service exposes the Google Drive API to Apps Script.
Create and read a cell note
The following function writes a note to cell B2 on the active sheet and then reads it back.
function addReviewNote() {
const sheet = SpreadsheetApp.getActiveSheet();
const cell = sheet.getRange('B2');
cell.setNote('Check this figure against the Q3 report.');
const note = cell.getNote();
Logger.log(note);
}setNote() replaces the cell’s existing note. If the cell has no note, getNote() returns null. To remove a note, use cell.clearNote().
A note is associated with the cell, so copying or moving the cell can affect where the note appears. A note does not create a reply thread or notify another person.
Enable the Drive advanced service
Before running a comment-listing function:
- Open the spreadsheet’s Extensions > Apps Script editor.
- In the editor, open Services.
- Add the Google Drive API advanced service.
- If Apps Script asks for it, enable the Google Drive API in the linked Google Cloud project.
- Authorize the script when you run it.
The script needs permission to read the spreadsheet file and its comments. A user who lacks access to the file cannot use the script to bypass that restriction.
List comments and their status
This function gets the active spreadsheet’s file ID and lists each comment’s author, content, and resolved status.
function listComments() {
const fileId = SpreadsheetApp.getActive().getId();
const result = Drive.Comments.list(fileId, {
fields: 'comments(content,resolved,author/displayName)'
});
(result.comments || []).forEach(function(comment) {
const author =
comment.author && comment.author.displayName
? comment.author.displayName
: 'Unknown author';
const status = comment.resolved ? ' (resolved)' : '';
Logger.log(author + ': ' + comment.content + status);
});
}The fields option is important. It tells the API which properties to return and avoids requesting unnecessary data. The resolved value is a Boolean: true means the discussion is resolved, while false means it remains open.
This example lists comments returned in one response. For a large file, inspect the response’s pagination fields and request additional pages. Also remember that comment content may be unavailable for deleted users or comments that your account cannot access.
When to automate
Automation helps when a review process repeatedly adds standard cell guidance or creates an audit report of open discussions. It does not replace sharing permissions, human replies, or the Sheets interface for resolving threads.
Exercise
Which service should a script use to write a note to cell B2, and which service should it use to list threaded comments?
Show answer
Use SpreadsheetApp and the cell Range methods for the note. Use the Drive advanced service, after enabling the Google Drive API, to list threaded comments.