Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Initial work is done by MattyKing
- */
- /**
- * Allows inline text formatting.
- * Example: =TEXTFORMAT(0,"this is serif bold", "serifbold")&" "&"normal text"
- * @param {type} type Array behaviour for types. 0 = All the text in same type, 1 = You need to profide same length typeface array. 2 = Returns available types
- * @param {text} string Any text or cell reference with text.
- * @param {typeface} typeface Typefaces: serifbold, serifbolditalic, sans, sansitalic, sansbold, sansbolditalic, script
- * @customfunction
- */
- function TEXTFORMAT(type, string, typeface) {
- const input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz !@#$%^&*()_+{}:<>?1234567890-=[];,./|".split("");
- const outputs = {
- serifbold: "𝐀𝐁𝐂𝐃𝐄𝐅𝐆𝐇𝐈𝐉𝐊𝐋𝐌𝐍𝐎𝐏𝐐𝐑𝐒𝐓𝐔𝐕𝐖𝐗𝐘𝐙𝐚𝐛𝐜𝐝𝐞𝐟𝐠𝐡𝐢𝐣𝐤𝐥𝐦𝐧𝐨𝐩𝐪𝐫𝐬𝐭𝐮𝐯𝐰𝐱𝐲𝐳",
- serifbolditalic: "𝑨𝑩𝑪𝑫𝑬𝑭𝑮𝑯𝑰𝑱𝑲𝑳𝑴𝑵𝑶𝑷𝑸𝑹𝑺𝑻𝑼𝑽𝑾𝑿𝒀𝒁𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋𝒌𝒍𝒎𝒏𝒐𝒑𝒒𝒓𝒔𝒕𝒖𝒗𝒘𝒙𝒚𝒛",
- sans: "𝖠𝖡𝖢𝖣𝖤𝖥𝖦𝖧𝖨𝖩𝖪𝖫𝖬𝖭𝖮𝖯𝖰𝖱𝖲𝖳𝖴𝖵𝖶𝖷𝖸𝖹𝖺𝖻𝖼𝖽𝖾𝖿𝗀𝗁𝗂𝗃𝗄𝗅𝗆𝗇𝗈𝗉𝗊𝗋𝗌𝗍𝗎𝗏𝗐𝗑𝗒𝗓",
- sansitalic: "𝘈𝘉𝘊𝘋𝘌𝘍𝘎𝘏𝘐𝘑𝘒𝘓𝘔𝘕𝘖𝘗𝘘𝘙𝘚𝘛𝘜𝘝𝘞𝘟𝘠𝘡𝘢𝘣𝘤𝘥𝘦𝘧𝘨𝘩𝘪𝘫𝘬𝘭𝘮𝘯𝘰𝘱𝘲𝘳𝘴𝘵𝘶𝘷𝘸𝘹𝘺𝘻",
- sansbold: "𝗔𝗕𝗖𝗗𝗘𝗙𝗚𝗛𝗜𝗝𝗞𝗟𝗠𝗡𝗢𝗣𝗤𝗥𝗦𝗧𝗨𝗩𝗪𝗫𝗬𝗭𝗮𝗯𝗰𝗱𝗲𝗳𝗴𝗵𝗶𝗷𝗸𝗹𝗺𝗻𝗼𝗽𝗾𝗿𝘀𝘁𝘂𝘃𝘄𝘅𝘆𝘇",
- sansbolditalic: "𝘼𝘽𝘾𝘿𝙀𝙁𝙂𝙃𝙄𝙅𝙆𝙇𝙈𝙉𝙊𝙋𝙌𝙍𝙎𝙏𝙐𝙑𝙒𝙓𝙔𝙕𝙖𝙗𝙘𝙙𝙚𝙛𝙜𝙝𝙞𝙟𝙠𝙡𝙢𝙣𝙤𝙥𝙦𝙧𝙨𝙩𝙪𝙫𝙬𝙭𝙮𝙯",
- script: "𝓐𝓑𝓒𝓓𝓔𝓕𝓖𝓗𝓘𝓙𝓚𝓛𝓜𝓝𝓞𝓟𝓠𝓡𝓢𝓣𝓤𝓥𝓦𝓧𝓨𝓩𝓪𝓫𝓬𝓭𝓮𝓯𝓰𝓱𝓲𝓳𝓴𝓵𝓶𝓷𝓸𝓹𝓺𝓻𝓼𝓽𝓾𝓿𝔀𝔁𝔂𝔃",
- };
- //Returns the availible typefaces.
- if (type == 2) {
- return Object.entries(outputs)
- }
- //Converts to array. So next logic is always based on an array.
- if (!Array.isArray(string)) {
- string = [string];
- }
- //Flatten. Because sheets gives a 2d array and that is not needed.
- string = string.flat();
- if (type == 1 && !Array.isArray(typeface)) {
- typeface = [typeface];
- typeface = typeface.flat();
- //Defaults back to the first entrie in the typeface array.
- } else if (type == 0 && Array.isArray(typeface)) {
- typeface = typeface[0];
- }
- //Creates an array for each converted string in the string a
- return string.map((str, i) => {
- let output;
- //Get the correct output typeface
- if (type == 0) {
- output = outputs[typeface]
- } else {
- output = outputs[typeface[i]]
- }
- return str.split('')
- .map(e => [e, 2 * input.indexOf(e)])
- .map(e => {
- if (e[1] < output.length) {
- return output.substring(e[1], e[1] + 2)
- } else {
- return e[0]
- }
- }).join('')
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement