Skip to content
Advertisement

How to Accept style suggestions of a google doc with Google Doc API?

As title, in this post I already know how to retrieve all-suggestion-accepted content of a document. Now I also want to retrieve the “style suggestion” of the content of the document. I’ve referred to the second half part of this guideline, but still have no clue about it. Below is the snippet I have as yet.

function get_all_suggestion_accepted()
{
  
  var doc_id = 'My doc ID';
  SUGGEST_MODE = 'PREVIEW_SUGGESTIONS_ACCEPTED'
  suggestions = Docs.Documents.get(doc_id, {
    'suggestionsViewMode': SUGGEST_MODE,
    "textStyleSuggestionState": { "underlineSuggested": true, }
  });

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

Advertisement

Answer

Currently, accepting/rejecting suggestions via Google Docs API is not possible.

To retrieve the style suggestion, you need to access the textRun of each paragraph element and check if suggestedTextStyleChanges exists.

Example:

enter image description here

Code:

function myFunction() {
  var suggestions = Docs.Documents.get("doc id");
  suggestions.body.content.forEach(obj => {
    if(obj.paragraph)
      obj.paragraph.elements.forEach(element => {
        if(element.textRun.suggestedTextStyleChanges){
          Logger.log(element.textRun.content);
          Logger.log(element.textRun.suggestedTextStyleChanges);
        }
      });
  });
}

Output:

enter image description here

Edit

In your code, you added "textStyleSuggestionState": { "underlineSuggested": true, }, but Docs.get method only accepts suggestionsViewMode as Query Parameter and the values you can input are:

DEFAULT_FOR_CURRENT_ACCESS – The SuggestionsViewMode applied to the returned document depends on the user’s current access level. If the user only has view access, PREVIEW_WITHOUT_SUGGESTIONS is applied. Otherwise, SUGGESTIONS_INLINE is applied. This is the default suggestions view mode.

SUGGESTIONS_INLINE The returned document has suggestions inline. Suggested changes will be differentiated from base content within the document.

Requests to retrieve a document using this mode will return a 403 error if the user does not have permission to view suggested changes.

PREVIEW_SUGGESTIONS_ACCEPTED The returned document is a preview with all suggested changes accepted.

Requests to retrieve a document using this mode will return a 403 error if the user does not have permission to view suggested changes.

PREVIEW_WITHOUT_SUGGESTIONS The returned document is a preview with all suggested changes rejected if there are any suggestions in the document.

The correct way to find all the underlineSuggested: true is by traversing it in the Response body and use SUGGESTIONS_INLINE as suggestionsViewMode.

Example:

Document:

enter image description here

This code will print the string that has underline suggestion:

function get_all_suggestion_accepted() {
  var suggestions = Docs.Documents.get("11Tx4uvv5yN_TplT4TIUyEWTZ6bUMTGaensYT20EZ4r0");
  suggestions.body.content.forEach(obj => {
    if(obj.paragraph)
      obj.paragraph.elements.forEach(element => {
        if(element.textRun.suggestedTextStyleChanges){
          var obj = JSON.parse(JSON.stringify(element.textRun.suggestedTextStyleChanges));
          if(obj[Object.keys(obj)[0]].textStyleSuggestionState.underlineSuggested){
            Logger.log(element.textRun.content);
          }
        }
      });
  });
}

Output:

enter image description here

Note: If you want to view all the suggestions, you have to use SUGGESTIONS_INLINE in the textStyleSuggestionState query parameter or remove it as SUGGESTIONS_INLINE is the default view if you have access to the document. Also, when you use PREVIEW_SUGGESTIONS_ACCEPTED you won’t see any suggestions in the object as it returns a preview of the document with all the suggestions accepted.

Further Reading

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement