Skip to content

Instantly share code, notes, and snippets.

@pfelipm
Last active May 10, 2021 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfelipm/d4361211d45ae01e2e7a92bc3bd9e27b to your computer and use it in GitHub Desktop.
Save pfelipm/d4361211d45ae01e2e7a92bc3bd9e27b to your computer and use it in GitHub Desktop.
From UNICODE text to gzipped blob encoded as text and back using Apps Script
/**
* These two functions:
* 1. Transform a UNICODE string to blob, compress and encode it as text using two different methods (base64 or JSON.stringify)
* 2. Reverse de process
*/
function testTextoGzipBlob() {
// Method 1: base64Encode (better compression):
// Text >> Blob >> gzip >> getBytes Byte[] >> *base64Encode* text >> [cache key/value] >> *base64Decode* Byte[] >> Blob >> ungzip >> Text 😅
let datos = '🎩Cadena de texto con eñes y tildes, ¿funcionará?⚙️';
datos += datos + datos + datos; // Make it bigger to appreciate compression
console.info(`Input text length: ${datos.length}, Blob size: ${Utilities.newBlob(datos).getBytes().length} bytes`);
// In
const blobIn = Utilities.gzip(Utilities.newBlob(datos));
const textIn = Utilities.base64Encode(blobIn.getBytes());
console.info(`Blob type: ${blobIn.getContentType()}, Base64 string compressed blob length: ${textIn.length}`);
console.info(`Blob size: ${Utilities.newBlob(textIn).getBytes().length} bytes`);
// Here we could store textIn in cache as text
// <-------------->
// Out
const byteArrayOut = Utilities.base64Decode(textIn);
// ⚠️ Blob.setContentType to "Avoid Blob object must have non-null content type for this operation"!!!!
let blobOut = Utilities.newBlob(byteArrayOut).setContentType('application/x-gzip');
blobOut = Utilities.ungzip(blobOut);
const textOut = blobOut.getDataAsString();
console.info(`Input text: '${datos},\nOutput text: ${textOut}`);
}
function testTextoGzipBlob2() {
// Method 2: JSON.stringify
// Text >> Blob >> gzip >> getBytes Byte[] >> *JSON.stringify* text >> [cache key/value] >> *JSON.parse* Byte[] >> Blob >> ungzip >> Text 😅
// Also works, but resulting size is larger
let datos = '🎩Cadena de texto con eñes y tildes, ¿funcionará?⚙️';
datos += datos + datos + datos; // Make it bigger to appreciate compression
console.info(`Input text length: ${datos.length}, Blob size: ${Utilities.newBlob(datos).getBytes().length} bytes`);
// In
const blobIn = Utilities.gzip(Utilities.newBlob(datos));
const textIn = JSON.stringify(blobIn.getBytes());
console.info(`Blob type: ${blobIn.getContentType()}, Stringified compressed blob length: ${textIn.length}`);
console.info(`Blob size: ${Utilities.newBlob(textIn).getBytes().length} bytes`);
// Here we could store textIn in cache as text
// <-------------->
// Out
const byteArrayOut = JSON.parse(textIn);
// ⚠️ Blob.setContentType to "Avoid Blob object must have non-null content type for this operation"!!!!
let blobOut = Utilities.newBlob(byteArrayOut).setContentType('application/x-gzip');
blobOut = Utilities.ungzip(blobOut);
const textOut = blobOut.getDataAsString();
console.info(`Input text: '${datos},\nOutput text: ${textOut}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment