1

As title, I manage to retrieve all-suggestion-accepted content on Google Docs through API. I have referred to its guideline and a couple of posts on this platform but in vain. Below is the snippet I currently have. Please Advise.

function myFunction() 
{
  var documentId ="My file ID";
  var doc = Docs.Documents.get(documentId);
  var  SUGGEST_MODE= "PREVIEW_SUGGESTIONS_ACCEPTED";
  var doc1 = Docs.Documents.get(documentId).setSuggestionsViewMode(SUGGEST_MODE);
  console.log(doc1.body.content)
}
Steven. Y
  • 31
  • 4

1 Answers1

1

Upon seeing the documentation, you should use it like this

Script:

function myFunction() {
  doc_id = '1s26M6g8PtSR65vRcsA90Vnn_gy1y3wj7glj8GxcNy_E';
  SUGGEST_MODE = 'PREVIEW_SUGGESTIONS_ACCEPTED'
  suggestions = Docs.Documents.get(doc_id, {
    'suggestionsViewMode': SUGGEST_MODE
  });

  new_content = '';
  
  suggestions.body.content.forEach(obj => {
    if(obj.paragraph)
      obj.paragraph.elements.forEach(element => {
        new_content += element.textRun.content;
      });
  });

  console.log(new_content);
}

Sample suggestions (with added paragraph):

sample

Output:

output

EDIT:

  • Added an additional paragraph, and instead of just logging them, I have stored them in a single variable and printed at the end.

Reference:

NightEye
  • 10,634
  • 2
  • 5
  • 24
  • 1
    thanks for the reply. But this is not what I am looking for. I want to retrieve the content of the document (e.g. the text or the body written on the file, in a way that all suggestions, both insertion, and deletion, are being accepted and automatically. ) – Steven. Y Feb 09 '22 at 16:13
  • 1
    If that's the case, you will need to traverse the returned response and then form up the content of the file. It will be difficult for me to provide the actual formation of your response given that I don't have any idea on how your document looks, how many suggestions is there, etc. – NightEye Feb 09 '22 at 16:15
  • 1
    @Steven.Y, I have modified my code. I now traverse the response and fetch the actual accepted suggestions. See edit above. – NightEye Feb 09 '22 at 16:21
  • 1
    @Steven.Y, I have further modified my code to support multiple paragraphs and now, the content is stored in a single variable. See modification above. – NightEye Feb 09 '22 at 16:35