I have a google drive file with hundreds of comments and im looking to extract them in a google script and print the results (comments.list) to a google sheet
I know step 1 is to create a loop and obtain comments.list, similar to Get more than 100 Google Drive Comments using nextPageToken
After this though, printing to the google sheet is giving me trouble. I can't seem to obtain the entire paginated list and store in an array properly to send to the sheet. I've gotten close, but it either: 1) will only print the last page of comments to the sheet, 2) iterate through each page and overwrite the previous write.
Edit: Seems like I need to store the response page by page into a single, concatenated array, and then iterate through this to render the comments. Any advice for concatenating the response, storing it in single array, etc.
Update w/ answer: Drive API 2 was giving me trouble.. the below is now working
Here's my code
function retrieveComments(fileId) {
var hList = [], cList = [];
var comments = [];
var sheet = SpreadsheetApp.getActiveSheet();
var token = "";
var fileId = '1C4-VtJN-molEb7KEIPw4BUtOC3CGm3oeSSdQ6pDe5XI';
do {
var callArguments = {'maxResults': 100, 'pageToken': token}
//'fields': 'items(commentId,content,context/value,fileId)'
let resp = Drive.Comments.list(fileId,callArguments);
token = resp.nextPageToken;
for (let i = 0; i < resp.items.length; i++) {
let item = resp.items[i];
comments.push([item.content, item.context]);
// Logger.log(comments)
}
if (token == '') {
Logger.log('That was the last ' + resp.items.length + '. Your Done!')
// ss.toast('That was the last ' + resp.items.length + '. Your Done!');
} else {
// Logger.log(+ resp.nextPageToken + '. Keep Going!')
Logger.log('Another ' + resp.items.length + '. Keep Going!')
// ss.toast('Another ' + resp.items.length + '. Keep Going!');
}
} while (token)
Logger.log (comments);
Logger.log ('Writing to sheet...');
if (comments && comments.length > 0) {
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
// add comment and highlight to array's first element
hList.unshift([comment[0]]);
cList.unshift([comment[1].value]);
sheet.getRange("A1:A" + hList.length).setValues(hList);
sheet.getRange("B1:B" + cList.length).setValues(cList);
}
}
// now lets set the headers
Logger.log ('Now lets add the headers');
function prependRow(sheet, rowData) {
var rowData = [
[ "Code", "Data" ]
];
sheet.insertRowBefore(1).getRange(1, 1, 1, rowData.length).setValues([rowData]);
}
Logger.log ('Done writing to the sheet!');
}