如何在 google sheet 中生成 uuid(例如 ccb8a660-91c9-a556-58e0-4fa7-229516872004)。
要么用宏?还是公式?
如何在 google sheet 中生成 uuid(例如 ccb8a660-91c9-a556-58e0-4fa7-229516872004)。
要么用宏?还是公式?
您可以使用Utilities.getUuid()
. 但是需要使用自定义函数来实现您想要的,因为在电子表格的功能中没有针对它的功能。为了生成 UUID,请执行以下流程。
=uuid()
工作表中的单元格。通过这个,你可以获得UUID。
function uuid() {
return Utilities.getUuid();
}
如果我误解了你的问题,我很抱歉。
使用自定义函数时,通过电子表格的自动重新计算来更改值。如果你想修复这个值,这个示例脚本怎么样?请将此视为几种解决方法之一。
function onEdit(e) {
if (e.range.getFormula().toUpperCase() == "=UUID(TRUE)") {
e.range.setValue(Utilities.getUuid());
}
}
function uuid() {
return Utilities.getUuid();
}
=uuid()
工作表中的单元格。
=uuid()
被放置为自定义函数。因此,当电子表格自动计算时,值会发生变化。=uuid(true)
工作表中的单元格。
=uuid()
被作为一个值onEdit()
。因此,即使自动计算电子表格,该值也不会更改。=uuid(true)
可以在手动放置函数时使用,因为这里使用了 OnEdit 事件触发器。thinkyfish 公式已更正,基于 broofa 在如何创建 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 版本 4 合规性。
创建的 uuid 会随着时间而改变。
我认为如果不使用 Apps 脚本就无法解决这个问题。
例如:
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());
}
}
我尝试创建一个纯工作表函数,该函数仅使用工作表代码创建完整的 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),"-",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))
感谢您提供有关此线程的所有详细信息。我最终做的是创建一个宏,它将填充一个范围内的所有选定单元格,每个单元格都有自己的 UUID。我使用这种方法在选定的单元格中设置一个值,而不用担心“覆盖”它们。我可以使用保护范围来避免无意中更新单元格。
该脚本有 2 个检查:
如果满足上述 2 个约束,则每个单元格都会填充一个值。这是宏代码:
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`);
}
希望这对某些人有用!
如果您只想要一个可以快速加入的公式,请尝试此操作。这里需要注意的是,生成的值中不会有任何字母字符,但无论如何这些对于几乎任何目的都应该“足够好”。
=RANDBETWEEN(10000000,99999999) & "-" & RANDBETWEEN(1000,9999) & "-" & RANDBETWEEN(1000,9999) & "-" & RANDBETWEEN(1000,9999)& "-" & RANDBETWEEN(1000,9999) & RANDBETWEEN(10000000,99999999)