Advertisement
RemcoE33

TEXTFORMATTER

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