stringgoogle-apps-scriptfontsgoogle-forms

Google script string with style


I'm quite puzzled with how strings are handled in Google Script. In particular, it seems that strings can be styled, but I have no idea how to actually do this.

For instance: I create a Google Form, add a Short Text question, and copy paste a bold text generated here : https://lingojam.com/BoldTextGenerator

If I open this form with a script, I can recover this text with

  var form = FormApp.openById(formID);
  var Items = form.getItems();
  var test = Items[0].getTitle();

This "test" variable is a string (I checked with Logger.log(typeof(test))), not a "Text" nor "RichText" and methods such as .isBold() won't work.

However, Logger.log(test) does output a bold text in the log journal - so this string does contain some information about its style.

Yet, I can't seem to define a styled string within Google Script. I have tried quite different things, but none worked

var dummy = "Hello World !"
Logger.log(dummy.bold())
Logger.log(dummy.setBold(true))
Logger.log(dummy.setFontWeight("bold"))
Logger.log("<b>"+dummy+"</b>")
Logger.log("**"+dummy+"**")

What can I do to have my dummy string to be logged in a bold font (my real objective being to use a .setTitle(dummy) method to have a bold font form item) ?


Solution

  • I believe your goal as follows.

    Issue and workaround:

    Unfortunately, in the current stage, there are not methods for directly managing the rich texts to the title of each items on Google Form in Google Form service. But when the text with the bold type is directly copied and pasted to the title of item on Google Form, it can be done. So in this answer, using this, as a current workaround, I would like to propose to convert the text data to the text with the bold type with the unicode, and put the converted text to Google Form.

    The flow of this workaround is as follows.

    1. Convert the text to the bold type with the unicode.
      • In this conversion, each character in the text is converted to the bold type using the difference between the original character code and the bold character code.
    2. Put to the converted text to the title of item on Google Form.

    When above flow is reflected to the script, it becomes as follows.

    Sample script:

    function myFunction() {
      // 1. Convert the text to the bold type with the unicode.
      const conv = {
        c: function(text, obj) {return text.replace(new RegExp(`[${obj.reduce((s, {r}) => s += r, "")}]`, "g"), e => {
          const t = e.codePointAt(0);
          if ((t >= 48 && t <= 57) || (t >= 65 && t <= 90) || (t >= 97 && t <= 122)) {
            return obj.reduce((s, {r, d}) => {
              if (new RegExp(`[${r}]`).test(e)) s = String.fromCodePoint(e.codePointAt(0) + d);
              return s;
            }, "")
          }
          return e;
        })},
        bold: function(text) {return this.c(text, [{r: "0-9", d: 120734}, {r: "A-Z", d: 120211}, {r: "a-z", d: 120205}])},
        italic: function(text) {return this.c(text, [{r: "A-Z", d: 120263}, {r: "a-z", d: 120257}])},
        boldItalic: function(text) {return this.c(text, [{r: "A-Z", d: 120315}, {r: "a-z", d: 120309}])},
      };
    
      var newTitle = "New title for item 1";
      var convertedNewTitle = conv.bold(newTitle); // Bold type
    //  var convertedNewTitle = conv.italic(newTitle); // Italic type
    //  var convertedNewTitle = conv.boldItalic(newTitle); // Bold-italic type
    
      // 2. Put to the converted text to the title of item on Google Form.
      var formID = "###";  // Please set the Form ID.
      var form = FormApp.openById(formID);
      var Items = form.getItems();
      Items[0].setTitle(convertedNewTitle);
    }
    

    Result:

    When above sample script is used, the following result is obtained.

    From:

    enter image description here

    To:

    enter image description here

    Testing

    https://jsfiddle.net/7bL5r3em/

    References: