Google 用于检索文件夹中所有文档的评论、突出显示的文本和文档名称的表格脚本

Google Sheets Script to retrieve comment, highlighted text, and document name for all documents in a folder

我正在为 Google 工作表编写脚本以执行以下功能:

  1. 查看特定文件夹中的所有文档('this folder')
  2. 对于每个文档,查看评论
  3. 对于每条评论,检索文档的名称、突出显示的文本和评论的内容
  4. 在电子表格中追加一行,其中包含文档名称、突出显示的文本和评论内容

我写了一个代码来执行#2、#3 和#4(即查看评论和收集评论数据),但这只适用于一个文档。

function listComments() {  
 
var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  sheet.appendRow(["Interview", "Highlight", "Code"]);

var docId = '[document title]'; 

  var comments = Drive.Comments.list(docId);

  if (comments.items && comments.items.length > 0) {
    for (var c = 0; c < comments.items.length; c++) {
      var comment = comments.items[c];  

            data = [
                comment.fileTitle,
                comment.context.value,
                comment.htmlContent,
            ]
        sheet.appendRow(data);
    }
  }
}

下面是我尝试改编的代码,要求函数查看文件夹中的所有文档,并对所有文档执行此操作。但是,它不起作用。

我认为它可能在某处陷入循环?

function listComments() {      
var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  sheet.appendRow(["Interview", "Highlight", "Code"]);

var folder = DriveApp.getFolderById("[this folder]");
var contents = folder.getFiles();

var file

    if (contents.items > 0) {
    for (var i = 0; i < contents.items.length; i++) {
        file = contents[i];
        
            while (file.hasNext()) {
                var file = contents.next();
                var comments = Drive.Comments.list(file);

                    if (comments.items && comments.items.length > 0) {
                    for (var c = 0; c < comments.items.length; c++) {
                    var comment = comments.items[c];  

                        data = [
                            comment.fileTitle,
                            comment.context.value,
                            comment.htmlContent,
                        ]
                    sheet.appendRow(data);
                    }
                    }                   
            }
    }
    }
}

非常感谢您提供的任何帮助或见解!

这应该有效:

function listComments() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  sheet.appendRow(["Interview", "Highlight", "Code"]);

  var folder = DriveApp.getFolderById("[this folder]");
  var files = folder.getFiles();

  while (files.hasNext()) {
    var file = files.next();
    var comments = Drive.Comments.list(file.getId());

    if (comments.items && comments.items.length > 0) {
      for (var comment of comments.items) {
        data = [
          comment.fileTitle,
          comment.context.value,
          comment.htmlContent,
        ]
        sheet.appendRow(data);
      }
    }
  }
}

如果您有很多文件和很多评论,此实现会运行得更快:

function listComments_improved() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = [["Interview", "Highlight", "Code"]]; // built a 2d array

  var files = DriveApp
    .getFolderById("[this folder]")
    .getFiles();

  while (files.hasNext()) {
    var comments = Drive.Comments.list(files.next().getId());
    if (!comments.items) continue;
    if (comments.items.length == 0) continue;
    comments.items.forEach(comment => {
      data.push([                                // fill the array with rows
        comment.fileTitle, 
        comment.context.value, 
        comment.htmlContent
        ]);
    });
  }

  sheet.clear()
    .getRange(1,1,data.length,data[0].length)
    .setValues(data);                            // set the array to the sheet
}