How do I rewrite this code for V8 from Rhino?

前端 未结 2 1106
天命终不由人
天命终不由人 2021-01-25 18:56

I used to a script on GAS. Because I do manage attendance by Chat and Google spread sheet with GAS. Chat tool is Chat work. It works on Gas(Rhino). But It doesn\'t work V8.

相关标签:
2条回答
  • 2021-01-25 19:52

    Google apps script switched runtime engines from Rhino to V8. This caught me by surprise too and borked a bunch of my code, including date and year manipulation.

    Years are handled differently. getYear() no longer works as expected.

    You can switch back to the old Rhino. See Enabling the Rhino Runtime

    There is info about V8 available at https://developers.google.com/apps-script/guides/v8-runtime

    Google has published a list of incompatibilities and other differences which contains good documentation about work arounds.

    Full link for script migration: https://developers.google.com/apps-script/guides/v8-runtime/migration

    Fortunately it's not the drama I was expecting. It would have been nice to have been given a heads up beforehand though.

    0 讨论(0)
  • 2021-01-25 19:53

    See the official documention here: https://developers.google.com/apps-script/guides/v8-runtime/migration#avoid_for_eachvariable_in_object

    In short:

    for each (var obj in json) {
      do_something_with(obj);
    }
    

    becomes

    for (var obj of json) {
      do_something_with(obj);
    }
    

    Note that in changed to of, which makes the for-loop iterate over values rather than keys. Accordingly, you can also use a traditional for-in loop, and manually use the key to get the value:

    for (var obj_key in json) {
      var obj = json[obj_key];
      do_something_with(obj);
    }
    

    You can also use let instead of var to get block-scoped variables, which are generally more intuitive to use, but that's an unrelated change and is not required if you just want things to work.

    0 讨论(0)
提交回复
热议问题