-
-
Save trfiladelfo/dea7e094aac2ef66b90324f1412aefa4 to your computer and use it in GitHub Desktop.
Google Script to generate a UUID in Google Sheets
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
function getId() { | |
/** | |
* Imported from https://github.com/kyo-ago/UUID | |
* Robbie Mitchell, @superstrong | |
*/ | |
/** | |
* UUID.core.js: The minimal subset of the RFC-compliant UUID generator UUID.js. | |
* | |
* @fileOverview | |
* @author LiosK | |
* @version core-1.0 | |
* @license The MIT License: Copyright (c) 2012 LiosK. | |
*/ | |
/** @constructor */ | |
function UUID() {} | |
/** | |
* The simplest function to get an UUID string. | |
* @returns {string} A version 4 UUID string. | |
*/ | |
UUID.generate = function() { | |
var rand = UUID._gri, hex = UUID._ha; | |
return hex(rand(32), 8) // time_low | |
+ "-" | |
+ hex(rand(16), 4) // time_mid | |
+ "-" | |
+ hex(0x4000 | rand(12), 4) // time_hi_and_version | |
+ "-" | |
+ hex(0x8000 | rand(14), 4) // clock_seq_hi_and_reserved clock_seq_low | |
+ "-" | |
+ hex(rand(48), 12); // node | |
}; | |
/** | |
* Returns an unsigned x-bit random integer. | |
* @param {int} x A positive integer ranging from 0 to 53, inclusive. | |
* @returns {int} An unsigned x-bit random integer (0 <= f(x) < 2^x). | |
*/ | |
UUID._gri = function(x) { // _getRandomInt | |
if (x < 0) return NaN; | |
if (x <= 30) return (0 | Math.random() * (1 << x)); | |
if (x <= 53) return (0 | Math.random() * (1 << 30)) | |
+ (0 | Math.random() * (1 << x - 30)) * (1 << 30); | |
return NaN; | |
}; | |
/** | |
* Converts an integer to a zero-filled hexadecimal string. | |
* @param {int} num | |
* @param {int} length | |
* @returns {string} | |
*/ | |
UUID._ha = function(num, length) { // _hexAligner | |
var str = num.toString(16), i = length - str.length, z = "0"; | |
for (; i > 0; i >>>= 1, z += z) { if (i & 1) { str = z + str; } } | |
return str; | |
}; | |
/** | |
* Returns result to Google Sheet | |
*/ | |
var newId = UUID.generate(); | |
return newId; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment