How to generate an uuid in google sheet (for exemple ccb8a660-91c9-a556-58e0-4fa7-229516872004).
Either with a macro? or a formula?
Thanks for all the details on this thread. What I ended up doing is creating a macro that will fill all the selected cells in a range each with their own UUID. I use this approach to set a value in selected cells and not worry about "overwriting" them. I can use Protect Range to avoid the cells being updated inadvertently.
This script has 2 checks:
If the above 2 constraints are met, then each cell gets filled with a value. Here's the macro code:
function fillSelectedWithUUIDs() {
let curSheet = SpreadsheetApp.getActiveSheet();
let curSelection = curSheet.getSelection();
let curRange = curSelection.getActiveRange();
let ui = SpreadsheetApp.getUi();
if (curRange.getNumColumns() !== 1) {
ui.alert(`Range must only contain one column.`);
return;
}
for (let i = 0; i < curRange.getNumRows(); i++) {
let curCell = curRange.getCell(1 + i, 1);
if (curCell.getValue() !== "") {
ui.alert(`ERROR: Cannot overwrite value in cell (${curCell.getA1Notation()})`);
return;
}
}
for (let i = 0; i < curRange.getNumRows(); i++) {
curRange.getCell(1 + i, 1).setValue(Utilities.getUuid())
}
ui.alert(`Added ${curRange.getNumRows()} UUIDs`);
}
Hope this is of use to some!
Try this if you just want a formula you can quickly drop in. The caveat here is that you won't have any alpha characters in the generated value, but these should be "good enough" for nearly any purpose regardless.
=RANDBETWEEN(10000000,99999999) & "-" & RANDBETWEEN(1000,9999) & "-" & RANDBETWEEN(1000,9999) & "-" & RANDBETWEEN(1000,9999)& "-" & RANDBETWEEN(1000,9999) & RANDBETWEEN(10000000,99999999)
the created uuid is changed with the time.
I think this problem could not be solved without using the Apps Script.
For example:
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getRange('A1:A10').getValues();
for (var i = 0; i < values.length; i++) {
// When a cell is empty, set a uuid to the cell.
if (!values[i][0]) {
sheet.getRange('A' + (1 + i)).setValue(Utilities.getUuid());
}
}
You can generate UUID using Utilities.getUuid()
. But it is required to use a custom function for achieving what you want, because there are no functions for it in Spreadsheet's functions. In order to generate UUID, please do the following flow.
=uuid()
to a cell in a sheet.By this, you can get UUID.
function uuid() {
return Utilities.getUuid();
}
If I misunderstand your question, I'm sorry.
When a custom function is used, the value is changed by the automatically recalculating of Spreadsheet. If you want to fix the value, how about this sample script? Please think of this as just one of several workarounds.
function onEdit(e) {
if (e.range.getFormula().toUpperCase() == "=UUID(TRUE)") {
e.range.setValue(Utilities.getUuid());
}
}
function uuid() {
return Utilities.getUuid();
}
=uuid()
to a cell in a sheet.
=uuid()
is put as a custom function. So when the Spreadsheet is automatically calculated, the value is changed.=uuid(true)
to a cell in a sheet.
=uuid()
is put as a value by onEdit()
. So even when the Spreadsheet is automatically calculated, the value is NOT changed.=uuid(true)
can use when the function is manually put, because this uses the OnEdit event trigger.I tried creating a pure sheet function that creates full uuid's just with sheet code and this is what I ended up with it's pretty silly but it does seem to work. It will re-generate every time the sheet updates so this might not be useful but I wanted to post it anyway because there wasn't a pure sheet implementation available.
=CONCATENATE(MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),"-",MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),"-",MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),"-",MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),"-",MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1))
thinkyfish formula corrected, based on broofa's response at How to create a GUID / UUID
=CONCATENATE(MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),"-",MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),"-4",MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),"-",MID("89ab",RANDBETWEEN(1,4),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),"-",MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1),MID("0123456789abcdef",RANDBETWEEN(1,16),1))
UUID Version 4 compliance.