我如何区分使用GAS的DriveApp中的文件和文件夹 [英] How can I discriminate between file and folder in DriveApp using GAS

查看:150
本文介绍了我如何区分使用GAS的DriveApp中的文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个文件夹实际上是一个在Google云端硬盘中具有特殊mimeType application / vnd.google-apps.folder 的文件。
但是有关DriveApp Folder类的文档不包含像DriveApp File类那样的getMimeType()方法。

I know a folder actually is a file with a special mimeType application/vnd.google-apps.folder in Google Drive. But documentation about DriveApp Folder class doesn't contain a method getMimeType() like DriveApp File class does.

如何在函数中找到参数这个函数是指一个文件夹还是一个(普通)文件?

How can find out in a function if an argument of that function refers to a folder or to a (normal) file?

function displayResults(fileOrFolder)
{
  var id   = fileOrFolder.getId();
  var name =  fileOrFolder.getName();

  var mimeType = fileOrFolder.getMimeType();  // works if file; error if folder

  if (fileOrFolder == <Class File>)
  {
     <do something>
  }
  else if (fileOrFolder == <Class Folder>)
  {
     <do something else>
  }
}


推荐答案

如果方法存在。如果obj.getMimeType ===未定义其文件夹。


或者,为了在api更改(将方法添加到文件夹)时使其更加健壮,请使用

if(obj.getMimeType === undefined || obj.getMimeType.toLowerCase()==application / vnd.google-apps.folder)

Look if the method exists. If obj.getMimeType===undefined its a folder.
or, to make it more robust in case the api changes (adds the method to folders), use
if (obj.getMimeType===undefined || obj.getMimeType.toLowerCase() =="application/vnd.google-apps.folder")

这篇关于我如何区分使用GAS的DriveApp中的文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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