找到XML文件并将其发送到驱动器

时间:2019-07-12 15:33:24

标签: google-apps-script google-drive-api

我正在研究如何找到xml文件并将其发送到云端硬盘。我找到了一些代码,但是我不太了解。有人可以帮忙解释一下这段代码在做什么吗?

Google App脚本文档

//GLOBALS
//Array of file extension which you would like to extract to Drive

var fileTypesToExtract = ['xml', 'zip'];

//Name of the folder in google drive i which files will be put

var folderName = 'Archivador';

//Name of the label which will be applied after processing the mail message

var labelName = 'Realizados';

function nLabels(){
var firstThread = GmailApp.getInboxThreads(0,2)[0];
var labels = firstThread.getLabels();
for (var i = 0; i < labels.length; i++) {
  Logger.log(labels[i].getName());
}
  }

function GmailToDrive(){
  //build query to search emails
  var query = '';
  //filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
  for(var i in fileTypesToExtract){
    query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
  }
  query = 'in:inbox has:nouserlabels ' + query;
  //
  var threads = GmailApp.search(query);
  var label = getGmailLabel_(labelName);
  var parentFolder;
  if(threads.length > 0){
    parentFolder = getFolder_(folderName);
  }
  var root = DriveApp.getRootFolder();
  var etiqueta = 0;
  var carpeta = "";
  var etiquetaInbox = GmailApp.getUserLabelByName('inbox');
  Logger.log("EI:" + etiquetaInbox);

  for(var i in threads){
    var mesgs = threads[i].getMessages();
    for(var j in mesgs){
      //get attachments
      var date = formatDate(mesgs[j].getDate());
      Logger.log("Date:"+mesgs[j].getDate());
      Logger.log("Date:"+date);
      var attachments = mesgs[j].getAttachments();
      for(var k in attachments){

       var cFactura
        var attachment = attachments[k];
        var isDefinedType = checkIfDefinedType_(attachment);
        if(!isDefinedType) continue;
        var val = attachment.getName();
        if (etiqueta == 0){
          Logger.log("PASO VALIDACION CARPETA");
          var anFolder = attachment.getName();
          var n = anFolder.length;
          carpeta = "["+date+"] " + anFolder.substring(0, n-4);

          //var attachmentBlob = attachment.copyBlob();
          //var file = DriveApp.createFile(attachmentBlob);

          cFactura = folderFactura(carpeta); 
          //parentFolder.createFolder(carpeta);
        }
        etiqueta = 1;

        /*
        var aFolder = getFolder_(anFolder);        
        aFolder.addFile(file);        
        root.removeFile(file);*/
      }
      //Fin del For
      if (etiqueta == 1){
        for(var k in attachments){

          var attachment = attachments[k];  
          var val = attachment.getName();         
          var attachmentBlob = attachment.copyBlob();
          var file = DriveApp.createFile(attachmentBlob);

          //var aFolder = getFolder_(carpeta);        
          cFactura.addFile(file);        
          root.removeFile(file);
      }
        threads[i].addLabel(label);
        threads[i].moveToArchive();

      } else {threads[i].addLabel(getGmailLabel_("Pendientes"));}

      etiqueta = 0;
      carpeta = "";
    }
  }
}

//This function will get the parent folder in Google drive
function getFolder_(folderName){
  var folder;
  var fi = DriveApp.getFoldersByName(folderName);
  if(fi.hasNext()){
    folder = fi.next();
  }
  else{
    folder = DriveApp.createFolder(folderName);
  }
  return folder;
}

function folderID(){
  var folder
  var fi = DriveApp.getFoldersByName("Archivador");
  //var pFolder = DriveApp.getFolderById(id);
  if(fi.hasNext()){
    folder = fi.next();
    Logger.log(folder.getId());

  }
  else{
  }
}

function formatDate(date){

  var fecha = date.getFullYear()+""+formatNumber(date.getMonth()+1)+""+formatNumber(date.getDate())+""+formatNumber(date.getHours())+""+formatNumber(date.getMinutes())+""+formatNumber(date.getSeconds());
  return fecha;
}

function formatNumber(number){
  var numero
  if(number<10){
    numero = "0"+ number;
    }
  else {
    numero=number;
    }
  return numero;
}

function folderFactura(folderName){
  var folder;
  var fi = DriveApp.getFoldersByName(folderName);
  var pFolder = DriveApp.getFolderById("1mD_lqYpfyj6dmV9KbE8heafffsmkcuhMP");
  if(fi.hasNext()){
    folder = fi.next();
  }
  else{
    folder = pFolder.createFolder(folderName);
  }
  return folder;
}

//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
  n = parseInt(n);
  var date = new Date();
  date.setDate(date.getDate() - n);
  return Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy/MM/dd');
}

function getGmailLabel_(name){
  var label = GmailApp.getUserLabelByName(name);
  if(!label){
    label = GmailApp.createLabel(name);
  }
  return label;
}

//this function will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
  var fileName = attachment.getName();
  var temp = fileName.split('.');
  var fileExtension = temp[temp.length-1].toLowerCase();
  if(fileTypesToExtract.indexOf(fileExtension) !== -1) return true;
  else return false;
}

Al ejecutarlo,我错了ID

0 个答案:

没有答案