Home > OS >  How to change the text based on suggestions through GAS and Google DOC API
How to change the text based on suggestions through GAS and Google DOC API

Time:02-12

As title, yesterday I learn how to retrieve the All-suggestion-accepted content of a document with API in enter image description here

This is the insertion and the deletion I made to the document, and I want to underline the deletion and insertion part within the All-suggestion-accepted content of a document based on the index. enter image description here

Below is the snippet and the result of the snippet, last two figures are the start and the end index of the insertion or deletion.

function get_all_insertion_deletion() {
  var documentId = "MYDOCUMENTID";
  var doc = Docs.Documents.get(documentId);
  remove_all(documentId);
  doc.body.content.forEach(function (content){
    if (content.paragraph) {
      var elements = content.paragraph.elements;
      
      elements.forEach(function (element){
        if(element.textRun.suggestedDeletionIds)
        {
          var d_length= element.endIndex-element.startIndex;
          var d= [element.textRun.suggestedDeletionIds,"delete",element.textRun.content,element.startIndex,element.endIndex];
          Logger.log(d);
          deletion  ;
        }
        if(element.textRun.suggestedInsertionIds)
        {
          var i_length= element.endIndex-element.startIndex;
          var i= [element.textRun.suggestedInsertionIds,"insert",element.textRun.content,element.startIndex,element.endIndex];
          Logger.log(i);
          insertion  ; }  }); }
      });
    }
 

This is the result of the snippet, last two figures are the start and the end index of the insertion and deletion

CodePudding user response:

I believe your goal is as follows.

  • You want to add the underline to "delete" and "insert" parts of your script in Google Document.

In order to achieve your goal, when your script is modified it becomes as follows.

Modified script:

function get_all_insertion_deletion() {
  var documentId = "MYDOCUMENTID";
  var doc = Docs.Documents.get(documentId);
  var requests = [];
  doc.body.content.forEach(function (content) {
    if (content.paragraph) {
      var elements = content.paragraph.elements;

      elements.forEach(function (element) {
        if (element.textRun.suggestedDeletionIds) {
          // var d_length = element.endIndex - element.startIndex;  // This is not used.
          var d = [element.textRun.suggestedDeletionIds, "delete", element.textRun.content, element.startIndex, element.endIndex];
          Logger.log(d);
          requests.push({ updateTextStyle: { range: { startIndex: element.startIndex, endIndex: element.endIndex }, textStyle: { underline: true }, fields: "underline" } });
          // deletion  ; // This is not used.
        }
        if (element.textRun.suggestedInsertionIds) {
          // var i_length = element.endIndex - element.startIndex;  // This is not used.
          var i = [element.textRun.suggestedInsertionIds, "insert", element.textRun.content, element.startIndex, element.endIndex];
          requests.push({ updateTextStyle: { range: { startIndex: element.startIndex, endIndex: element.endIndex }, textStyle: { underline: true }, fields: "underline" } });
          Logger.log(i);
          // insertion  ; // This is not used.
        }
      });
    }
  });
  Docs.Documents.batchUpdate({requests}, documentId);
}

or, I thought that you can also the following modified script.

function get_all_insertion_deletion() {
  var documentId = "MYDOCUMENTID";
  var doc = Docs.Documents.get(documentId);
  var requests = doc.body.content.flatMap(content => {
    if (content.paragraph) {
      var elements = content.paragraph.elements;
      return elements.flatMap(element => element.textRun.suggestedDeletionIds || element.textRun.suggestedInsertionIds ? { updateTextStyle: { range: { startIndex: element.startIndex, endIndex: element.endIndex }, textStyle: { underline: true }, fields: "underline" } } : []);
    }
    return [];
  });
  Docs.Documents.batchUpdate({requests}, documentId);
}

Reference:

  • Related