1

I hope everyone is in good health. I am new to AppScript and love the concept of automating the Google Docs using it.

What I tried;
I recently got into a problem where I needed to update the text according to its comment in Google Docs using AppScript.
Here is the demoenter image description here but I came to know that it was not possible by using AppScript, so I have to use Google Drive API and I tried it but I still didn't got successful. It did seem to be changed when I try console.log(comments.items[i].context.value); but everything was same in my Google Doc, nothing seem to be changed.

Here is my code;

function listComments() {
  let comments = Drive.Comments.list(DocumentApp.getActiveDocument().getId());

  if (comments.items && comments.items.length > 0) {
    for (let i = 1; i < comments.items.length; i++) {
      let comment = comments.items[i].content;
      let text = comments.items[i].context.value;
      comments.items[i].context.setValue(`[${text}](${comment})`);
      console.log(comments.items[i].context.value);
    }
  } else {
    Logger.log('No comment found.');
  }
}

I can't find the documentation anywhere for updating the Comment Context Value in AppScript which will also reflect in my Google Doc.
I hope you will be able to address my problem. Thanks in Advance !

abdulsamad
  • 158
  • 1
  • 10

1 Answers1

0

Upon checking Drive API, I did not find any methods to update the comment's context.value properties. When updating comments, you need to use Comments.update(). However, this method only lets you modify/update the comment's content. If you are trying to replace a specific string in the Google Docs based on the comment's content, You need to update the document using Document Service in Apps Script


When updating an existing comment you need to provide the following parameters:

commentId - The ID of the comment.

fileId - The ID of the file.

commentId is available in the Comments.list response

Sample:

 {
   "kind": "drive#comment",
   "commentId": "sampleCommentId",
   ....
   },
   "htmlContent": "Test Name",
   "content": "Test Name",
   "deleted": false,
   "status": "resolved",
   "context": {
    "type": "text/html",
    "value": "name"
   },

Example:

enter image description here

content = "Test Name"

context.value = "name"

If I will update this comment using Comments.update() with the following request body:

{
  "content": "My Name",
  "context": {
    "value": "name"
  }
}

Output:

enter image description here

Ron M
  • 5,791
  • 1
  • 4
  • 16
  • I don't want to update comment The only wanted to update the text associated with it in the doc and as you mentioned to update Doc I would need to use Document Service but how would I came to know which text is associated with which comment – abdulsamad Dec 29 '20 at 17:32
  • I could copy the context.value and search the doc file and replace the string with that text I want but their could be multiple same strings – abdulsamad Dec 29 '20 at 17:33
  • No. I tried modifying only the context value but it did not reflect to the doc file. Based on the reference document update() will only update the content of the comment. – Ron M Dec 29 '20 at 17:55
  • Regarding your initial concern in finding the context value location in the doc. Please refer here https://stackoverflow.com/questions/58983822/location-of-a-comment-in-its-corresponding-google-doc – Ron M Dec 29 '20 at 17:55
  • Yes, I also tried changing it, I thought it was because of some permission issues and you might get different results but it seems like context value does not get updated – abdulsamad Dec 29 '20 at 18:07
  • that's right, it is not possible to update the context value – Ron M Dec 29 '20 at 18:10
  • thanks for guiding me , Stay happy I think I have to use something other than comments to solve my problem – abdulsamad Dec 29 '20 at 18:31
  • @abdulsamad you can combine what you have with `replaceText` instead, the catch is it replaces all occurrences of the text you want replaced. `var docBody = DocumentApp.getActiveDocument().getBody();` `docBody.replaceText(comment.context.value, comment.content); ` It works when you are sure the text you want substituted is occurring only once – NightEye Dec 29 '20 at 18:35
  • Yes but as I mentioned earlier that there could be more than one occurrences – abdulsamad Dec 29 '20 at 18:42
  • @abdulsamad, currently it is not possible to locate the comment in Google Docs. There are existing feature request regarding this. Refer [here](https://stackoverflow.com/questions/58983822/location-of-a-comment-in-its-corresponding-google-doc) – Ron M Dec 30 '20 at 15:08