How to generate an uuid in google sheet?
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.
- Open the script editor for creating Google Apps Script.
- Copy and paste the following script and save it.
- Put
=uuid()
to a cell in a sheet.
By this, you can get UUID.
Script :
function uuid() {
return Utilities.getUuid();
}
Reference :
- getUuid()
If I misunderstand your question, I'm sorry.
Added:
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.
Sample script:
function onEdit(e) {
if (e.range.getFormula().toUpperCase() == "=UUID(TRUE)") {
e.range.setValue(Utilities.getUuid());
}
}
function uuid() {
return Utilities.getUuid();
}
- When you use this script, please do the following flow.
- Copy and paste the script to the bound-script of Spreadsheet and save it.
- Put
=uuid()
to a cell in a sheet.- In this case,
=uuid()
is put as a custom function. So when the Spreadsheet is automatically calculated, the value is changed.
- In this case,
- Put
=uuid(true)
to a cell in a sheet.- In this case,
=uuid()
is put as a value byonEdit()
. So even when the Spreadsheet is automatically calculated, the value is NOT changed.
- In this case,
Note:
- In this case,
=uuid(true)
can use when the function is manually put, because this uses the OnEdit event trigger. - This is a simple sample script. So please modify this for your situation.
Reference:
- Simple Triggers
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.