如何在Google工作表中生成uuid(例如ccb8a660-91c9-a556-58e0-4fa7-229516872004)。
是否带有宏?还是公式?我没有在Google帮助中找到解决方案。
Merci Dom
答案 0 :(得分:23)
您可以使用Utilities.getUuid()
生成UUID。但是需要使用自定义函数来实现所需的功能,因为Spreadsheet的函数中没有针对该函数的函数。为了生成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事件触发器。答案 1 :(得分:5)
thinkyfish 公式根据 broofa 在 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 版本 4 合规性。
答案 2 :(得分:3)
我尝试创建一个纯工作表函数,该函数仅使用工作表代码创建完整的 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))
答案 3 :(得分:1)
感谢您提供有关此线程的所有详细信息。我最终做的是创建一个宏,它将填充一个范围内的所有选定单元格,每个单元格都有自己的 UUID。我使用这种方法在选定的单元格中设置一个值,而不用担心“覆盖”它们。我可以使用保护范围来避免无意中更新单元格。
这个脚本有两个检查:
如果满足上述 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`);
}
希望这对某些人有用!
答案 4 :(得分:0)
创建的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());
}
}
答案 5 :(得分:-1)
如果只希望可以快速使用一个公式,请尝试此操作。这里的警告是,生成的值中将没有任何字母字符,但是无论如何,这些字符对于任何目的都应该是“足够好”。 / p>
=RANDBETWEEN(10000000,99999999) & "-" & RANDBETWEEN(1000,9999) & "-" & RANDBETWEEN(1000,9999) & "-" & RANDBETWEEN(1000,9999)& "-" & RANDBETWEEN(1000,9999) & RANDBETWEEN(10000000,99999999)