Advertisement
RemcoE33

TEXTFORMATTER

May 26th, 2022
1,081
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