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

Remove text background with google script ( manually done by format > paragraph> borders and shading > reset )


https://docs.google.com/document/d/1VrkXwfR24nIkX9KEGquIqFgkqPAke6MDst_N7YgSOY0/edit?usp=sharing

this is a sample doc what i am attempting to do is automize the flow of pressing: format> paragraph>borders and shading>reset

the goal is to reset the documents white background which aparrently is done via borders and shading

so to conclude i want to : 1.create the code 2.run the code on google app script and see that it works 3.add it as an add on lets focus on 1- 2

I attempted :

I attempted :

function resetParagraphFormatting() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var paragraphs = body.getParagraphs();
  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i];
    paragraph.setAttributes({
      "BORDER_WIDTH": 0,
      "BORDER_COLOR": null,
      "BACKGROUND_COLOR": null
    });
  }
}

we executes in action script but it doesn't affect the doc when i run it .


Solution

  • I thought that in your situation when Google Docs API is used, the script might be simple. So, in this answer, I would like to propose to achieve your goal using Google Docs API. The sample script is as follows.

    Sample script:

    Before you use this script, please enable Google Docs API at Advanced Google services.

    function resetParagraphFormatting() {
      const docId = DocumentApp.getActiveDocument().getId();
      const obj = Docs.Documents.get(docId).body.content;
      Docs.Documents.batchUpdate({ requests: [{ updateParagraphStyle: { range: { startIndex: 1, endIndex: obj.pop().endIndex }, fields: "borderBetween,borderBottom,borderTop,borderLeft,borderRight,shading", paragraphStyle: {} } }] }, docId);
    }
    

    Testing:

    When this script is run, the properties of "Borders and shading" are reset as follows.

    enter image description here

    Note:

    • If you want to also reset the spaces of the paragraph, please include the fields of spaceAbove,spaceBelow into fields: "borderBetween,borderBottom,borderTop,borderLeft,borderRight,shading".

    References: