Google Apps脚本-更改Gmail中单个电子邮件的标签 [英] Google Apps Script - Changing label of individual email in Gmail
问题描述
我想以编程方式在Google脚本中的gmail中更改单个电子邮件的标签.我无法使用标准的 GmailApp服务,因为它适用整个线程的操作,而不是单个电子邮件.我发现了一些使用高级Gmail API完成此操作的示例(
I want to change the label of an individual email programatically in gmail in a Google Script. I can't use the standard GmailApp service because it applies actions to the whole thread instead of an individual email. I've found a few examples of this being done with the Advanced Gmail API (Search/replace labels in specific messages (not the whole thread) with Google Apps Script). But I've not had success with this. I keep getting the following error:
提供的参数数量无效.仅预期3-4(第5行,文件测试")
Invalid number of arguments provided. Expected 3-4 only (line 5, file "test")
以下是我在Google脚本编辑器中尝试过的代码段:
Here is a code snippet that I've tried in Google Script's Editor:
function changeLabel() {
var addLabel = '3to-smartsheetstest';
var removeLabel = '3to-smartsheets';
var msgId = '142b7c52e4cc4619';
var msgLabel = Gmail.Users.Messages.modify({
'userId': 'me',
'id': messageId,
'resource':{
'addLabelIds': [addlabel],
'removeLabelIds': [removelabel]
}
})
}
Google的资源文档说:为javascript构建这样的参数:
Google's resource documentation says to structure the parameters like this for javascript:
'userId': userId,
'id': messageId,
'addLabelIds': labelsToAdd,
'removeLabelIds': labelsToRemove
但是似乎实际上需要以这种方式进行结构化(在此处的错误报告中进行了澄清: https://github.com/google/google-api-nodejs-client/issues/312 ):
But it seems that it actually needs to be structured this way (clarified in a bug report here: https://github.com/google/google-api-nodejs-client/issues/312):
'userId': 'some email address',
'id': 'some message id',
'resource':{
'addLabelIds': ['some label id'],
'removeLabelIds': []
}
无论哪种方式,我都会遇到相同的错误.
Either way I get the same error.
推荐答案
此修改如何?
- 请修改
Gmail.Users.Messages.modify()
的变量的拼写.- 将
messageId
修改为msgId
. - 将
addlabel
修改为addLabel
. - 将
removelabel
修改为removeLabel
.
- Please modify the spelling of variables for
Gmail.Users.Messages.modify()
.- Modify
messageId
tomsgId
. - Modify
addlabel
toaddLabel
. - Modify
removelabel
toremoveLabel
.
var msgLabel = Gmail.Users.Messages.modify({ 'userId': 'me', 'id': messageId, 'resource':{ 'addLabelIds': [addlabel], 'removeLabelIds': [removelabel] } })
至:
var msgLabel = Gmail.Users.Messages.modify({ 'addLabelIds': [addLabel], 'removeLabelIds': [removeLabel] }, 'me', msgId);
注意:
- 此修改后的脚本假定在高级Google服务和API控制台中启用了Gmail API.
如果我误解了你的问题,对不起.
If I misunderstand your question, I'm sorry.
这篇关于Google Apps脚本-更改Gmail中单个电子邮件的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Modify
- 将