Google脚本/基本JavaScript - 由调试器修复的问题 [英] Google Scripts/Basic JavaScript - Issue fixed by debugger

查看:111
本文介绍了Google脚本/基本JavaScript - 由调试器修复的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Google表格添加Google脚本附加功能,但我试图在将其设置在表单上之前让脚本正常工作。下面的代码工作正常,如果我在extractNumbers函数中设置了一个断点。如果我只是执行没有断点的代码,我得到一个错误:


TypeError:无法调用未定义的方法replace。 (第36行,文件)


以下是代码:

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

函数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;
出口;
} else if(newCell === oldHome& newHome === oldCell){
return oldCell;
出口;
}

if(newCell ===''&& oldCell!==''){
return oldCell;
出口; ($ new



出口;
}

返回未找到值;
出口;
}

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

现在我意识到我的if / then逻辑比较不雅观,但为我的目的,快速和肮脏是好的。我只是需要它运行。



另外,我已经阅读了其他新手JavaScript程序员有类似的问题相关的代码执行序列。如果有人想链接到一个针对非高级读者的简明信息来源,那也会很棒。编辑:编辑:我把我的代码放到一个新的小提琴中,它工作正常,但它仍然在Google脚本编辑器中失败,除非运行在带有断点的调试模式下。问题似乎是函数参数对函数不可用,除非有断点。任何人都可以访问Google脚本,可以尝试从 https://jsfiddle.net/hrzqg64L/ 更新的代码?

解决方案

没有任何建议可以解决问题的根源 - 您的答案也没有,尽管您避免了这个问题通过在任何地方放置一个附件。



没有AJAX,没有异步行为 - 它比这更简单。 参数阴影同样也是一种红鲱鱼。糟糕的编码习惯,是的 - 但不是一个因素。


如果有人想链接到一个简洁的源码,观众,这将是伟大的。


对不起 - 没有这样的事情。我可以解释发生了什么,但不能保证它可以被新手访问。



例外





正如所写, extractNumbers()我们只是澄清导致异常的原因或抛出的错误。如果它有一个传递给它的空参数(或任何非字符串参数),$ c>将抛出一个异常。如果您选择 extractNumbers()然后点击运行,您将获得:


TypeError:不能调用未定义的方法替换。 (第36行,文件)


这就是告诉你在第36行,它是 return str .replace(/ \D + / g,''); ,变量 str 包含一个未定义的对象(...并且没有 replace()方法)。



你可以检查你的参数以确保它们是有效的,并且适当地处理它们。有时候这可能是有效的默认值,有时候您可能会返回一个错误或抛出一个更明确的参数问题的异常。



在Google调试器中运行代码



在Google调试器中运行代码的唯一方法是选择一个函数,然后选择运行或调试。假设你发布了所有的代码,你只有两个函数可供选择:


  • phoneCheck() extractNumbers()



无论何时Google Apps脚本运行脚本的任何部分,整个脚本都会被加载并扫描以查找所有符号&检查语法。所有符号的范围也被注意到,函数和全局符号之间的任何依赖关系(任何闭包或代码块之外的符号)也是如此。

这需要一些时间。当被要求执行特定功能时,为了加快速度,全局符号只有在被请求的函数或函数可能调用的依赖关系的情况下才被评估。还有另一个条件会触发全局符号的评估,也就是说,如果调试器可能需要停止并显示值的话。



当发生这种情况时,任何在闭包之外的代码(例如 function 之外的)将被执行。 这是您在设置断点时观察到的情况。



为什么在设置断点时工作?



正如所解释的那样,仅仅设置一个断点就会触发对全局符号的评估。



你用几行代码开始编写脚本关闭:

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

正是这样的代码才能正确调用 phoneCheck()带参数。因为 myVar 被评估, phoneCheck()被参数调用,并且依次调用 extractNumbers ()带有一个已定义的参数。



不幸的是,由于调试器的工作方式,您不能选择自己运行该代码。您需要依靠这些副作用行为。



如何解决这个问题?



简单。不要依赖全局代码来调用被测试的函数。

  function test_phoneCheck(){
var myVar = phoneCheck( A1, A2, 01, 02);
Logger.log(myVar);
}


I'm working on a Google Scripts add on for Google Sheets, but I'm trying to get the script working before I actually set it up on the sheet. 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:

TypeError: Cannot call method "replace" of undefined. (line 36, file "")

Here's 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 realize my if/then logic is more than a bit inelegant, but for my purposes, quick and dirty is fine. I just need it to run.

ALSO, I have read of other novice JavaScript programmers having similar issues related to the sequence of code execution. If someone would like to link to a concise source aimed at a non-advanced audience, that would be great too. Thanks!

EDIT: I put my code into a new fiddle and it works fine, but it continues to fail in Google Scripts editor unless running in debug mode with a breakpoint. The problem seems to be that the function parameters aren't available to the function unless there is a breakpoint. Anyone have access to Google Scripts that can try my updated code from https://jsfiddle.net/hrzqg64L/ ?

解决方案

None of the suggestions got to the root of your problem - and neither did your answer, although you've avoided the problem by putting an enclosure around everything.

There's no AJAX, no asynchronous behavior - it's simpler than that. "Shadowing of parameters" is likewise a red herring. Bad coding practice, yes - but not a factor here.

If someone would like to link to a concise source aimed at a non-advanced audience, that would be great too.

Sorry - no such thing. I can explain what's going on, but can't guarantee it will be accessible to novices.

The exception

Let's just clarify what causes the exception, or thrown error, that you've observed.

As written, extractNumbers() will throw an exception if it has a null parameter (or any non-string parameter) passed to it. If you choose to extractNumbers() then hit "run", you'll get:

TypeError: Cannot call method "replace" of undefined. (line 36, file "")

That is telling you that on line 36, which is return str.replace( /\D+/g, '');, the variable str contains an object that is undefined (...and has no replace() method).

For bullet-proof code, you would check your parameter(s) to ensure they are valid, and handle them appropriately. Sometimes that would be with a valid default, and other times you might return an error or throw an exception that is more explicit about the parameter problems.

Running code in Google's debugger

The only way to run code in Google's Debugger is to select a function, then choose "run" or "debug". Assuming you posted all your code, you had just two functions to choose from:

  • phoneCheck()
  • extractNumbers()

Whenever Google Apps Script runs any part of a script, the entire script is loaded and scanned to find all symbols & check syntax. The scope of all symbols is noted as well, and so are any dependencies between functions and global symbols (symbols outside of any closure, or block of code).

That takes some time. To speed things up when asked to execute a specific function, the global symbols are only evaluated if they are a dependency for the requested function or the functions it may call. There is another condition that will trigger evaluation of global symbols, and that is if there is a possibility that the debugger may need to stop and display values.

When this happens, any code that is outside a closure (outside a function, for example) will be executed. This is what you observed when you set breakpoints.

Why did it work when breakpoints were set?

As explained, just having a breakpoint set triggers evaluation of global symbols.

You start this script with a few lines of code that are not in any closure:

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

It is that code which makes the only proper invocation of phoneCheck() with parameters. Because myVar is evaluated, phoneCheck() gets called with parameters, and in turn calls extractNumbers() with a defined parameter.

Unfortunately, because of the way the debugger works, you cannot choose to run that code yourself. You need to rely on these side-effect behaviors.

How to fix this?

Simple. Don't rely on global code to invoke functions under test. Instead, write an explicit test function, and call that.

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

这篇关于Google脚本/基本JavaScript - 由调试器修复的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆