Created
May 31, 2018 23:29
-
-
Save hlecuanda/b8f8e88f27aa7925e8e6b858f6dcb0f4 to your computer and use it in GitHub Desktop.
Cached custom function for G Suite / Google Sheets in Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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