Google Scripts / Basic JavaScript - fixed by debugger

I am working on Google scripts for Google Sheets, but I am trying to get the script to work before I actually set it on a worksheet. The code below works fine if I set a breakpoint somewhere in the extractNumbers function. If I just execute the code without breakpoints, I get an error message:

TypeError: Unable to call the replace method from undefined. (line 36, file "")

Here is the code:

var myVar = phoneCheck("a1","a2","o1","o2");
Logger.log(myVar);

function phoneCheck(newCell,newHome,oldCell,oldHome) {
  Logger.clear();
  var newCell = extractNumbers(newCell);
  var oldCell = extractNumbers(oldCell);
  var newHome = extractNumbers(newHome);
  var oldHome = extractNumbers(oldHome);

  if (newCell === oldCell) {
    return newCell;
    exit;
  } else if (newCell === oldHome && newHome === oldCell) {
    return oldCell;
    exit;
  }

  if (newCell === '' && oldCell !== '' ) {
    return oldCell;
    exit;
  }

  if (newCell !== oldCell && newCell !== oldHome) {
   return newCell;
    exit;
  }

  return "No value found";
  exit;
}

function extractNumbers(input) {
  Logger.log(input);
  var str = input;
  return str.replace( /\D+/g, '');
}

Now I understand that my if / then logic is more than a little inefficient, but for my purposes it’s quick and dirty. I just need to run it.

, JavaScript, , . - , - , . !

EDIT: , , Google, . , , . Google, https://jsfiddle.net/hrzqg64L/?

+1
2

- , , .

AJAX, - . " " . , - .

- , - , .

- . , , , .

, , .

, extractNumbers() , ( ), . extractNumbers(), "run", :

TypeError: "replace" undefined. ( 36, "")

, 36, return str.replace( /\D+/g, '');, str , undefined (... replace()).

(), , , . , , .

Google

Google Debugger - , "" "". , , :

  • phoneCheck()
  • extractNumbers()

, Google Apps Script - script, Script, . , ( ).

. , , , , . , , , .

, , ( , ). , .

, ?

, .

Script , :

var myVar = phoneCheck("a1","a2","o1","o2");
Logger.log(myVar);

phoneCheck() . myVar , phoneCheck() , , extractNumbers() .

, - , , . .

?

Simple. . .

function test_phoneCheck() {
  var myVar = phoneCheck("a1","a2","o1","o2");
  Logger.log(myVar);
}
+2

, , .

, Google Script. , Script void, ! , Script :

function init () {
  var numberArray = ["a3", "a2", "o3", "o10"];
  var myVar = phoneCheck(numberArray);
  Logger.log(myVar);

  function phoneCheck(myArray) {
    var phoneString = '';
    Logger.clear();
    var arrayLength = myArray.length;
    for (i = 0; i < arrayLength; i++) {
      phoneString += myArray[i].replace(/\D+/g, '');
    }
    return phoneString;
  }
}

, , Script , . , , Script .

, ! , .

0

Source: https://habr.com/ru/post/1607736/


All Articles