My team uses multiple Google documents and spreadsheets for project management. We add comments to different sections and cells as we work.
I need to gather all these comments into one central spreadsheet so we can track outstanding comments from all our files. The plan is to run this automatically so our comment list stays current.
I built this script but I’m having trouble with the comment retrieval functions. The API docs aren’t clear on how to actually fetch the comments. Any suggestions?
function collectOpenComments() {
// Access the current sheet
var currentSheet = SpreadsheetApp.getActiveSheet();
var fileUrls = currentSheet.getRange("M2:M").getValues().flat();
// Use same sheet for results
var outputSheet = currentSheet;
// Clear previous data but keep column M intact
outputSheet.getRange(2, 1, outputSheet.getLastRow() - 1, outputSheet.getLastColumn() - 1).clearContent();
// Create column headers
outputSheet.getRange(1, 1).setValue("Link to Comment");
outputSheet.getRange(1, 2).setValue("Author");
outputSheet.getRange(1, 3).setValue("Comment Content");
var currentRow = 2;
for (var index = 0; index < fileUrls.length; index++) {
var fileUrl = fileUrls[index];
// Check file type from URL
if (fileUrl.includes('/docs.google.com/document/d/')) {
// Handle Google Document
var docId = fileUrl.split('/d/')[1].split('/')[0];
var doc = DocumentApp.openById(docId);
var docComments = DriveApp.getComments.list(docId).items;
} else if (fileUrl.includes('/docs.google.com/spreadsheets/d/')) {
// Handle Google Spreadsheet
var workbook = SpreadsheetApp.openByUrl(fileUrl);
var activeTab = workbook.getActiveSheet();
var sheetComments = DriveApp.getComments.list(docId).items;
} else {
// Skip unknown file types
Logger.log('Unknown file type: ' + fileUrl);
continue;
}
for (var commentIndex = 0; commentIndex < docComments.length; commentIndex++) {
var singleComment = docComments[commentIndex];
if (!singleComment.isResolved()) {
var authorEmail = singleComment.getUser().getEmail();
var commentBody = singleComment.getContent();
var directLink = singleComment.getUrl();
outputSheet.getRange(currentRow, 1).setValue(directLink);
outputSheet.getRange(currentRow, 2).setValue(authorEmail);
outputSheet.getRange(currentRow, 3).setValue(commentBody);
currentRow++;
}
}
}
}