chaotic3quilibrium

GoogleSheet Formula BASE64 Encoding/Decoding

Feb 18th, 2025
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.23 KB | Source Code | 0 0
  1. /**
  2.   * A custom function that encodes or decodes base64.
  3.   *
  4.   *@param {"R29vZ2xl"} data The string to encode/decode.
  5.   *@param {1} encode 1/true = encode (default).0/false = decode.
  6.   *@param {1} charsetStr The character set to use. Allowed values are "UTF-8" and "US-ASCII". Defaults to UTF-8.
  7.   *@param {0} websafe Whether the output string should be safe for use in URLs or not. Defaults to false.
  8.   *@param {0} asString Whether the decoded value should be returned as a string (default) or byte array.
  9.   *@result {"Google"} The result to be returned.
  10.   *@customfunction
  11.   */
  12. function BASE64EITHER(data,encode,charsetStr,websafe,asString) {
  13.   if(data==="" || data==null){return ""}
  14.   if(encode==="" || encode==null){encode=1}  
  15.   else if(encode != 1 && encode!=0 && encode!= true && encode!= false){return "Encode?";}
  16.   if(encode==true){encode=1;}
  17.   else if(encode==false){encode=0;}
  18.   if(charsetStr==="" || charsetStr==null){charsetStr="UTF-8"}
  19.   else if(charsetStr!="UTF-8" && charsetStr!="US-ASCII"){return "Charset?"}
  20.   if(charsetStr=="UTF-8" || charsetStr==1){var charset = Utilities.Charset.UTF_8;}
  21.   else{var charset = Utilities.Charset.US_ASCII;}
  22.   if(websafe==="" ||websafe==null){websafe=0}
  23.   else if(websafe != 1 && websafe!=0 && websafe!= true && websafe!= false){return "Websafe?";}
  24.   else if(websafe==true){websafe=1;}
  25.   else if(websafe==false){websafe=0}
  26.   if(asString==="" ||asString==null){asString=1}
  27.   else if(asString != 1 && asString!=0 && asString!= true && asString!= false){return "AsString?";}
  28.   else if(asString==true){asString=1;}
  29.   else if(asString==false){asString=0}
  30.   var value;
  31.   if(encode==0){
  32.     if(websafe==0){
  33.       if(asString==0){
  34.         value= Utilities.base64Decode(data, charset);
  35.       }else{
  36.         value= Utilities.newBlob(Utilities.base64Decode(data, charset)).getDataAsString(charsetStr);
  37.       }
  38.     }else{
  39.       if(asString==0){
  40.         value= Utilities.base64DecodeWebSafe(data, charset);
  41.       }else{
  42.         value= Utilities.newBlob(Utilities.base64Decode(data, charset)).getDataAsString(charsetStr);
  43.       }    
  44.     }
  45.   }
  46.   else{
  47.       if(websafe==0){
  48.         value= Utilities.base64Encode(data, charset);
  49.     }
  50.     else{value= Utilities.base64EncodeWebSafe(data, charset);}        
  51.   }    
  52.   return value;  
  53. }
  54.  
  55. /**
  56.   * A custom function that ENcodes into base64 by forwarding the call to the BASE64 function.
  57.   *
  58.   *@param {"{"residentUserId"=14,"dob":"2020/01/01-12:34:56 CST"}"} data The string to encode.
  59.   *Defaults to 1/true = encode
  60.   *Defaults to UTF-8.
  61.   *Defaults to false.
  62.   *@result {"eyJyZXNpZGVudFVzZXJJZCI9MTQsImRvYiI6IjIwMjAvMDEvMDEtMTI6MzQ6NTYgQ1NUIn0="} The result to be returned.
  63.   *@customfunction
  64.   */
  65. function BASE64ENCODE(data) {
  66.   return BASE64EITHER(data, 1)
  67. }
  68.  
  69. /**
  70.   * A custom function that DEcodes from base64 by forwarding the call to the BASE64 function.
  71.   *
  72.   *@param {"eyJyZXNpZGVudFVzZXJJZCI9MTQsImRvYiI6IjIwMjAvMDEvMDEtMTI6MzQ6NTYgQ1NUIn0="} data The string to decode.
  73.   *Defaults to 0/false = decode
  74.   *Defaults to UTF-8.
  75.   *Defaults to false.
  76.   *@result {"{"residentUserId"=14,"dob":"2020/01/01-12:34:56 CST"}"} The result to be returned.
  77.   *@customfunction
  78.   */
  79. function BASE64DECODE(data) {
  80.   return BASE64EITHER(data, 0)
  81. }    
  82.  
Add Comment
Please, Sign In to add comment