1
votes

I have a slide with the text box as well as different shapes. I need to remove formatting on all text inside the page elements.

Remove formatting actually restores to original text property based on the theme or master of a slide

I did not found any function to clear the formatting and restore it to base format.

I tried with,

setUnderline(false).setItalic(false).setBold(false).setStrikethrough(false)

But, It will not restore its fontsize and font family since I didnt find a way to get the placeholder default fontsize and fontfamily.

Is there a workaround available? How to unset the fontfamily and fontsize?

UPDATE: (not working)

text.getTextStyle().setUnderline(false).setItalic(false).setBold(false).setStrikethrough(false).setFontFamily("").setFontFamily("").setFontSize(null);

This will throw a server error occured

1
Often setting a property to null can be used to signal its removal. If that raises an error, you'll want to include a bit more of your code in your question snippet so that it is obvious what classes are being manipulated. - tehhowch
No, it doesnt work. I have made an UPDATE to the question - Code Guy
@Maurice Codik any workarounds? - Code Guy
I dont have a great workaround in native apps script. If you're up for using the Slides advanced service, you can to this with an updateTextStyleRequest via batchUpdatePresentation. - Maurice Codik
Thank you very much Codik, can you help me with a small snippet. Many thanks. - Code Guy

1 Answers

3
votes

How about this workaround? I think that there are 2 patterns for this situation. From your question, in this answer, it clears the formats of underline, italic, bold, strikethrough, fontFamily and fontSize. In this answer, "clear" means that it modifies to the default formats.

Workaround 1:

Use Slide services. At first, it retrieves the default values of the text style. As a sample, put a text box including a text value to a slide. In this case, the format of text value is not changed. Using Slides API, the default text style is retrieved as follows.

"style": {
 "underline": false,
 "italic": false,
 "bold": false,
 "strikethrough": false,
 "fontFamily": "Arial",
 "fontSize": {
  "magnitude": 14,
  "unit": "PT"
 }
}

In this workaround, these values are used as the default values. The sample script is as follows.

Sample script:

In this sample, the text styles of the PageElementType of SHAPE and TABLE are modified to the default formats.

function toDefault(text) {
  if (text.getRange(0,1).asString().charCodeAt(0) != 10) {
    var style = text.getTextStyle();
    return style.setUnderline(false).setItalic(false).setBold(false).setStrikethrough(false).setFontFamily("Arial").setFontSize(14);
  }
  return null;
}

function myFunction() {
  var s = SlidesApp.getActivePresentation();
  var slide = s.getSlides()[0]; // As a sample, 1st page is used.
  var pageElements = slide.getPageElements();
  pageElements.forEach(function(e) {
    if (e.getPageElementType() == "SHAPE") {
      var text = e.asShape().getText(); 
      toDefault(text);
    } else if (e.getPageElementType() == "TABLE") {
      var table = e.asTable();
      for (var row = 0; row < table.getNumRows(); row++) {
        for (var col = 0; col < table.getNumColumns(); col++) {
          var text = table.getCell(row, col).getText();
          toDefault(text);
        }
      }
    }
  });
}

Workaround 2:

Use Slides API. For updateTextStyle of Slides.Presentations.batchUpdate(), when only "fields": "underline,italic,bold,strikethrough,fontFamily,fontSize" without setting each value is used, the default values of underline,italic,bold,strikethrough,fontFamily,fontSize are used. In this workaround, this is used.

Sample script:

In this sample, the text styles of the PageElementType of SHAPE and TABLE are modified to the default formats.

function myFunction() {
  var s = SlidesApp.getActivePresentation();
  var slide = s.getSlides()[0];
  var presentationId = s.getId();
  var pageElements = slide.getPageElements();
  var reqs = pageElements.reduce(function(o, e) {
    if (e.getPageElementType() == "SHAPE") {
      if (e.asShape().getText().getRange(0,1).asString().charCodeAt(0) != 10) {
        o.push({"updateTextStyle": {"objectId": e.getObjectId(), "fields": "underline,italic,bold,strikethrough,fontFamily,fontSize"}});
      }
    } else if (e.getPageElementType() == "TABLE") {
      var table = e.asTable();
      var objectId = e.getObjectId();
      for (var row = 0; row < table.getNumRows(); row++) {
        for (var col = 0; col < table.getNumColumns(); col++) {
          var text = table.getCell(row, col).getText();
          if (text.getRange(0,1).asString().charCodeAt(0) != 10) {
            o.push({"updateTextStyle": {"objectId": e.getObjectId(), "cellLocation": {"columnIndex": row, "rowIndex": col}, "fields": "underline,italic,bold,strikethrough,fontFamily,fontSize"}});
          }
        }
      }
    }
    return o;
  }, []);
  var resource = {"requests": reqs};
  Slides.Presentations.batchUpdate(resource, presentationId);
}

Note:

  • When you use workaround 2, please enable Slides API at Advanced Google Services and API console.
  • If you want to clear all formats of the text style, for the workaround 2, please modify from "fields": "underline,italic,bold,strikethrough,fontFamily,fontSize" to "fields": "*".

References: