Generating a unique string

Often you need to generate a unique string. You could generate a guid - here's a snippet from that meets the RFC4122v4 standard.

However, I find that something based on the time in milliseconds with a little bit of randomness works just fine. 

And here's how you would use it.

function testU () {
  Logger.log( cUseful.generateUniqueString ());

and the result


The key is simply n characters (default 3) + the time now in milliseconds to base(36) and that's it. For a comprehensive set of functions for doing random things, see Generating and managing random lists with JavaScript and Apps Script

If you have some scripts you want add to this library, let me know on my forum.

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.

See Google Apps Scripts snippets for more like this and Useful stuff library for more from this library.
For help and more information join our forumfollow the blogfollow me on twitter