1

Fellow Stackers. I'm using a Google Apps Script to (a) capture all "Comments" in a Google Document... enter image description here ...and (b) list them in a column of a Google Sheet...

enter image description here

However, I'm wondering if it's possible to...

(1) Array "Comments" into individual cells down a column in my Sheet rather than into a single column, as I have now. This is the bit of GAS I'm currently using to grab comment contents:

 var comments = JSON.parse(Drive.Comments.list(id));
 var items=comments.items;

 var string = "";
  for(var i in items){
    string+='\n';
    string+=items[i].content;
  }

(2) Order the "Comments" by anchor position in my Google Document—i.e. the comment anchored highest in the doc would appear in the first cell of the Sheet's column.

(3) Also include "Suggested Edits" from my Google Document alongside the comments. Can those be accessed via API yet?

Thanks in advance to anyone who may be able to help!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Greg
  • 1,264
  • 14
  • 21
  • please do also include what you tried on each case as currently there is no such code so we can tell you why its not working. – Zig Mandel May 21 '15 at 19:40
  • Sorry for my uncouth approach. I've updated my question to include screenshots and a bit of problematic code for question No. 1. For question Nos. 2-3, I unfortunately did not know where to begin, so I've no "relevant" code to add. In hindsight, I realize this forum is not the place for those kinds of philosophical questions, though, so I will refrain from asking such going forward. – Greg May 22 '15 at 13:37
  • Good edit, Greg. It's quite a good question now, and likely to help others in the future. Don't be afraid to ask the philosophical, especially when it's so closely related to the practical parts - again, it's something that can help others who may be wondering the same thing. The worst that will happen is "the community" will nudge you to modify it or take it to another forum. – Mogsdad May 22 '15 at 13:46

1 Answers1

3

(1) Array "Comments" into individual cells down a column in my Sheet rather than into a single column, as I have now.

This bit of code is taking an array of comments, and concatenating them into a single string:

  var string = "";
  for(var i in items){
    string+='\n';
    string+=items[i].content;
  }

To be able to put each comment into a separate cell in a column, you need to change that array into a 2-dimensional array, with each of the original elements in its own "row". Something like:

  var data = [];           // start with an empty array
  for (var i=0; i<items.length; i++) {
    var item = items[i];   // current comment
    // A row is an array of cells
    var row = [item.htmlContent,item.author.displayName,item.createdDate];
    data.push(row);        // Add this row to the data array
  }

This line writes the content of a single cell, albeit using setValues() which can fill a rectangular range:

var targetRange = sheet.getRange(lastRow+1,1,1,1).setValues([[string]]);

With the 2-D array created earlier, you can append to the sheet like so:

var targetRange = sheet.getRange(lastRow+1,1,data.length,data[0].length);
targetRange.setValues(data);

Result:

function driveApiComment(id){

  var comments = JSON.parse(Drive.Comments.list(id));
  var items=comments.items;

  var data = [];           // start with an empty array
  for (var i=0; i<items.length; i++) {
    var item = items[i];   // current comment
    // A row is an array of cells
    var row = [item.htmlContent,item.author.displayName,item.createdDate];
    data.push(row);        // Add this row to the data array
  }

  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheets()[0];
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1,1,data.length,data[0].length);
  targetRange.setValues(data);

}

(2) Order the "Comments" by anchor position in my Google Document—i.e. the comment anchored highest in the doc would appear in the first cell of the Sheet's column.

You're out of luck (for now, at least). See:

Summary: Google's anchors are not decipherable. (Likely they are a key to a hidden database that includes the actual line & char refs to your document, along with your social security number and mother's maiden name.) You could retrieve them & sort them alphabetically... but that would have no relation to where the comments appear in a document.

(3) Also include "Suggested Edits" from my Google Document alongside the comments. Can those be accessed via API yet?

Nope.

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Thank you very much for taking the time to offer your insight, Mogsdad. Your response is as helpful as it is thorough. Again, apologies for the structure of my initial questions; your patience has not gone unnoticed. All the best. – Greg May 22 '15 at 13:40
  • Thanks! Glad to help - fun question. – Mogsdad May 22 '15 at 13:48