listFileComments(fileId, opts) pages through a file's Drive comments and returns one [author, created, content, quotedText, link] row per comment. By default it skips resolved comments; pass {includeResolved: true} to keep them. This uses the Drive advanced service (Drive API v3), not DriveApp.
When to use
- Pulling open comments on a spreadsheet into a to-do tab
- Auditing comment threads on a Doc or Slide deck
- Any script that needs author, date, quoted text, and a link back to the thread
How to use this snippet
Enable the Drive API advanced service in the Apps Script editor (and in the Cloud project the first time you are asked). Then:
/**
* List Drive comments on a file as [author, created, content, quotedText, link] rows.
* Pages with Drive.Comments.list (Drive API v3). fields is always sent (required
* by the comments API). Resolved comments are skipped unless
* opts.includeResolved is true. A missing resolved field is treated as open.
* created is a Date. The link is the file's webViewLink with disco=commentId
* so Sheets, Docs, and Slides can open that thread.
*
* @param {string} fileId
* @param {{includeResolved?: boolean}} [opts]
* @return {Array<Array>}
*/
function listFileComments(fileId, opts) {
opts = opts || {};
var includeResolved = !!opts.includeResolved;
var id = String(fileId == null ? '' : fileId).trim();
if (!id) throw new Error('listFileComments: fileId required');
if (typeof Drive === 'undefined' || !Drive.Comments || !Drive.Comments.list) {
throw new Error('listFileComments: enable the Drive advanced service (Drive API v3)');
}
var webViewLink = '';
try {
var file = Drive.Files.get(id, { fields: 'webViewLink' });
webViewLink = (file && file.webViewLink) || '';
} catch (e) {
webViewLink = '';
}
function commentLink_(commentId) {
if (!commentId) return webViewLink || '';
var base = webViewLink || ('https://drive.google.com/file/d/' + id + '/view');
var join = base.indexOf('?') >= 0 ? '&' : '?';
return base + join + 'disco=' + encodeURIComponent(commentId);
}
var fields = 'comments(id,author(displayName,emailAddress),content,createdTime,quotedFileContent,resolved),nextPageToken';
var token = null;
var rows = [];
do {
var params = { pageSize: 100, fields: fields };
if (token) params.pageToken = token;
var res = Drive.Comments.list(id, params);
var comments = (res && res.comments) || [];
for (var i = 0; i < comments.length; i++) {
var c = comments[i] || {};
if (c.resolved && !includeResolved) continue;
var authorObj = c.author || {};
var author = String(authorObj.displayName || authorObj.emailAddress || '').trim();
var created = c.createdTime ? new Date(c.createdTime) : '';
var content = c.content == null ? '' : String(c.content);
var quoted = '';
if (c.quotedFileContent && c.quotedFileContent.value != null) {
quoted = String(c.quotedFileContent.value);
}
rows.push([author, created, content, quoted, commentLink_(c.id)]);
}
token = (res && res.nextPageToken) || null;
} while (token);
return rows;
}
Example
function refreshCommentTodo() {
var ss = SpreadsheetApp.getActive();
var rows = listFileComments(ss.getId()); // open comments only
var tab = ss.getSheetByName('Comment to-do') || ss.insertSheet('Comment to-do');
tab.clearContents();
var out = [['Author', 'Created', 'Comment', 'Quoted text', 'Link']].concat(rows);
tab.getRange(1, 1, out.length, 5).setValues(out);
}
Notes
{includeResolved}is our filter, not a Drive API parameter. The API returns both; we drop resolved rows unless you ask for them.resolvedis often absent on a comment that has never been resolved. This helper treats a missing value as open.quotedTextis the selection at the time the comment was made, not a live cell value.- This lists Drive comments on the file, not the old yellow cell notes (
Range.getNote()). - The first run from the editor prompts for Drive and Sheets access. A time-driven trigger cannot finish that consent for you.
Happy Coding!
