Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A custom function that encodes or decodes base64.
- *
- *@param {"R29vZ2xl"} data The string to encode/decode.
- *@param {1} encode 1/true = encode (default).0/false = decode.
- *@param {1} charsetStr The character set to use. Allowed values are "UTF-8" and "US-ASCII". Defaults to UTF-8.
- *@param {0} websafe Whether the output string should be safe for use in URLs or not. Defaults to false.
- *@param {0} asString Whether the decoded value should be returned as a string (default) or byte array.
- *@result {"Google"} The result to be returned.
- *@customfunction
- */
- function BASE64EITHER(data,encode,charsetStr,websafe,asString) {
- if(data==="" || data==null){return ""}
- if(encode==="" || encode==null){encode=1}
- else if(encode != 1 && encode!=0 && encode!= true && encode!= false){return "Encode?";}
- if(encode==true){encode=1;}
- else if(encode==false){encode=0;}
- if(charsetStr==="" || charsetStr==null){charsetStr="UTF-8"}
- else if(charsetStr!="UTF-8" && charsetStr!="US-ASCII"){return "Charset?"}
- if(charsetStr=="UTF-8" || charsetStr==1){var charset = Utilities.Charset.UTF_8;}
- else{var charset = Utilities.Charset.US_ASCII;}
- if(websafe==="" ||websafe==null){websafe=0}
- else if(websafe != 1 && websafe!=0 && websafe!= true && websafe!= false){return "Websafe?";}
- else if(websafe==true){websafe=1;}
- else if(websafe==false){websafe=0}
- if(asString==="" ||asString==null){asString=1}
- else if(asString != 1 && asString!=0 && asString!= true && asString!= false){return "AsString?";}
- else if(asString==true){asString=1;}
- else if(asString==false){asString=0}
- var value;
- if(encode==0){
- if(websafe==0){
- if(asString==0){
- value= Utilities.base64Decode(data, charset);
- }else{
- value= Utilities.newBlob(Utilities.base64Decode(data, charset)).getDataAsString(charsetStr);
- }
- }else{
- if(asString==0){
- value= Utilities.base64DecodeWebSafe(data, charset);
- }else{
- value= Utilities.newBlob(Utilities.base64Decode(data, charset)).getDataAsString(charsetStr);
- }
- }
- }
- else{
- if(websafe==0){
- value= Utilities.base64Encode(data, charset);
- }
- else{value= Utilities.base64EncodeWebSafe(data, charset);}
- }
- return value;
- }
- /**
- * A custom function that ENcodes into base64 by forwarding the call to the BASE64 function.
- *
- *@param {"{"residentUserId"=14,"dob":"2020/01/01-12:34:56 CST"}"} data The string to encode.
- *Defaults to 1/true = encode
- *Defaults to UTF-8.
- *Defaults to false.
- *@result {"eyJyZXNpZGVudFVzZXJJZCI9MTQsImRvYiI6IjIwMjAvMDEvMDEtMTI6MzQ6NTYgQ1NUIn0="} The result to be returned.
- *@customfunction
- */
- function BASE64ENCODE(data) {
- return BASE64EITHER(data, 1)
- }
- /**
- * A custom function that DEcodes from base64 by forwarding the call to the BASE64 function.
- *
- *@param {"eyJyZXNpZGVudFVzZXJJZCI9MTQsImRvYiI6IjIwMjAvMDEvMDEtMTI6MzQ6NTYgQ1NUIn0="} data The string to decode.
- *Defaults to 0/false = decode
- *Defaults to UTF-8.
- *Defaults to false.
- *@result {"{"residentUserId"=14,"dob":"2020/01/01-12:34:56 CST"}"} The result to be returned.
- *@customfunction
- */
- function BASE64DECODE(data) {
- return BASE64EITHER(data, 0)
- }
Add Comment
Please, Sign In to add comment