简体   繁体   English

在 Google Apps 脚本中返回一个数组

[英]Returning an array in Google Apps Script

I am trying to create a function that returns a two-element array value in Google Apps Script.我正在尝试创建一个 function,它在 Google Apps 脚本中返回一个二元素数组值。 Apparently I have misunderstood something because I thought that would be a simple matter of specifying:显然我误解了一些东西,因为我认为这将是一个简单的指定问题:

      return [ value1 ][ value2 ] 

at the end of the function, but that is not working for me.在 function 的末尾,但这对我不起作用。 So as a proof of concept, I wrote the following:因此,作为概念证明,我写了以下内容:

function testReturnArray() {
      var theValue = returnArray();
      Logger.log(theValue);
      }

function returnArray() { return ["a"]["b"]; }

When I run this code through the debugger, the log written is:当我通过调试器运行这段代码时,写入的日志是:

6:33:08 PM  Info    null

Clearly that's not my intended result.显然这不是我想要的结果。 Can you please point me to the problem?你能指出我的问题吗? I really would like to have two values returned from this function, and this is the easiest way I could think to do that.我真的很想从这个 function 返回两个值,这是我能想到的最简单的方法。 (Alternative being to return a class, but that may be overkill for my goal, and may also have a similar issue.) (另一种方法是返回 class,但这对我的目标来说可能有点过头了,而且也可能有类似的问题。)

That's not how you write arrays in JavaScript.这不是您在 JavaScript 中编写 arrays 的方式。 You need to use a comma to separate the values.您需要使用逗号分隔值。 There's a lot of content explaining arrays, but I'll suggest this as one to start with.有很多解释 arrays 的内容,但我建议从这里开始。

 var theValue = returnArray(); console.log(theValue); function returnArray() { return [ "a", "b" ]; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM