简体   繁体   English

如何自动删除 Google 表格特定列中电话号码中的多余字符?

[英]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.示例脚本如下。

Sample script:示例脚本:

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("");
}
  • In this script, when above values of 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-7890123 456 7890放入“D”列时,这些值将转换为1234567890

Note:笔记:

  • This script is run by the simple trigger of OnEdit.该脚本由 OnEdit 的简单触发器运行。 So when you directly run the script with the script editor, an error like TypeError: Cannot read property 'range' of undefined occurs.因此,当您直接使用脚本编辑器运行脚本时,会出现TypeError: Cannot read property 'range' of undefined类的错误。 Please be careful this.请注意这一点。

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Google 表格电子表格中删除绑定脚本? - How can I remove a bound script from a Google Sheets Spreadsheet? 如何根据值自动将 Google 表格中的列值转换为超链接? - How can I automatically turn a column value in Google Sheets into a hyperlink based on the value? 如何使用 Google Script 从 Google 表格中提取特定列? - How do I extract a specific Column from google sheets using Google Script? 如何创建自动从 Google 表格获取数据并替换幻灯片模板中的标签的 function? - How can I create a function that automatically takes data from Google Sheets and replaces the tags in a Slides template? 在 Google 表格中,如何将基于特定列值的行复制到新选项卡中,然后将 append 复选框复制到该行? - In Google Sheets, how can I copy a row based on a specific column value into a new tab, and then append checkboxes to the row? 如何解码谷歌表格中的特定字符(unicode) - How to decode specific characters (unicode) in Google Sheets 我可以自动在Google工作表列中记录其他人编辑其他Google工作表的时间吗? - Can I automatically Record in a google sheet column , the timings that other google sheets have been edited by others? Google 表格自动添加额外的行 - Google sheets automatically adding extra rows 如何在Google表格中为特定行设置日期戳? - How can I set up a datestamp in Google Sheets for specific rows? 如何使用 Google Scripts 仅从特定工作表中获取信息? - How can I use Google Scripts to only fetch information from specific sheets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM