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!