未正确引用单元格

Not properly referencing cells

我正在为大型数据库编写 phone 数字格式化程序。一切正常,但空白单元格打印不一致。大多数单元格都已通读且格式正确,但有些单元格在错误的单元格中显示空白输出。

我曾尝试通过将 cleanNumber 变量重置为空白字符串来解决此问题,但这只是在第 33 行的 indexOf() 中引发了另一个问题。

function myFunction() {
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // connects sheet to code
  var startRow = 18;
  var endRow = 41;
  for (var i = startRow; i <= endRow; i++) {     // i = currnet row | row to end at | add 1 to count each time
    var workingCell = activeSheet.getRange(i, 2).getValue();
    Logger.log("Original number: " + workingCell)
    //If blank, move to next row
    exit: if (workingCell.length == 0.0) {
      var blank = "";
      activeSheet.getRange(i, 3).setValue(blank);
      Logger.log("This row is blank")
      //break exit;
    }
    // cleanNumber if it isn't formatted already 
    else if (isNaN(workingCell)) { // runs if active cell is not a preformatted number 
      var cleanNumber = workingCell.replace(/\D/g, '');  // removes all non-numeric values
      activeSheet.getRange(i, 3).setValue(cleanNumber);
      Logger.log("Extra char's removed: " + cleanNumber)
    }
    // runs if active cell is already preformatted 
    else {
      activeSheet.getRange(i, 3).setValue(workingCell);
      Logger.log("No need for formatting: " + workingCell)
    }
    // If cleanNumber has a country code(+1), remove it
    if ((cleanNumber.indexOf("1")) == 0) {
      cleanNumber = cleanNumber.substring(1); //removes first character = "1"
      activeSheet.getRange(i, 3).setValue(cleanNumber);
      Logger.log("Country code removed: " + cleanNumber);
    }
    // If number is longer than 10 characters, create an extension variable - with entire number, remove 10 characters from front
    if (cleanNumber.length > 10.0) {
      var extension = cleanNumber.substring(10, 15);
      var phoneNumber = cleanNumber.substring(0, 10);
      var formatted = phoneNumber.slice(0, 3) + "-" + phoneNumber.slice(3, 6) + "-" + phoneNumber.slice(6, 15);
      var finalPhoneNumber = formatted + " ext. " + extension;
      activeSheet.getRange(i, 3).setValue(finalPhoneNumber);
      Logger.log("This number is in its final ext. format: " + finalPhoneNumber);
    }
    //if number doesnt have an extension, put it into final format
    else if (cleanNumber.length = 10.0) {
      var frontFinal = cleanNumber.substring(0, 3);
      var midFinal = cleanNumber.substring(3, 6);
      var endFinal = cleanNumber.substring(6, 10);
      var finalNumber = frontFinal + "-" + midFinal + "-" + endFinal;

      activeSheet.getRange(i, 3).setValue(finalNumber);
      Logger.log("This number is in its final format: " + finalNumber);
    }
    //if number is less than 10 numbers
    else {
      Logger.log("This number is shorter than 10 numbers" + cleanNumber);
    }
    cleanNumber = " ";
  }
}

预格式化的数字在左边,输出在右列。

这是一些示例数据,请注意问题似乎源于空白行。

未格式化
1999-111-1111
1+2222-222222
4444444444 分机号。 223
9738094395
9172609107
866.786.6682
973 330 2212
(631)563-4000 分机号。 234

我期待解决这个问题,谢谢你的帮助:)

您可以使用 ARRAYFORMULA 或在脚本中使用 RegExp。

=ArrayFormula(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(TO_TEXT(A2:A),"\D",),"^(?:1)?(\d{3})(\d{3})(\d{4})(\d{0,5}).*$","-- ext. ")," ext\. $",))

推荐您use batch operations

const values = [
  ['1999-111-1111'],
  ['1+2222-222222'],
  ['4444444444 ext. 223'],
  [9738094395],
  [9172609107],
  ['866.786.6682'],
  ['973 330 2212'],
  ['(631)563-4000 ext. 234'],
  ['973-809-4395'],
  ['']
];

const results = [];
for (const value of values) {
  const cleanNumber  = value[0].toString().replace(/\D/g, '');
  const m = cleanNumber.match(/^(?:1)?(\d{3})(\d{3})(\d{4})(\d{0,5}).*$/);
  if (m) {
    let finalNumber = `${m[1]}-${m[2]}-${m[3]}`;
    if (m[4]) { finalNumber += ` ext. ${m[4]}`; }
    results.push([finalNumber]);
  }
  else {
    results.push(value);
  }
}
console.log(results.flat());