0

I am using this very helpful script (below) from a previous question (Export Google Docs comments into Google Sheets, along with highlighted text?) to retrieve comments (and the associated highlighted text) from a Google Doc and put it into a Google Sheet.

I want to add a third column that retrieves the relevant section header (Heading1) for the highlighted text. For example, if the comment and highlighted text are in Section 3 of the document, I want to retrieve the section heading "Section 3" and have it appear in a third column.

function listComments() {
  // Change docId into your document's ID
  // See below on how to
  var docId = '14oWkmw0GJ_RqNZVpPyFH5e0Mn7u1-5J-1v-lG1va9kk'; 
  var comments = Drive.Comments.list(docId);
  var hList = [], cList = [];

  // 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 
      hList.unshift([comment.context.value]);
      cList.unshift([comment.content]);
    }
    // Set values to A and B
    var sheet = SpreadsheetApp.getActiveSheet();
    sheet.getRange("A1:A" + hList.length).setValues(hList);
    sheet.getRange("B1:B" + cList.length).setValues(cList);
  }
}

I would appreciate any help!

Thanks, Chloe

chloe
  • 23
  • 4
  • The method "Drive.Comments.list(docID)" from the existing code returns a json value but it only contains the highlighted text and the actual comment.The existing code won't be able to retrieve the heading from the document. Alternatively, you can refer to this existing answer from https://stackoverflow.com/a/30202004 to retrieve the heading of a document via a DocumentApp class instead of using the Drive API. – SputnikDrunk2 Jul 26 '21 at 15:54
  • Thank you! I will review this - the other option I am considering, to avoid this issue, is asking the function to go through separate documents and retrieve the document title instead of the header. I've posted another question with a different code I've been working on for this. – chloe Jul 27 '21 at 03:46

0 Answers0