为什么Object FolderIterator找不到函数getId?

时间:2015-11-07 01:20:03

标签: google-apps-script gmail

我尝试在下面运行代码。代码将gmail附件移动到定义的文件夹。 它返回错误"对象FolderIterator找不到函数getId"。 代码在var folderID = folder.getId()处停止 文件夹由DriveApp定义 为什么Object FolderIterator找不到函数getId?

  function sendtoDrive() {

  var ss1 = SpreadsheetApp.openById(SpreadSheetID);
  var ash = ss1.getSheets()[0];
  var dr = ash.getDataRange();
  var lr = dr.getLastRow();
  var dvs = dr.getValues();
  var n=0;
 var properties = PropertiesService.getScriptProperties();

  for(n=1;n<lr;n++ ){
    try {
      var sdr = dvs[n][0];
      try {
        var folder = DriveApp.getFoldersByName(sdr);
      }
      catch (e) {
        try {
          var folder = DriveApp.createFolder(sdr);
        }
        catch (e) {
          Logger.log(e.message);
          throw e;
        }        
      } 
      var mailsAlreadyProcessed = properties.getProperty(sdr);
      if(mailsAlreadyProcessed == undefined){
         var folderID=folder.getId()
         properties.setProperty(sdr, folderID + ',');
        var mailsAlreadyProcessed = properties.getProperty(sdr);
      }

      var label = GmailApp.getUserLabelByName(sdr);
      var labelDone = GmailApp.getUserLabelByName('Done');
      var threads = label.getThreads();

      if (threads.length > 0) {
        var folderId = mailsAlreadyProcessed.split(',')[0];
        var folder = DriveApp.getFolderById(folderId);

        for (i in threads) {
          if (threads[i].getLabels() != 'Done') {
            var messages = threads[i].getMessages();
            for (j in messages) {
              var attachments = messages[j].getAttachments();
              if (attachments.length > 0 && mailsAlreadyProcessed.indexOf(messages[j].getId()) == -1) {
                mailsAlreadyProcessed += messages[j].getId();

                for (k in attachments) {
                  var file = attachments[k];
                  folder.createFile(file);
                  Utilities.sleep(100); 
                }

                ScriptProperties.setProperty('mailsAlreadyProcessed', mailsAlreadyProcessed);
              }
            }
            labelDone.addToThread(threads[i]);
            label.removeFromThread(threads[i]);
            Utilities.sleep(100); 
          }
        }
      }
    }      
    catch (e) {
      Logger.log(e.message);
      throw e;
    }
  }
}

1 个答案:

答案 0 :(得分:2)

DriveApp.getFoldersByName(sdr)会返回文件夹迭代器,因为在Google云端硬盘中,多个文件夹可以使用相同的名称。 如果您确定只存在一个具有该名称的文件夹,那么您可以在代码后面找到.next()的文件夹对象。

folder = DriveApp.getFoldersByName(sdr).next();