有没有一种方法可以一次在Google应用程序脚本的Gmail线程中添加多个标签? [英] Is there a way to add multiple labels at once to a Gmail thread in a google app script?
问题描述
我正在尝试以编程方式在Google-app脚本中向Gmail线程添加一些标签.
I am trying to add some labels to Gmail threads in a google-app script programmatically.
使用 thread.addLabel 现在起作用.因为我要向多个线程添加多个标签,所以这种方法首先会导致配额使用效率低下,效率低下.我必须为每个线程的每个标签调用thread.allLabel.
Using thread.addLabel function right now. Because I am adding multiple labels to many threads, this approach is first slow and second inefficient in quota usage. I have to call thread.allLabel for every label for every thread.
是否可以将多个标签一起添加到Gmail线程/邮件中?Gmail API具有 thread.modify 函数,可用于添加多个标签.Google Apps脚本中是否有类似内容?
Is there a way to add multiple labels to Gmail threads / messages together? The Gmail API has a thread.modify function that can be used to add multiple labels. Is there something similar in google apps script ?
推荐答案
您要使用Google Apps脚本向线程添加多个标签.如果我的理解正确,那么如何使用高级Google服务的Gmail API?在GmailApp中,找不到用于向线程添加多个标签的方法.因此,我提出了这一建议.我认为您可以使用 gmail.users.messages.modify
实现它.示例脚本如下.为了使用此脚本,请在高级Google服务和API控制台中启用Gmail API.
You want to add several labels to a thread using Google Apps Script. If my understanding is correct, how about using Gmail API of Advanced Google Services? In GmailApp, I couldn't find methods for adding several labels to a thread. So I propose this. I think that you can achieve it using gmail.users.messages.modify
. The sample script is as follows. In order to use this script, please enable Gmail API at Advanced Google Services and API console.
- 在脚本编辑器上
- 资源->高级Google服务
- 打开Gmail API v1
- 在脚本编辑器上
- 资源-> Cloud Platform项目
- View API控制台
- 在入门"中,单击启用API"并获取凭据(例如密钥).
- 在左侧,单击库.
- 在搜索API和服务,输入"Gmail".然后点击Gmail API.
- 单击启用"按钮.
- 如果已启用API,请不要关闭.
如果现在要使用用于使用Gmail API的脚本打开脚本编辑器,则可以通过访问以下URL
If now you are opening the script editor with the script for using Gmail API, you can enable Gmail API for the project by accessing this URL https://console.cloud.google.com/apis/api/gmail.googleapis.com/overview
- 检索标签列表.
- 使用标签名称检索标签ID.
- 将标签添加到线程中.
示例脚本:
var userId = "me"; var threadId = "### thread ID ###"; // Please input a thread ID. var labels = ["### label name 1 ###", "### label name 2 ###"]; // Please input label names here. var labelList = Gmail.Users.Labels.list(userId).labels; var labelIds = labels.map(function(e){return labelList.filter(function(f){return f.name == e})[0].id}); Gmail.Users.Messages.modify({"addLabelIds": labelIds}, userId, threadId);
注意:
- 这是一个简单的示例脚本.因此,请根据您的环境进行修改.
如果我误解了你的问题,对不起.
If I misunderstand your question, I'm sorry.
这篇关于有没有一种方法可以一次在Google应用程序脚本的Gmail线程中添加多个标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!