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

Code Guy
  • 3,059
  • 2
  • 30
  • 74
  • 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 Sep 29 '18 at 15:46
  • No, it doesnt work. I have made an UPDATE to the question – Code Guy Sep 30 '18 at 01:16
  • @Maurice Codik any workarounds? – Code Guy Oct 02 '18 at 03:10
  • I dont have a great workaround in native apps script. If you're up for using the [Slides advanced service](https://developers.google.com/apps-script/advanced/slides), you can to this with an updateTextStyleRequest via batchUpdatePresentation. – Maurice Codik Oct 02 '18 at 13:55
  • Thank you very much Codik, can you help me with a small snippet. Many thanks. – Code Guy Oct 02 '18 at 14:02
  • I thought I can use textRange.clear and setting the same text again will make the existing text to loose formatting but, this doesn't work for selected textrange inside a shape l. Also, this method will not reset the font family. – Code Guy Oct 02 '18 at 14:12
  • Is there a way to get a shapes' default font family and font size? If so we can set the same after performing the above? – Code Guy Oct 02 '18 at 14:14
  • @Tanaike do you have any workaround? – Code Guy Oct 04 '18 at 01:16
  • @Code Guy Thank you for this information. Can you give me the time to think of about it, because I knew this question just now? I would like to try to think of the workaround. Then, I will report you whether I could find it. By the way, can I ask you about your question? What do you want to clear the formats? From your question, I confirmed ``underline, italic, bold, strikethrough, fontFamily and fontSize``. – Tanaike Oct 04 '18 at 08:35
  • @Code Guy I'm really sorry for my late response. I posted an answer. Could you please confirm it? If I misunderstand your question, I'm sorry. – Tanaike Oct 06 '18 at 07:18

1 Answers1

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @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. – Tanaike Oct 07 '18 at 22:48
  • 1
    @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 https://stackoverflow.com/questions/52630036/bring-the-pageelements-to-front-or-back-using-apps-script-in-google-slides – Tanaike Oct 08 '18 at 08:35
  • 1
    @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. – Tanaike Oct 08 '18 at 08:58
  • @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. – Tanaike Oct 08 '18 at 21:39
  • @Code Guy I apologize for my poor skill. – Tanaike Oct 09 '18 at 08:43
  • @Code Guy I'm sorry. I couldn't understand about the meaning. Should I delete my all comments? – Tanaike Oct 10 '18 at 22:07
  • Does if (e.getPageElementType() == "SHAPE") work or do you need if (e.getPageElementType().toString() == "SHAPE") ? I have found you need the toString(). – aNewb Mar 18 '21 at 15:04
  • @Tanaike What does this line of code check for? `if (text.getRange(0,1).asString().charCodeAt(0) != 10)`? – jason Oct 07 '21 at 03:50