Search code examples
javascriptgoogle-apps-scriptgoogle-docsgoogle-docs-api

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


As title, yesterday I learn how to retrieve the All-suggestion-accepted content of a document with API in this post, and I refer to the other post and know how to retrieve the insertion and deletion of the suggestion. Also, with the method above, I can retrieve the start-index(The position where insertion or deletion starts in a document) and end-index(The position where insertion or deletion ends in a document) of the insertion and deletion.

So, can I make changes, such as underlining the insertion or deletion parts, to the All-suggestion-accepted content referring to the indexes as positions? Is there going to be an error if I do this?

This is the array Google Doc API returned to me, and its format is [suggestedDeletionIds,"delete",content,startIndex,endIndex] 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


Solution

  • 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: