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

Time:02-16

As title, in 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

  • Related