Search code examples
javascriptjsonapigoogle-apps-scriptdrive

How to specify request params with Drive.Comments.list


I'm able to pull back the default 20 comments with the following code when I specify a single fileId param. But I would actually like to pull back a hundred or for curiousity's sake paginate to the next 20.

From my code below, in the getComments function, when i specify options as the param to Drive.Comments.list it renders an error. When I specify the fileId directly it returns 20.

It should be a simple fix - I'm a bit of rookie. Any suggestions? Please and thanks!

function main() {
  var fileId = getFileId();
  var fileComments = getComments(fileId);
//  logObj(fileComments);
  //Logger.log(fileComments);//.items.length); // this always says 20 
//  logObj(fileComments);
  logObj(fileComments.items);
}

function getFileId() {
  return DocumentApp.getActiveDocument().getId(); 
}
function getComments(fileId) {
  var options = {
    'fileId': fileId,
    'pageSize': 99  
  };

  var commnts =  Drive.Comments.list(fileId);

  return commnts;
  //Logger.log(cmnts.items.length);
}
function logObj(obj){
  for (key in obj) {
    var tmp =  obj[key];
    Logger.log(key + " = " + tmp );
  }
}

function findComments(criteria){

}

Solution

  • The Drive.Comments.list() will take 2 parameters. (fileId, {options}). Also, the option for result limits is maxResults (0 to 100). Use the result commnts.nextPageToken with the options.pageToken to page through large data sets

    function getComments(fileId) {
      var options = {
        'maxResults': 99  
      };
    
      var commnts =  Drive.Comments.list(fileId, options);
    
      return commnts;
      //Logger.log(cmnts.items.length);
    }