gpt4 book ai didi

Google Apps Script's Logger.log() not working for doGet function in code.gs(Google Apps脚本的Logger.log()不适用于code.gs中的doGet函数)

转载 作者:bug小助手 更新时间:2023-10-24 17:54:46 25 4
gpt4 key购买 nike



In a Google Apps Script project for my google sheets, in the Code.gs file, I have two functions - doGet for when standard GET requests, and sendEmail which has a trigger setup on it - if the google form linked to my sheet receives a new response, the trigger runs this function. Both those functions have a Logger.log() command at end, to keep track of runs.

在我的Google工作表的Google Apps脚本项目中,在Code.gs文件中,我有两个函数-doGet用于当标准GET请求时使用,sendEmail具有触发器设置-如果链接到我的工作表的Google表单收到新的响应,触发器将运行此函数。这两个函数在末尾都有一个Logger.log()命令,用于跟踪运行。


Here's a basic structure of my code:

下面是我的代码的基本结构:


function doGet(request) {
var data = request.parameter.data;
var taskmode = request.parameter.mode;
var dataarr = [{}];
dataarr = data.split(" | ");
// code...

if(taskmode == "search") {
// code...
if(condition) var responsePrint = "NA";
else var responsePrint = "Some Data";

Logger.log(responsePrint); // does not work
return ContentService.createTextOutput(responsePrint); //works
} else if(taskmode == "update") {
//some code
if(condition) var responsePrint = "Done";
else var responsePrint = "Not Done";

Logger.log(responsePrint); //does not work
return ContentService.createTextOutput(responsePrint); //works
} else if(taskmode == "new") {
sheet.appendRow(dataarr);
var responsePrint = "Done";
Logger.log(responsePrint); //doesnt work
return ContentService.createTextOutput(responsePrint); //works
}
// .... +4 more such else..if conditions, which have both Logger.log() which does not work & ContentService ...() which works.
}

function sendemail(e) {
var data = e['values'];
Logger.log(data); //works

//... other code

var paramslist = [email,name,actcode,date,expiry,version,link];
var params = paramslist.join(" | ");
var finalurl = "https://mywebsite.com/awsdata/sendemail.php?data=" + encodeURIComponent(params);
var response = UrlFetchApp.fetch(finalurl);
Logger.log(response.getContentText()); //works
}

What's strange is, in my execution tab, the logs for doGet function show empty every time. See the below image, logs for doGet run shows "No logs", whereas logs for sendEmail show what they're supposed to (as in code above) - first line is the raw list, then is the response of the fetch command. There's no delay in log activity, as 2-3 days old logs show same - empty.

奇怪的是,在我的Execution选项卡中,doGet函数的日志每次都显示为空。如下图所示,doGet运行的日志显示“无日志”,而sendEmail的日志显示它们应该执行的操作(如上面的代码所示)--第一行是原始列表,然后是FETCH命令的响应。日志活动没有延迟,因为2-3天前的日志显示相同-为空。


enter image description here


For every condition, return ContentService.createTextOutput(responsePrint); prints responsePrint variable correctly on the screen, so the variable is not the issue. What could be the problem here? When I console.log() it, it shows. Why is logger.log() working for one (trigger) function but not the other?

对于每个条件,Return ContentService.createTextOutput(responsePrint);都会在屏幕上正确打印响应打印变量,因此变量不是问题所在。这可能是什么问题呢?当我sole.log()它时,它会显示出来。为什么logger.log()可以用于一个(触发器)函数,而不能用于另一个函数?


更多回答

Although I'm not sure whether I could correctly understand your expected result, I proposed an answer. Please confirm it. If I misunderstood your question and that was not useful, I apologize.

虽然我不确定我是否能正确理解您的预期结果,但我提出了一个答案。请确认一下。如果我误解了你的问题,这是没有用的,我道歉。

优秀答案推荐

Although I'm not sure whether I could correctly understand your actual situation, from "if the google form linked to my sheet receives a new response, the trigger runs this function" and your shown script, I guessed the following situation.

虽然我不确定我是否能正确理解你的实际情况,从“如果链接到我的工作表的google表单收到新的响应,触发器运行此函数”和你显示的脚本,我猜到了以下情况。



  • Your showing script is the container-bound script of Google Spreadsheet or the standalone script.

  • Function doGet(request) is run by the HTTP request (GET method) from the outside.

  • Function sendemail(e) is run by the installable trigger of OnSubmit trigger.


Under this condition, I guessed that you wanted to achieve the following goal.

在这种情况下,我猜你想要实现以下目标。



  • You want to know the reason for Why is logger.log() working for one (trigger) function but not the other?.


If my understanding is correct, how about the following answer?

如果我的理解是正确的,那么下面的答案如何?


Answer


About doGet


In the case of Web Apps, when doGet is run, you want to show the log from Logger.log and console.log, it is required to link the Google Apps Script project with the Google Cloud Platform Project and/or request with the access token. Ref (Author: me) From your question, I guessed your situation as follows.

对于Web Apps,当doGet运行时,您希望显示来自Logger.log和console.log的日志,它需要将Google Apps脚本项目与Google Cloud Platform项目和/或请求与访问令牌相链接。参考(作者:我)从你的问题,我猜你的情况如下。



  • Your Google Apps Script project might not linked with the Google Cloud Platform Project.

  • Your HTTP request for requesting doGet might not use the access token.


In those cases, unfortunately, the log by Logger.log and console.log is not shown in the log. It seems that this is the current specification on the Google side. If you want to see the log from Logger.log and console.log, please see the following table.

遗憾的是,在这些情况下,Logger.log和console.log的日志不会显示在日志中。这似乎是谷歌方面目前的规范。如果您想查看Logger.log和console.log中的日志,请参见下表。























Without access token With access token
Without linking GCP Apps Script Dashboard
With linking GCP Stackdriver Apps Script Dashboard and Stackdriver


In this table, the condition for showing logs from Logger.log and console.log can be seen. For example, when the Google Apps Script project is not linked with the GCP project, it is required to use the access token. If the access token is not used, it is required to link the Google Apps Script project with the GCP project.

在该表中,可以看到显示Logger.log和console.log中的日志的条件。例如,当Google Apps脚本项目没有链接到GCP项目时,它需要使用访问令牌。如果没有使用访问令牌,则需要将Google Apps脚本项目与GCP项目链接起来。


About sendemail


In the case of the installable trigger of the OnSubmit trigger, the script is run as the owner of the script and the scopes have already been authorized. By this, when the script is run by the trigger, the log from Logger.log and console.log can be seen.

在OnSubmit触发器的可安装触发器的情况下,脚本作为脚本的所有者运行,并且作用域已经获得授权。这样,当脚本由触发器运行时,可以看到来自Logger.log和console.log的日志。


Reference:



更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com