[英]Google Scripts / Sheets Add prefix to data once it has been entered into the cell
Objectives目标
CSC100000001291
使用零填充确保总字符串长度为 = 15,因此单元格值为CSC100000001291
Can be done using a formula but across two columns but would prefer to use one column as it look tidier.可以使用公式完成,但跨越两列,但更喜欢使用一列,因为它看起来更整洁。
eg [User Input] = [Final Cell Value]例如 [用户输入] = [最终单元格值]
- 1291 = CSC100000001291
- 12922 = CSC100000012922
Currently trying to work this one out目前正在努力解决这个问题
Use the following:使用以下内容:
padStart()
to add the leading zeros padStart()
添加前导零replace()
the first 4 zeros with "CSC1" (since only the first occurrence will be replaced if passing a string instead of regular expression)用“CSC1” replace()
前4个零(因为如果传递字符串而不是正则表达式,则只会替换第一个出现的零)setValue()
to update the edited cell setValue()
更新编辑的单元格function onEdit(e) {
if (e.range.columnStart == 1) { // Column A
const padded = e.value.padStart(15, 0);
e.range.setValue(padded.replace('0000', 'CSC1'));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.