简体   繁体   English

Google Scripts / Sheets 一旦数据被输入到单元格中,就为数据添加前缀

[英]Google Scripts / Sheets Add prefix to data once it has been entered into the cell

Objectives目标

  1. Add a Prefix of CSC1[Leading Zeros] to a number such as 1291 or 12922 or 129223 this should apply to the whole column (Column F)将 CSC1[前导零] 的前缀添加到诸如 1291 或 12922 或 129223 之类的数字,这应该适用于整个列(F 列)
  2. Ensure total string length is = 15 using padding of zeros so the cell value would be CSC100000001291使用零填充确保总字符串长度为 = 15,因此单元格值为CSC100000001291
  3. The user would only enter 1291 but I plan to make the script overwrite this to make the the cell value CSC100000001291用户只会输入 1291 但我计划让脚本覆盖它以使单元格值 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:使用以下内容:

  • An onEdit trigger to check when someone has updated a cell用于检查某人何时更新单元格的onEdit 触发器
  • 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.

相关问题 Google Scripts / Sheets 添加前缀国际电话 - Google Scripts / Sheets Add prefix international phone 如何使用Google表格脚本将Google表格单元格数据添加到html电子邮件中? - How do you add google sheet cell data into an html-email using Google Scripts for Sheets? 根据表格的值一次发送Google表格电子邮件 - Google Sheets email once based on cell values 检测到某个单词刚刚在Textarea中输入 - Detect Once a Certain Word Has Just Been Entered in a Textarea Google表格根据单元格编号值添加行具有值的单元格计数 - Google Sheets Add row based on cell number value the count of cell which has value 为行中的任何字段输入信息后,如何添加必填字段,该行中的所有字段也应为必填项 - How to add required filed once information has been entered for any field in the row, all fields in that row should be required too 如何使用脚本在 Google 表格中更新单元格时添加时间戳? - How to time stamp when a cell was updated in Google Sheets using scripts? Google表格脚本-如何引用给定行的单元格? - Google sheets scripts--how to refer to a cell given a row? Apps 脚本 Google 表格 - 根据单元格值移动行 - Apps Scripts Google Sheets - Move Row based on Cell value 表单填写完毕后,添加事件监听器以触发Google adwords跟踪脚本 - Add event listener to trigger Google adwords tracking script once a form has been completed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM