Skip to content

Instantly share code, notes, and snippets.

@superstrong
Created March 22, 2016 16:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save superstrong/71ec75b9d143b88490f6 to your computer and use it in GitHub Desktop.
Save superstrong/71ec75b9d143b88490f6 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;
}
@coltonoscopy
Copy link

This is great, but the only problem is this will always generate the same UUID because Google Spreadsheets doesn't let Math.random() reselect a new value every time it's called; have you figured out a workaround? I adopted your code and tried messing around to no avail.

@sobotka
Copy link

sobotka commented Aug 11, 2019

This is ancient, but I thought I’d leave a breadcrumb for anyone looking to solve this in their context.

It turns out that getMilliseconds and random via the Date object and Math object respectively, do in fact work and deliver unique values on repeated function script calls in Google Sheets via Apps Script etc. We can validate this by running a for loop and placing the values into an array and outputting the report to the StackDriver console. As it turns out, if you perform all of your work and stuff the results into an array, and then assign the results via setValues etc. from the array, everything works as expected.

I’m certain that multiple UUIDs could be generated in this manner as above, assuming the total number of values required is reduced ahead of time via getRows / getColumns, and allocating the appropriate sized array prior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment