I have this code that converts plain text to Unicode styled characters:
function convertText(text, style) {
// 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}])},
};
if(style == 'bold')
return(conv.bold(text));
else if(style == 'italic')
return(conv.italic(text));
else if(style == 'bolditalic')
return(conv.boldItalic(text));
else
return text;
}
which I have found on this StackOverflow link:
Google script string with style
I tried reversing the code to convert the styled unicode characters to plain text but failed. Hopefully somebody could reverse the code to get the plain text from the styled unicode characters.
2
Answers
Although I’m not sure whether I could correctly understand your expected result, how about the following sample script?
Sample script:
In this sample,
normalize()
is used.When this script is run, the following result is obtained. In this case, I thought that
NFKC
andNFKD
might be able to be used.In this sample output, the original text is converted to
italic
,bold
, andbolditalic
and the converted text is also converted to normal text.text1
,text2
, andtext1
are the source text, and the converted text, and the normal text converted from the converted text, respectively.Testing:
Reference:
Building on Tanaike’s work, here’s a function that will accept any text and return it formatted or plain, as requested.
Supports arrays of text. Can re-format already formatted text. The function can be called from another JavaScript function, or directly from a spreadsheet formula as a custom function.
Here’s a short test function that converts text to various formats, then converts those formatted strings back to plain text:
To test this as a custom function, enter text in column
A
and put this formula in row 1 of a free column: