-1

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

Steven. Y
  • 31
  • 4
  • 1
    I have to apologize for my poor English skill. Unfortunately, I cannot understand your question. Can I ask you about the detail of your question? If you can do, please provide the sample input and output situations as the image? – Tanaike Feb 11 '22 at 00:17
  • @Tanaike I've rewritten the whole post, please advise. – Steven. Y Feb 11 '22 at 03:46
  • 1
    Thank you for replying. I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of the solution. I would be grateful if you can forgive my poor English skill. – Tanaike Feb 11 '22 at 04:42
  • @Tanaike Not a problem additional information is added to the post, please advise. – Steven. Y Feb 11 '22 at 05:29
  • 1
    Thank you for replying and adding more information. From your additional information, I proposed a modified script as an answer. Could you please confirm it? If I misunderstood your question, I apologize. – Tanaike Feb 11 '22 at 06:36

1 Answers1

1

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This is exactly what I need. By the way, is it possible to underline the changed content after every suggestion is accepted? – Steven. Y Feb 11 '22 at 11:11
  • 1
    @Steven. Y Thank you for replying. I'm glad your issue was resolved. About your additional question of `is it possible to underline the changed content after every suggestion is accepted?`, I think that when the suggestion is accepted, `suggestedDeletionIds` and `suggestedInsertionIds` cannot be retrieved. I apologize for this. – Tanaike Feb 11 '22 at 11:52