来自触发器的Gmail API message.list [英] Gmail API message.list from trigger
问题描述
我在创建每晚运行的google脚本时遇到问题.当我从脚本文件运行该代码时,该代码运行良好,并且运行正常,但是当由已安装的触发器执行时,我得到以下信息:
I am having a problem creating an google script that would run every night. The code runs fine when I run it from the script file and behaves as expected, however when performed by an installed trigger I get the following:
TypeError:无法从未定义中读取属性"length". (第17行,主要"文件)
TypeError: Cannot read property "length" from undefined. (line 17, file "Main")
要清楚一点,我知道所使用的特定查询必须返回结果,因为从脚本编辑器运行相同的脚本可以正常工作
代码:
function doGet(query) {
var sSheet = sheetSelect(), //calls spreadsheet selection function and assigns the spreadsheet to variable
queriedMessages, //object to store the queried messages list
pageToken, //string token value that will be pulled from the queredMessages
auth = 'me';
if (!query) query = 'in:all newer_than:1d -is:chats -in:trash';
do {
queriedMessages = Gmail.Users.Messages.list(auth, {'q':query, 'pageToken':pageToken}); //callls the Gmail API to query messages
dataOutput(sSheet, queriedMessages.messages, queriedMessages.messages.length); //calls function to output all data to spreadsheet from the current list
pageToken = queriedMessages.nextPageToken; //gets the next page token from the list
}
while (pageToken); //the loop is executed until there are no more next page tokens left
}
任何想法为何其表现如此不同?我尝试为特定的电子邮件提供userId.似乎这可能是某种身份验证问题,但是除了忘记Gmail API并采取一种绕行方式使用Gmail App之外,我无法找出解决方法,因为这似乎是Gmail API方法messages.list()的问题
Any ideas why it behaves so differently? I have tried providing userId for a specific e-mail. Seems like this might be some kind of authentication issue but I cannot figure out how to fix it other than forgetting about Gmail API and go a roundabout way of using Gmail App as it seems to be an issue with Gmail API method messages.list()
谢谢您的任何帮助!
我设法解决了这个问题.问题是我想留下一个选项来传递带有函数调用的查询.然后的问题是,已安装的触发器实际上将一个变量传递给了查询变量,然后就没有设置新的触发器.
I managed to fix the issue. The problem was me wanting to leave an option to pass on a query with the function call. The problem then is that the installed trigger actually passed on a variable to the query variable and a new one is then not set.
推荐答案
我认为这要简单得多.如果我在最后一天列出邮件,则会得到:
I think it's much simpler than that. If I list messages in the last day, I get:
请求:
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1d
回复:
{
"messages": [
{
"id": "150612f9d7f83db9",
"threadId": "150611d4e92b7a5f"
}, ...
]
}
如果我在最后一秒列出了消息,则会得到:
If I list messages in the last second, I get:
请求:
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1s
回复:
{
"resultSizeEstimate": 0
}
换句话说,如果您没有收到与该特定查询相关的消息,则queriedMessages.messages
将为undefined
,而queriedMessages.messages.length
将会引起您的错误.
In other words, queriedMessages.messages
will be undefined
if you get no messages with that particular query, and queriedMessages.messages.length
will give rise to your error.
这篇关于来自触发器的Gmail API message.list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!