[英]How can I automatically remove extra characters from phone numbers in a specific column on Google Sheets?
I have a Google spreadsheet where multiple users input 10 digit phone numbers on column D. I would like all phone numbers to be formatted with ONLY the 10 digits and nothing extra.我有一个谷歌电子表格,其中多个用户在 D 列输入 10 位数的电话号码。我希望所有电话号码的格式都只有 10 位数,没有额外的。 No spaces, parenthesis, or hyphens.没有空格、括号或连字符。 How can this be accomplished automatically as soon as a user inputs the number?用户输入数字后,如何自动完成? Currently I am manually doing this by selecting the entire column and using the Find and Replace function.目前,我通过选择整个列并使用查找和替换 function 手动执行此操作。 I have attempted to record a macro using this but have not had any luck.我曾尝试使用它来录制宏,但没有任何运气。
Example Inputs:示例输入:
123-456-7890 123-456-7890
(123)456-7890 (123)456-7890
123 456 7890 123 456 7890
Example Output Result:示例 Output 结果:
1234567890 1234567890
You can also do it by a single formula您也可以通过一个公式来完成
=REGEXREPLACE(A2,"[^0-9]","")
I believe your goal as follows.我相信你的目标如下。
You want to convert the following situation using Google Apps Script.您想使用 Google Apps 脚本转换以下情况。
From从
123-456-7890 (123)456-7890 123 456 7890
To至
1234567890
You want to run the script when the value is put to the column "D".您希望在将值放入“D”列时运行脚本。
In this case, I would like to run the function using OnEdit trigger as the simple trigger.在这种情况下,我想使用 OnEdit 触发器作为简单触发器来运行 function。 And, in order to replace the value, I would like to propose to use TextFinder.而且,为了替换值,我想建议使用 TextFinder。 The sample script is as follows.示例脚本如下。
Please copy and paste the following script to the script editor of Google Spreadsheet.请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中。 And, please set the sheet name of the sheet you want to use.并且,请设置您要使用的工作表的工作表名称。 In order to run the script, please put the value to the column "D".为了运行脚本,请将值放入“D”列。
function onEdit(e) {
const sheetName = "Sheet1"; // Please set the sheet name.
const range = e.range;
if (range.getSheet().getSheetName() != sheetName || range.columnStart != 4) return;
range.createTextFinder("[-() ]").useRegularExpression(true).replaceAllWith("");
}
123-456-7890
, (123)456-7890
, 123 456 7890
are put to the column "D", those values are converted to 1234567890
.在此脚本中,当将上述值123-456-7890
、 (123)456-7890
、 123 456 7890
放入“D”列时,这些值将转换为1234567890
。TypeError: Cannot read property 'range' of undefined
occurs.因此,当您直接使用脚本编辑器运行脚本时,会出现TypeError: Cannot read property 'range' of undefined
类的错误。 Please be careful this.请注意这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.