在文件夹google apps脚本之间移动文件

时间:2018-01-27 20:22:43

标签: google-apps-script

我正在尝试将pdf中的文件列表从一个文件夹树移动到另一个文件夹树。两个文件夹都按照学生姓名命名的文件夹排序。 scritp工作得很好。它从学生“A”文件夹中获取文件,并将其粘贴到名为“A”的右侧文件夹中。但它从下一个学生文件夹“B”获取文件,并将其粘贴到学生“A”文件夹中。怎么了?几个小时我一直在寻找错误...... 非常感谢您的帮助。

function moviendo() {

var fecha = new Date();
var ano = fecha.getYear();
var mes = fecha.getMonth();

if (mes >= 0 && mes <= 8){var cursoac = ano-2+"-"+ ano-1}
if (mes >= 9 && mes <= 11){var cursoac = ano+"-"+ ano+1}

var cursoac = "2016-2017"

//var origen = DriveApp.getFolderById("0Bzgw8SlR34pUNDNlV2dOS0JmU2M");
var origen = DriveApp.getFolderById("1TeBhHwhwcGha_KK5xxlkE8J7mx07Nrb9");
var destino = DriveApp.getFolderById("1aZGTr8kihc2LjTF6oOfUiOZAAdhy45DJ");

var folders = origen.getFolders();
while (folders.hasNext()){
var folder = folders.next();
var foldername = folder.getName();

var foldest = destino.getFolders();
while (foldest.hasNext()){
var foldes = foldest.next();
var foldesname = foldes.getName();

Logger.log("Destino: "+foldesname);
Logger.log("Origen: "+foldername);

if (foldesname == foldername){

//INTENTAR CREAR "DISCIPLINA"

try {

    Logger.log("TRY: "+foldes.getName());
    var fol = foldes.getFolders();
    while (fol.hasNext()) {
     var foldis = fol.next();
     var foldisname = foldis.getName();
     if(foldisname == "Disciplina"){
     var folderf = foldis.getId();
     }}

     var folderfin = DriveApp.getFolderById(folderf);
     }

    catch(f) {
    var folderf = foldes.createFolder("Disciplina").getId();
    var folderfin = DriveApp.getFolderById(folderf)
    }

Logger.log("Folderfin: "+folderfin);

//INTENTA CREAR EL CURSO ACTUAL DE ARCHIVO

try {

    var fol2 = folderfin.getFolders();
    while (fol2.hasNext()) {
     var foldis2 = fol2.next();
     var foldisname2 = foldis2.getName();
     if(foldisname2 == cursoac){
     var folderf2 = foldis2.getId();
     }}

     var folderfin2 = DriveApp.getFolderById(folderf2);
     }

    catch(e) {
    var folderf2 = folderfin.createFolder(cursoac).getId();
    var folderfin2 = DriveApp.getFolderById(folderf2)
    }

Logger.log("Folderfin2: "+folderfin2);

var files = folder.getFiles();
while (files.hasNext()){
var file = files.next();
folderfin2.addFile(file);
folder.removeFile(file);
}

}}}

}

1 个答案:

答案 0 :(得分:1)

使用folder.getFoldersByName()方法可以简化代码。下面是一个如何使用文件夹名称&#34; JACK&#34;来访问/创建文件夹的示例。并分配给变量destFold

  var folder = DriveApp.getFolderById("ID")
  var folders = folder.getFoldersByName("Jack")
  var destFold
  if(folders.hasNext())
     destFold = folders.next()
    else {
     destFold = folder.createFolder("Jack")
    }

将此算法应用于您的代码会产生以下结果(function getFolder(name,folder)):

function moviendo() {
var fecha = new Date();
var ano = fecha.getYear();
var mes = fecha.getMonth();

if (mes >= 0 && mes <= 8){var cursoac = ano-2+"-"+ ano-1}
if (mes >= 9 && mes <= 11){var cursoac = ano+"-"+ ano+1}

var cursoac = "2016-2017"

//var origen = DriveApp.getFolderById("0Bzgw8SlR34pUNDNlV2dOS0JmU2M");
var origen = DriveApp.getFolderById("1icoXnLKpVDkR9_i8eurP-CX4PMIMEASD") //1TeBhHwhwcGha_KK5xxlkE8J7mx07Nrb9");
var destino = DriveApp.getFolderById("1rnY1KhP14qXSMjFlvJZ0elbmy_a3TrqC")//"1aZGTr8kihc2LjTF6oOfUiOZAAdhy45DJ");

var folders = origen.getFolders();
while (folders.hasNext()){
var folder = folders.next();
var foldername = folder.getName();
  Logger.log("Origin: "+foldername)
  var foldes = getFolder(foldername,destino)
//INTENTAR CREAR "DISCIPLINA"   
  var folderfin = getFolder("Disciplina",foldes)
  var folderfin2 = getFolder(cursoac, folderfin)
  var files = folder.getFiles();
   while (files.hasNext()){
     var file = files.next();
     folderfin2.addFile(file);
     folder.removeFile(file);
   }
}
}

function getFolder(name,folder){
  var folderfin = folder.getFoldersByName(name)
    if (folderfin.hasNext()){
       var folderfin = folderfin.next();
     } else {
       var folderfin = folder.createFolder(name)
     }
  Logger.log("Return Folder:"+folderfin)
  return folderfin
}

您的代码似乎没有明显的问题。我简化了它以调试它。希望有所帮助。