Google表格支持JavaScript function.bind()吗? [英] Is JavaScript function.bind() supported in google sheets?

查看:62
本文介绍了Google表格支持JavaScript function.bind()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试在Google表格脚本中运行的简单代码.目的是为 foreach 回调函数提供其他参数.

Here a simple code i'm trying to run in google sheets script. The purpose is supplying the foreach callback function additional parameters.

function print(str, num) {
    Logger.log(str + ": " + num);
}

function test()
{
  var array = [1,2,3];
  var str = "Stam";
  //This line has an exception
  // TypeError: Cannot convert null to an object
  array.forEach(print.bind(null, str));
}
test();

此代码基于此处所述的解决方案.

this code is based on the solution described here.

我知道还有其他解决方案,尽管我想了解为什么这个解决方案不起作用.

I know there are other solutions, though i want to understand why this one does not work.

我可能更伤人,谷歌床单可能不支持.

I wounder maybe it is not supported with google sheets.

推荐答案

这个答案怎么样?请认为这只是几个可能的答案之一.

How about this answer? Please think of this as just one of several possible answers.

在Javascript中,当使用 bind(null,str) null 时,将使用 this .在Google Apps脚本中,当 null 用于 bind(null,str)时,会发生错误,例如无法将null转换为对象".我认为这可能是Google方面的规范.我不确定在将来的更新中是否对此做了修改.因此,作为当前解决方法,如何使用 this 而不是 null ?或者,如果您想在严格模式下像 null 一样使用 bind(null,str),那么使用 {} 代替null bind({},str)?

At Javascript, when null of bind(null, str) is used, this is used. At Google Apps Script, when null is used for bind(null, str), an error occurs like "Cannot convert null to an object". I think that this might be the specification of Google side. I'm not sure whether this is modified in the future update. So as the current workaround, how about using this instead of null? Or if you want to use bind(null, str) like null under the strict mode, how about using {} instead of null like bind({}, str)?

顺便说一句,我认为可以删除脚本最后一行中的 test(); .因为在您的情况下,在脚本编辑器上运行 test()时, test() test(); 运行两次最后一行.最后一行的 test(); 作为全局变量运行.

By the way, I think that test(); at the last line of your script can be removed. Because in your case, when test() is run at the script editor, test() is run 2 times by test(); at the last line. test(); at the last line is run as the global.

从上述情况来看,修改脚本后,如何进行以下修改?

From above situation, when your script is modified, how about the following modification?

function print(str, num) {
  Logger.log(str + ": " + num);
}

function test() {
  var array = [1,2,3];
  var str = "Stam";
  array.forEach(print.bind(this, str));
}

或者您还可以如下修改脚本的 test().以下 test()与上面的结果相同.

or you can also modify test() of your script as follows. The following test() retrieves the same result with above one.

function test() {
  var array = [1,2,3];
  var str = "Stam";
  array.forEach(function(e) {print(str, e)});
}

结果:

在脚本编辑器中运行 test()时,可以在日志中看到以下结果.

Result:

When you run test() at the script editor, you can see the following result at the log.

Stam: 1
Stam: 2
Stam: 3

参考:

  • Function.prototype.bind()
  • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

    If I misunderstood your question and this was not the direction you want, I apologize.

    这篇关于Google表格支持JavaScript function.bind()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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