Skip to content

Instantly share code, notes, and snippets.

@trfiladelfo
Forked from superstrong/Code.gs
Created June 3, 2021 03:05
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 trfiladelfo/dea7e094aac2ef66b90324f1412aefa4 to your computer and use it in GitHub Desktop.
Save trfiladelfo/dea7e094aac2ef66b90324f1412aefa4 to your computer and use it in GitHub Desktop.
Google Script to generate a UUID in Google Sheets
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