谷歌驱动器上,做文件夹中存在始终返回false [英] on google drive, does folder exist always returns false

查看:184
本文介绍了谷歌驱动器上,做文件夹中存在始终返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌应用程序脚本来创建谷歌驱动器上的文件夹。我不想在驱动器上出现任何重复,所以我创建了一个功能,以找出是否该文件夹存在。

我把所有的文件夹名称到一个数组,然后看如果文件夹名称数组中存在。它始终返回false,但...

你能看到我已经错在这里?或者是有发现如果在驱动器上存在或不是一个文件夹的较好的方法??

 函数doesContainerExist(容器){
  变种夹= DriveApp.getFolders();
  VAR文件夹=新的阵列;
  VAR布尔;  而(folders.hasNext()){
    Folders.push(folders.next());
  }  如果(Folders.indexOf(集装箱)== -1){
    布尔= FALSE;
  }其他{
    布尔= TRUE;
  }
  Logger.log(容器);  返回boolean变量;};


解决方案

有是检查,如果一个文件夹已经存在另一种有效的方式,这里是code与testfunction你可以试试:

 函数TESTTEST(){
  Logger.log(testFolder('照片'));如果文件夹存在,如果它不//返回true,假的
}功能testFolder(文件夹名){
  VAR存在= TRUE;
  尝试{VAR testFolder = DocsList.getFolder(文件夹名)}
  赶上(错误){存在= FALSE}
  返回存在;
}

由于您的最终目的是创建一个文件夹,你可以直接在脚本中的捕捉部分中创建,或干脆在现有$ C $整合的try / catch c您用来创建该文件夹。

I am using google app scripts to create a folder on the google drive. I don't want any duplication on the drive so I created a function to find out if the folder exists.

I am putting all the folder names into an array and then seeing if the folder name exists in that array. It always returns false though...

Can you see where I've gone wrong here? or is there a better method of finding out if a folder exists on the drive or not??

function doesContainerExist(container) {
  var folders = DriveApp.getFolders();
  var Folders = new Array;
  var boolean;

  while(folders.hasNext()) {
    Folders.push(folders.next());
  }

  if(Folders.indexOf(container) == -1) {
    boolean = false;
  } else {
    boolean = true;
  }
  Logger.log(container);

  return boolean;

};

解决方案

There is another efficient way to check if a folder already exist, here is the code with a testfunction for you to try :

function testTest(){
  Logger.log(testFolder('Photos'));// returns true if folder exists, false if it doesn't
}

function testFolder(folderName){
  var exist = true;
  try{var testFolder = DocsList.getFolder(folderName)}
  catch(err){exist=false}
  return exist;
}

Since your final purpose is to create a folder you can create it directly inside the 'catch' part of the script or simply integrate the try/catch in your existing code you use to create the folder.

这篇关于谷歌驱动器上,做文件夹中存在始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆