Google apps script 来自触发器的Gmail API message.list
我在创建每天晚上运行的google脚本时遇到了问题。当我从脚本文件运行代码时,代码运行良好,并表现出预期的行为,但是当由安装的触发器执行时,我得到以下结果: TypeError:无法从undefined中读取属性“length”。(第17行,文件“主”) 编辑:要清楚,我知道使用的特定查询必须返回结果,因为从脚本编辑器运行相同的脚本工作正常 代码:Google apps script 来自触发器的Gmail API message.list,google-apps-script,gmail-api,Google Apps Script,Gmail Api,我在创建每天晚上运行的google脚本时遇到了问题。当我从脚本文件运行代码时,代码运行良好,并表现出预期的行为,但是当由安装的触发器执行时,我得到以下结果: TypeError:无法从undefined中读取属性“length”。(第17行,文件“主”) 编辑:要清楚,我知道使用的特定查询必须返回结果,因为从脚本编辑器运行相同的脚本工作正常 代码: function doGet(query) { var sSheet = sheetSelect(), //calls spreadsheet
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
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1d
{
"messages": [
{
"id": "150612f9d7f83db9",
"threadId": "150611d4e92b7a5f"
}, ...
]
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1s
{
"resultSizeEstimate": 0
}
你知道为什么它的行为如此不同吗?我已尝试为特定电子邮件提供用户ID。似乎这可能是某种身份验证问题,但我不知道如何解决它,除了忘记Gmail API,转而使用Gmail应用程序,因为这似乎是Gmail API方法消息的问题。list()
提前感谢您的帮助
我设法解决了这个问题。问题是我想留下一个选项来传递带有函数调用的查询。问题是,已安装的触发器实际上将一个变量传递给查询变量,然后不设置新的变量。我认为这要简单得多。如果我在最后一天列出邮件,我会得到: 请求:
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
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1d
{
"messages": [
{
"id": "150612f9d7f83db9",
"threadId": "150611d4e92b7a5f"
}, ...
]
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1s
{
"resultSizeEstimate": 0
}
响应:
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
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1d
{
"messages": [
{
"id": "150612f9d7f83db9",
"threadId": "150611d4e92b7a5f"
}, ...
]
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1s
{
"resultSizeEstimate": 0
}
如果我在最后一秒钟列出邮件,我会得到:
请求:
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
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1d
{
"messages": [
{
"id": "150612f9d7f83db9",
"threadId": "150611d4e92b7a5f"
}, ...
]
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1s
{
"resultSizeEstimate": 0
}
响应:
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
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1d
{
"messages": [
{
"id": "150612f9d7f83db9",
"threadId": "150611d4e92b7a5f"
}, ...
]
}
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than%3A1s
{
"resultSizeEstimate": 0
}
换句话说,
queryedmessages.messages
如果在该特定查询中没有收到任何消息,则queryedmessages.messages.length
将导致错误。但是该特定查询应返回消息。我试图从编辑器中运行脚本,然后在一分钟后触发。当通过触发器运行时,它不返回任何内容,但从编辑器运行时,它返回63条消息。如果我指定我的电子邮件而不是使用“我”标识符,则触发器似乎没有以用户身份运行应用程序,或者没有身份验证来查看用户的电子邮件