1

Could you please help me with an issue I am having adding comments on Google Doc? I have done all the work required in retrieving comments from Google Drive. This is how I retrieve comments from Google Doc:

String fileId ='1up_-hN_z6Fv4NuPUDZioIzFgk42o4j6n8'; 

String commentId = "AAAAp8ow"; 

googleUser = await _googleUserSignIn.signIn(); 

final authHeaders = await googleUser!.authHeaders; 

final client = GoogleHttpClient(authHeaders); 

print(' passed 1 '); 

final marcoDrive = drive.DriveApi(client); 

var  result = await marcoDrive.comments.get(fileId, commentId, $fields: 'content'); 

The screenshot below shows the debugging results:

enter image description here

As you can see on the screenshot above, the results variable contains a comment data fetch from Google Doc. The screenshot below shows Google Doc and the orange circle is the comment: enter image description here

My issue is creating a comment on Google Doc. I find the Comment request parameter, highlighted below using an orange arrow, problematic as I am struggling to create a comment in Google Doc.

var result2 = await marcoDrive.comments.create(request, fileId, $fields: 'content: data is data');

enter image description here

Could you please help me to understand what the Comment request parameter means and how to use it? I would also appreciate it if you could help me understand how to anchor comments.

The programming language I'm using is Dart. Here is a link that I used to help me understand how to manage Google Drive Doc comments: (https://developers.google.com/drive/api/v3/manage-comments).

Filburt
  • 17,626
  • 12
  • 64
  • 115

1 Answers1

1

You have to supply a Comment resource as your request body, as you can see at the official docs: Comments.create.

Here are the fields corresponding to the Comment resource. The only required field is content.

The JSON representation would be something like:

{
  "content": "YOUR COMMENT"
}

In Dart, it would probably be something along the following lines:

Comment request = Comment();
request.content = "YOUR COMMENT";
var result = await marcoDrive.comments.create(request, fileId, $fields);

Library reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Thank you so much for you help and greatly appreciated!!! – Adan Tabari Aug 05 '21 at 11:06
  • import 'package:googleapis/drive/v3.dart' as drive; var request = drive.Comment(); request.content = 'Law of Conservation of Energy'; var result2 = await marcoDrive.comments.create(request, fileId, $fields: 'content'); – Adan Tabari Aug 05 '21 at 11:12
  • @Lamblichus, If I want to anchor a specific word in a sentence, do I use the same way I used for request.content but instead request.anchor? – Adan Tabari Aug 05 '21 at 11:34
  • For example: request.content = 'Energy != thermodynamics'; request.anchor ='Testing'; var result2 = await marcoDrive.comments.create(request, fileId, $fields: 'content'); – Adan Tabari Aug 05 '21 at 11:35
  • @AdanTabari Unfortunately, comments created via API cannot be anchored to a specific part of the Google Docs (this parameter is used for non-Google Docs) (see, for example, [this answer](https://stackoverflow.com/a/67019897)). – Iamblichus Aug 05 '21 at 11:55