I have been using this forum to get assistance writing a program that exports comments made on a Google Doc to a Google Sheet. The program is working as intended, but I have recently come across a new issue when trying to export a large number of comments. Google script appears to only be able to export 20 comments by default.
I posted this question online, and it was suggested to me to try increasing the number of comments that can be returned. However, the Google Apps script appears to have a limit of 100 comments, and my project needs to export far more than 100 comments. It was also suggested that I should try using page tokens to accomplish this.
My original post can be found here: How to export more than 20 comments on a Google Doc to a Google Sheet
I have read through the answers and spent some time researching page tokens, along with reviewing several posts of other people trying to integrate page tokens into their programs.
Unfortunately, I was unable to figure out how to get page tokens to work for my code. I was hoping someone with more experience would be able to either explain how page tokens work in a "dumbed down" way for someone who has little coding experience, or if they could provide suggestions on how to update my code to utilize page tokens.
My current code is listed below:
function listComments() {
// Initialize Variables
var docId = '1h-BJrklSFgfGNCzdAWziNx5b5EDRjHHJ3DTkXHNhtUE';
var comments = Drive.Comments.list(docId, {fields: "*", maxResults: 100});
var sList = [], hList = [], cList = [], nList = [], dList = [];
// Get list of comments
if (comments.items && comments.items.length > 0) {
for (var i = 0; i < comments.items.length; i++) {
var comment = comments.items[i];
// add comment and highlight to array's first element
sList.unshift([comment.status]);
hList.unshift([comment.context.value]);
cList.unshift([comment.content]);
nList.unshift([comment.author ? comment.author.displayName : '']);
dList.unshift([comment.createdDate ? new Date(comment.createdDate).toLocaleString() : '']);
}
// Update spreadsheet values
var sheet = SpreadsheetApp.openById('1XNJdSRMB1KR9_gpzbd6_rIC6WYpPoExuWkb12Nd1tRI').getSheetByName('Comments');
sheet.getRange("A2:A" + (sList.length + 1)).setValues(sList);
sheet.getRange("B2:B" + (hList.length + 1)).setValues(hList);
sheet.getRange("C2:C" + (cList.length + 1)).setValues(cList);
sheet.getRange("D2:D" + (nList.length + 1)).setValues(nList);
sheet.getRange("E2:E" + (dList.length + 1)).setValues(dList);
}
}
Any assistance would be greatly appreciated!
From if (comments.items && comments.items.length > 0) {
in your showing script, I guess that you are using Drive API v2. In this case, how about the following modification?
function listComments() {
// Initialize Variables
var docId = '1h-BJrklSFgfGNCzdAWziNx5b5EDRjHHJ3DTkXHNhtUE';
var comments = [];
var pageToken = "";
do {
var res = Drive.Comments.list(docId, { includeDeleted: false, maxResults: 100, fields: "*", pageToken });
comments = [...comments, ...res.items];
pageToken = res.nextPageToken;
} while (pageToken);
if (comments.length == 0) return;
var sList = [], hList = [], cList = [], nList = [], dList = [];
// Get list of comments
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
// add comment and highlight to array's first element
sList.unshift([comment.status]);
hList.unshift([comment.context.value]);
cList.unshift([comment.content]);
nList.unshift([comment.author ? comment.author.displayName : '']);
dList.unshift([comment.createdDate ? new Date(comment.createdDate).toLocaleString() : '']);
}
// Update spreadsheet values
var sheet = SpreadsheetApp.openById('1XNJdSRMB1KR9_gpzbd6_rIC6WYpPoExuWkb12Nd1tRI').getSheetByName('Comments');
sheet.getRange("A2:A" + (sList.length + 1)).setValues(sList);
sheet.getRange("B2:B" + (hList.length + 1)).setValues(hList);
sheet.getRange("C2:C" + (cList.length + 1)).setValues(cList);
sheet.getRange("D2:D" + (nList.length + 1)).setValues(nList);
sheet.getRange("E2:E" + (dList.length + 1)).setValues(dList);
}