google-apps-script计算文件夹中的文件 [英] google-apps-script count files in folder

查看:361
本文介绍了google-apps-script计算文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索,但似乎找不到我要找的。

我正在寻找的代码,让我做这个伪代码:


  1. 计算特定Google云端硬盘文件夹中的文件数(按文件夹ID)

  2. 如果文件计数高于'x'do this {other piece of code}

  3. else,退出

帮助我?

解决方案

我找不到任何方法来计算文件数,所以我想你需要遍历每个文件,并有一个计数器:

  function countFilesInFolder(){
var count,文件,文件,theFolder; //定义变量而不赋值

theFolder = DriveApp.getFolderById('your file ID');
files = theFolder.getFiles();

count = 0;

while(files.hasNext()){
count ++;
file = files.next();
//Logger.log(file.getName());
if(count> 5){
fncNextFunction();
break;
};
};

Logger.log(count);
};

function fncNextFunction(){
Logger.log('fncNextFunction');

};

注意:如果我只是计数器递增而没有得到下一个文件,脚本编辑器冻结。 / p>

以下是使用高级云端硬盘服务的替代方法:

  /这需要Drive API在高级Google服务中打开
function countFilesInFolder(){

var query ='trashed = false and'+
'您的文件夹ID在这里在父母;

var filesInFolder = Drive.Files.list({q:query});

Logger.log('filesInFolder:'+ filesInFolder.items.length);

for(var i = 0; i< filesInFolder.items.length; i ++){
//Logger.log('key:'+ key);
var thisItem = filesInFolder.items [i];
Logger.log('value:'+ thisItem.title);
};
};

有关使用页面令牌列出超过100个结果的示例,请参阅文档


I've had a search but cannot seem to find what I am looking for.

I am seeking the code to enable me to do this pseudocode:

  1. Count number of files in specific Google Drive folder (by folder Id)
  2. If file count is above 'x' do this {other piece of code}
  3. else, exit

Can anyone help me out?

解决方案

I can't find any method to give the count for the number of files, so I guess you'll need to loop through every file, and have a counter:

function countFilesInFolder() {
  var count,file,files,theFolder;//Define variables without assigning a value

  theFolder = DriveApp.getFolderById('your file ID');
  files = theFolder.getFiles();

  count = 0;

  while (files.hasNext()) {
   count++;
   file = files.next();
   //Logger.log(file.getName());
   if (count > 5) {
     fncNextFunction();
     break;
   };
 };

  Logger.log(count);
};

function fncNextFunction() {
  Logger.log('fncNextFunction');

};

Note: If I just have the counter increment without getting the next file, the script editor freezes.

Here is an alternative method using the advanced Drive Service:

//This requires the Drive API To be turned on in the Advanced Google Services
function countFilesInFolder() {

 var query = 'trashed = false and ' +
        "'Your Folder ID here' in parents";

  var filesInFolder = Drive.Files.list({q: query});

  Logger.log('filesInFolder: ' + filesInFolder.items.length);

  for (var i=0;i<filesInFolder.items.length;i++) {
    //Logger.log('key: ' + key);
    var thisItem = filesInFolder.items[i];
    Logger.log('value: ' + thisItem.title);
  };
};

For an example of using page tokens to list more than 100 results, see The Documentation

这篇关于google-apps-script计算文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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