1

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

10
  • 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. Commented Sep 29, 2018 at 15:46
  • No, it doesnt work. I have made an UPDATE to the question Commented Sep 30, 2018 at 1:16
  • @Maurice Codik any workarounds? Commented Oct 2, 2018 at 3:10
  • 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. Commented Oct 2, 2018 at 13:55
  • Thank you very much Codik, can you help me with a small snippet. Many thanks. Commented Oct 2, 2018 at 14:02

1 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

8 Comments

@Code Guy Did my answer show you the result what you want? Would you please tell me about it? That is also useful for me to study. If this works, other people who have the same issue with you can also base your question as a question which can be solved. If you have issues for my answer yet, feel free to tell me. I would like to study to solve your issues.
@Code Guy Thank you for replying. I'm glad this was useful for you. About your other issues, when you want to clear the formats of colors and so on, please check the fields of updateTextStyle for Slides API. Also you can see about it from Note of my answer. If you want to select the object, you can see the sample script at stackoverflow.com/questions/52630036/…
@Code Guy About I observed that, the undo function will not undo the changes done via the batch request API. Is there a way to achive?, I cannot find the workaround yet. I'm really sorry for my poor skill.
@Code Guy Thank you for replying. I would like to study more and more. By the way, about one of workarounds of "undo" of Sheets API, how about saving values before the sheet is updated? In this case, unfortunately, the shortcut key cannot be used.
@Code Guy I apologize for my poor skill.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.