Skip to content

Instantly share code, notes, and snippets.

@hlecuanda
Created May 31, 2018 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlecuanda/b8f8e88f27aa7925e8e6b858f6dcb0f4 to your computer and use it in GitHub Desktop.
Save hlecuanda/b8f8e88f27aa7925e8e6b858f6dcb0f4 to your computer and use it in GitHub Desktop.
Cached custom function for G Suite / Google Sheets in Apps Script
kch = CacheService.getDocumentCache(); // set up the cache service as a global object
function lp(num){
return ("0000" + num).slice(-2) // elegant
Utilities.sleep(500);
}
/**
* A function to left pad an integert with 0 if less than two digits
*
* @param {Num} number - number to pad
* @return {Padded} string - the number,zero padded to 2 spaces
* @customfunction
*/
function leftpad(num){
var r = kch.get(num);
if (r) {
Utilities.sleep(500);
return r
} else {
r = lp(num);
kch.put(num,r,1200);
Utilities.sleep(500);
return r
}
}
function testpad(){
var num_a = 5;
var num_b = 10;
var pad_a = leftpad(num_a);
var pad_b = leftpad(num_b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment