Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 谷歌脚本GmailApp搜索标签或nouserlabels_Google Apps Script_Gmail - Fatal编程技术网

Google apps script 谷歌脚本GmailApp搜索标签或nouserlabels

Google apps script 谷歌脚本GmailApp搜索标签或nouserlabels,google-apps-script,gmail,Google Apps Script,Gmail,我正在尝试创建一个脚本,允许我按标签名或不按用户标签搜索电子邮件及其附件。如果searchForLabels中有多个条目,则脚本不起作用 function searchLabels(){ //if there is more than one entry here, the script does not work var searchForLabels = [ 'has:nouserlabels', 'label:Test1'

我正在尝试创建一个脚本,允许我按标签名或不按用户标签搜索电子邮件及其附件。如果searchForLabels中有多个条目,则脚本不起作用

function searchLabels(){

  //if there is more than one entry here, the script does not work
  var searchForLabels = 
      [
        'has:nouserlabels',
        'label:Test1'
      ];

  for (var l = 0; l < searchForLabels.length; l++) {

    var threads = GmailApp.search('in:inbox newer_than:4d' + searchForLabels);  
    var msgs = GmailApp.getMessagesForThreads(threads);
    Logger.log(searchForLabels)

    if (searchForLabels == 'has:nouserlabels'){    
      for (var i = 0 ; i < msgs.length; i++) {
        for (var j = 0; j < msgs[i].length; j++) {
          var message = msgs[i][j];
          var from = message.getFrom();
          var subject = message.getSubject();
          var getAttachments = message.getAttachments();
          var body = message.getPlainBody();
          var getTo = message.getTo();
          Logger.log(subject)

          for (var k = 0; k < getAttachments.length; k++) {
            var attachment = getAttachments[k];
            var content = attachment.getContentType();

            //rest of my code
          } 
        }      
      }
    }
  }
}
函数searchLabels(){
//如果此处有多个条目,则脚本不起作用
var searchForLabels=
[
“has:nouser标签”,
'标签:Test1'
];
对于(var l=0;l
我对您的代码做了两个小改动。测试后,它会按照您的要求工作

第一个是在
var-threads=GmailApp.search('in:inbox-newer\u-than:4d'+searchForLabels[l])上行。我在
searchForLabels
数组上使用了
l
迭代器。我使用它是因为,阅读这行的上下文,它似乎是合适的。此更改将在标记上迭代。如果这不是你的初衷,请原谅我

第二个更改是脚本工作所必需的。我注释掉了
if(searchForLabels=='has:nouserlabels'){
,因为如果
searchForLabels
数组有多个元素,它将永远不会是
true
。这是您检测到的错误。请注意相应的
}
也是如何注释掉的

这是脚本的最终工作版本:

function searchLabels() {
  //if there is more than one entry here, the script does not work
  var searchForLabels = [
    'has:nouserlabels',
    'label:Test1'
  ];
  for (var l = 0; l < searchForLabels.length; l++) {
    var threads = GmailApp.search('in:inbox newer_than:4d ' + searchForLabels[l]);
    var msgs = GmailApp.getMessagesForThreads(threads);
    Logger.log(searchForLabels)
    //if (searchForLabels == 'has:nouserlabels') {
      for (var i = 0; i < msgs.length; i++) {
        for (var j = 0; j < msgs[i].length; j++) {
          var message = msgs[i][j];
          var from = message.getFrom();
          var subject = message.getSubject();
          var getAttachments = message.getAttachments();
          var body = message.getPlainBody();
          var getTo = message.getTo();
          Logger.log(subject)
          for (var k = 0; k < getAttachments.length; k++) {
            var attachment = getAttachments[k];
            var content = attachment.getContentType();
            //rest of my code
          }
        }
      }
    //}
  }
}
函数searchLabels(){
//如果此处有多个条目,则脚本不起作用
var searchForLabels=[
“has:nouser标签”,
'标签:Test1'
];
对于(var l=0;l
我想补充一点,您可以使用on-Apps脚本以简单的方式完成您的请求。特别是,您可以使用列出所有带有所选标记的邮件。在那里,您可以看到一个(单击
JavaScript
)函数,其中一个函数已准备好进行搜索。要启用此API,您应遵循以下指南。请随时询问更多说明或任何问题。

函数搜索标签(){
function searchLabels() {
  var searchForLabels = [
    'has:nouserlabels',
    'label:Test1'
  ];
  for (var l = 0; l < searchForLabels.length; l++) {
    var threads = GmailApp.search('in:inbox newer_than:4d ' + searchForLabels[l]);
    var msgs = GmailApp.getMessagesForThreads(threads);
    Logger.log(searchForLabels)
    if (searchForLabels[l] == 'has:nouserlabels') { //limits emails to those without tag
    //if (searchForLabels[l] == 'label:Test1') { //or to those with tag
      for (var i = 0; i < msgs.length; i++) {
        for (var j = 0; j < msgs[i].length; j++) {
          var message = msgs[i][j];
          var from = message.getFrom();
          var subject = message.getSubject();
          var getAttachments = message.getAttachments();
          var body = message.getPlainBody();
          var getTo = message.getTo();
          Logger.log(subject)
          for (var k = 0; k < getAttachments.length; k++) {
            var attachment = getAttachments[k];
            var content = attachment.getContentType();
            //rest of my code
          }
        }
      }
    }
  }
}
var searchForLabels=[ “has:nouser标签”, '标签:Test1' ]; 对于(var l=0;l
Jacques Guzel Heron-谢谢你的回答。根据你的建议,我已经更正了我的代码。你能添加一个解释吗?哪部分需要解释?通常,代码允许你搜索没有我任何标签的电子邮件,然后从邮件中获取附件;getAttachment是从邮件中获取的