如何将 Google Apps Script 中的变量结果打印到控制台?

huangapple go评论66阅读模式
英文:

How do I print the result of a variable in google apps script to the console?

问题

我目前正在运行这段代码:

function getRandomValues() {
  var values = [];
  for (var i = 0; i < 100; ++i) {
    values.push([Math.floor((Math.random()*100)+1)]);
  }
  return values;
   Logger.log(values);
}

我希望它能在1到100之间选择一个随机数,然后将其打印到控制台。就我所知,选择随机数的部分工作得很好,但在继续之前,我想检查一下,我很难在任何我能看到的地方得到输出。

我已经在Google上进行了大量的研究,以查找其他人是否遇到了同样的问题,这导致我使用了 "Logger.log",但无论我之后做什么,都无法在控制台上看到值。甚至当我尝试在logger.log括号中只写("Hello World!")时,它也不起作用。我尝试在括号之间放入了许多东西,如(var values),(values []),(return values),(values result)和(values return),但都没有效果。正如你现在可以看到的,我对Google应用脚本非常陌生,我在尝试学习它以创建一个简单项目的前3小时内。我看到我的 "Logger.log(values);" 在我的脚本中是灰色的,我必须假设这是其中一个问题,但我不知道如何解决。我在网上找到的唯一事情是人们没有正确声明变量。任何帮助将不胜感激。

英文:

I am currently running this code:

```
function getRandomValues() {
  var values = [];
  for (var i = 0; i < 100; ++i) {
    values.push([Math.floor((Math.random()*100)+1)]);
  }
  return values;
   Logger.log(values);
}

```

Which I was hoping would pick a random number between 1 and 100, then print it to the console. For all I know the picking a number section is working perfectly well but I want to check it before continuing and I'm struggling to get an output anywhere that I can see.

I have tried a good amount of research on google to find other people experienceing the same issues which led me to using "Logger.log" but no matter what I do after that I don't get a value to the console. Even when I try just writing ("Hello World!") within the logger.log brackets it doesn't work. I have tried putting many things between the brackets such as (var values), (values []), (return values), (values result) and (values return) but nothing works. As I'm sure you can tell by now I am incredibly new to google apps script and I am within my first 3 hours of trying to learn it to create a simple project. I do see that my "Logger.log(values);" is greyed out in my script and have to assume that is one of the issues but I don't know how to fix that. The only thing I find online is people not declaring variables properly. Any help would be greatly appreciated.

答案1

得分: 0

在你的脚本中,Logger.log(values)return values 之后。在当前阶段,Google Apps Script 是以同步方式运行的,而你的函数在 return values 处结束。因此,脚本在运行 Logger.log(values) 后就结束了。我认为这是你当前问题的原因。

为了在日志中显示 values,请将 Logger.log(values) 放在 return values 的前面。所以,请将它修改如下:

function getRandomValues() {
  var values = [];
  for (var i = 0; i < 100; ++i) {
    values.push([Math.floor((Math.random() * 100) + 1)]);
  }
  Logger.log(values); // <--- 这里
  return values;
}

通过这个修改,在运行函数 getRandomValues() 时,你可以在日志中看到 values 的值。

英文:

In the case of your following script,

function getRandomValues() {
  var values = [];
  for (var i = 0; i < 100; ++i) {
    values.push([Math.floor((Math.random()*100)+1)]);
  }
  return values;
   Logger.log(values);
}

Logger.log(values) is put after return values. In the current stage, Google Apps Script is run with the synchronous process and your function finishes at return values. By this, the script is finished after Logger.log(values) is run. I thought that this is the reason for your current issue.

In order to show values in the log, please put Logger.log(values) before the line of return values. So, please modify it as follows.

function getRandomValues() {
  var values = [];
  for (var i = 0; i < 100; ++i) {
    values.push([Math.floor((Math.random() * 100) + 1)]);
  }
  Logger.log(values); // <--- here
  return values;
}

By this modification, when you run the function getRandomValues(), you can see the values of values in the log.

huangapple
  • 本文由 发表于 2023年7月14日 06:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定