如何识别Google云端硬盘文件夹中的最新文件 [英] How to identify the most recent file in a Google Drive folder

查看:112
本文介绍了如何识别Google云端硬盘文件夹中的最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google驱动器中有一个文件夹,其中的文件通常会从报告工具中转储。这些文件采用.xls格式。它们都有相同的名称,我想识别最近的文件,以便我可以将数据导入到Google工作表。



正如您将能够分辨从下面我是新的Google Apps脚本。我一直在试图将文件迭代器中的各种文件捕获到一个数组中,以便对结果运行Math.max函数,最终只得到一个值。当然,一旦我确定了最新的日期,我也需要返回与之相关的文件ID - 但我还没有那么远。

 函数listLatestFile(id){
var folder = DriveApp.getFoldersByName(GmailAttachments);
var files = DriveApp.getFilesByName('mrp_out.xlsx');
while(files.hasNext()){
var file = files.next();
var date =(1 - file.getDateCreated());
var datediff = new Array(date);
var datelast = Math.max(datediff);
Logger.log(datelast);
}
//Logger.log(日期);
};


解决方案

您离解决方案并不遥远......我尝试使用稍微不同的方法:我将所有日期和ID存储在二维数组中,我对数组进行排序,并在第二维中检索第一个元素(排序后)以返回文件ID。



代码:

pre $ function listLatestFile(){
var folder = DriveApp.getFoldersByName( GmailAttachments);
var files = DriveApp.getFilesByName('mrp_out.xlsx');
var result = [];
while(files.hasNext()){
var file = files.next();
result.push([file.getDateCreated(),file.getId()]);
}
Logger.log(result);
var id = result.sort()[0] [1];
Logger.log(id);
return id; //返回最近的文件ID
};

编辑:如果您想对排序方法有更多的控制权,您可以使用更复杂的排序方法,如下所示:

  ... //同上
结果.sort(function(x,y){
var xp = x [0]; //获取内部数组中的第一个元素
var yp = y [0];
return xp == yp?0:xp< yp?-1:1; //选择排序顺序
});
var id = result [0] [1];
Logger.log(id);
return id;
};


I have a folder in Google drive in to which files are regularly dumped from a reporting tool. The files are in .xls format. They all have the same name and I want to identify the most recent file so that I can import the data in to a Google sheet.

As you will be able to tell from the below I am new to Google Apps script. I have been trying to catch the various files from the file iterator into an array to run a Math.max function on the result to end up with just one value. Of course, once I have identified the newest date I also need to return the fileID associated with - but I haven't got that far yet. I am probably heading in the completely wrong direction so any tips/guidance would be greatly appreciated.

function listLatestFile(id) {
var folder = DriveApp.getFoldersByName("GmailAttachments");
var files = DriveApp.getFilesByName('mrp_out.xlsx');
while (files.hasNext()) {
 var file = files.next();
 var date = (1 - file.getDateCreated());
 var datediff = new Array(date);
 var datelast = Math.max(datediff);
 Logger.log(datelast);
 } 
//Logger.log(date);
};

解决方案

You were not very far from the solution... I tried using a slightly different approach : I store all dates and IDs in an 2D array, I sort the array and retrieve the first element (after sorting) in the 2cond dimension to return the file ID.

code :

function listLatestFile() {
  var folder = DriveApp.getFoldersByName("GmailAttachments");
  var files = DriveApp.getFilesByName('mrp_out.xlsx');
  var result = [];
  while (files.hasNext()) {
    var file = files.next();
    result.push([file.getDateCreated(),file.getId()]);
 } 
  Logger.log(result);
  var id = result.sort()[0][1];
  Logger.log(id);
  return id;// return most recent file ID
};

EDIT : if you want to have more control on the sorting method, you can use a more "sophisticated" sort method like below :

   ...// same as above
  result.sort(function(x,y){
    var xp = x[0];// get first element in inner array
    var yp = y[0];
    return xp == yp ? 0 : xp < yp ? -1 : 1;// choose the sort order
  });    
  var id = result[0][1];
  Logger.log(id);
  return id;
};

这篇关于如何识别Google云端硬盘文件夹中的最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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