由于箭头功能,简单的Javascript代码无法在Google文档中使用 [英] Simple Javascript code not working in Google docs due to arrow function

查看:58
本文介绍了由于箭头功能,简单的Javascript代码无法在Google文档中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将一段简单的代码实现为google docs中的脚本.它的功能是验证ID号.问题在于,此代码所使用的语法对于google docs脚本而言过于先进-它使用的箭头功能据我所知不被google docs支持.尝试重写代码是行不通的-我是一个初学者,虽然知道如何阅读适应代码的代码对我来说要复杂得多.谁能建议如何将箭头功能改回单独的功能?

I have tried implementing a piece of simple code as a script in google docs. It's function is validating ID numbers. The problem is that the syntax used for this code is too advanced for the google docs script - it uses the arrow function which is as fas I as I understand not being supported by google docs. Trying to rewrite the code just didn't work - I am quite a beginner and while knowing how to read code adapting one is much more complicated for me. Can anyone suggest how to change back the arrow function into a seperate function?

下面是代码:

function isValidIsraeliID(id) {
    var id = String(id).trim();
    if (id.length > 9 || id.length < 5 || isNaN(id)) return false;

    // Pad string with zeros up to 9 digits
    id = id.length < 9 ? ("00000000" + id).slice(-9) : id;

    return Array.from(id, Number)
            .reduce((counter, digit, i) => {
            const step = digit * ((i % 2) + 1);
            return counter + (step > 9 ? step - 9 : step);
                }) % 10 === 0;
}

我相信引起问题的那条线就是这样:

I believe the line causing the problem is this one:

  .reduce((counter, digit, i) => {

但我也很可能是错的...

But I might as well be wrong...

谢谢!

推荐答案

编辑:V8引擎当前支持箭头功能.请参阅: https://developers.google.com/apps-script/guides/v8-runtime#arrow_functions

Arrow functions are currently supported with the V8 engine. See: https://developers.google.com/apps-script/guides/v8-runtime#arrow_functions

Google Apps脚本(GAS)尚不支持ECMAScript 2015(ES6).不幸的是,在当前阶段,不能使用从ES6添加的功能.因此,需要将此类函数转换为GAS.在您的脚本中,也使用了此类功能.那修改呢?

Google Apps Script (GAS) doesn't support ECMAScript 2015 (ES6) yet. Unfortunately, in the current stage, the functions which was added from ES6 cannot be used. So it is required to convert such functions for GAS. In your script, also such functions are used. So how about this modification?

  • 箭头功能不能在GAS上使用.
    • 某些性能和NielsNet提到了这一点.
    • Arrow function cannot be used at GAS.
      • This was mentioned by CertainPerformance and NielsNet.
      • 在这里,我用的是Array.map().
      function isValidIsraeliID(id) {
        var id = String(id).trim();
        if (id.length > 9 || id.length < 5 || isNaN(id)) return false;
      
        // Pad string with zeros up to 9 digits
        id = id.length < 9 ? ("00000000" + id).slice(-9) : id;
      
        return Array.map(id, Number) // Modified
        .reduce(function(counter, digit, i) { // Modified
          const step = digit * ((i % 2) + 1);
          return counter + (step > 9 ? step - 9 : step);
        }) % 10 === 0;
      }
      

      参考文献:

      • 内置的Google Services:基本的JavaScript功能
      • 箭头功能
      • Array.from()
      • Array.prototype.map()
      • References :

        • Built-in Google Services: Basic JavaScript features
        • Arrow functions
        • Array.from()
        • Array.prototype.map()
        • 如果此结果不是您想要的,请告诉我.我想修改它.

          If this result was not what you want, please tell me. I would like to modify it.

          这篇关于由于箭头功能,简单的Javascript代码无法在Google文档中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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