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-22 17:34:44 26 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.

在我的谷歌工作表的谷歌应用程序脚本项目中,在Code.gs文件中,我有两个函数-当标准GET请求时的doGet和带有触发器设置的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.

奇怪的是,在我的执行选项卡中,doGet函数的日志每次都显示为空。请参阅下图,doGet-run的日志显示“无日志”,而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?

对于每个条件,返回ContentService.createTextOutput(responsePrint);打印response在屏幕上正确打印变量,因此变量不是问题所在。这里可能有什么问题?当我控制台.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.

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



  • 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应用程序,当运行doGet时,您希望显示Logger.log和console.log中的日志,需要将Google Apps脚本项目与Google云平台项目链接和/或使用访问令牌进行请求。Ref(作者:我)根据你的问题,我猜测你的情况如下。



  • 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 Script项目未与GCP项目链接时,需要使用访问令牌。如果未使用访问令牌,则需要将Google Apps Script项目与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:



更多回答

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