如何接受 google 文档与 Google 文档 API 的样式建议?

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

如标题,在 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中,但仍然没有任何线索。以下是我目前的片段。


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); 
}

目前,无法通过 Google 文档 API accepting/rejecting 建议。

要检索样式建议,您需要访问每个段落元素的 textRun 并检查 suggestedTextStyleChanges 是否存在。

示例:

代码:

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);
        }
      });
  });
}

输出:

编辑

在您的代码中,您添加了 "textStyleSuggestionState": { "underlineSuggested": true, },但 Docs.get 方法仅接受 suggestionsViewMode 作为 查询参数 以及您可以输入的值是:

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.

找到所有 underlineSuggested: true 的正确方法是在响应主体中遍历它并使用 SUGGESTIONS_INLINE 作为 suggestionsViewMode.

示例:

文档:

此代码将打印带有下划线建议的字符串:

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);
          }
        }
      });
  });
}

输出:

注意:如果要查看所有建议,必须在textStyleSuggestionState查询参数中使用SUGGESTIONS_INLINE或将其删除为SUGGESTIONS_INLINE 是默认视图,如果您有权访问该文档。此外,当您使用 PREVIEW_SUGGESTIONS_ACCEPTED 时,您将不会在对象中看到任何建议,因为它 returns 接受了所有建议的文档预览。

进一步阅读