gpt4 book ai didi

javascript - 获取 Google 云端硬盘文件夹中的最新文件

转载 作者:行者123 更新时间:2023-11-29 19:30:08 26 4
gpt4 key购买 nike

我的 Google App 脚本中有以下几行,但文件 API 现已弃用,我无法更新代码。

原文是:

var folder = DocsList.getFolder('Folder Name');
var file = folder.getFilesByType(DocsList.FileType.SPREADSHEET)[0];
var filename = file.getName(); //and get its name
var fileId = file.getId(); // its ID
var fileurl = file.getUrl(); //and its url
var source = SpreadsheetApp.openById(fileId);`

我需要我的脚本转到我的驱动器上的一个文件夹,在这个文件夹中获取最新的谷歌电子表格文件并打开它。

最佳答案

这是获取文件夹中最新文件的代码:

function getNewestFileInFolder() {
var arryFileDates,file,fileDate,files,folder,folders,
newestDate,newestFileID,objFilesByDate;

folders = DriveApp.getFoldersByName('yourFolderName');
arryFileDates = [];
objFilesByDate = {};

while (folders.hasNext()) {
folder = folders.next();

files = folder.getFilesByType("application/vnd.google-apps.spreadsheet");
fileDate = "";

while (files.hasNext()) {//If no files are found then this won't loop
file = files.next();
Logger.log('xxxx: file data: ' + file.getLastUpdated());
Logger.log('xxxx: file name: ' + file.getName());
Logger.log('xxxx: mime type: ' + file.getMimeType())
Logger.log(" ");

fileDate = file.getLastUpdated();
objFilesByDate[fileDate] = file.getId(); //Create an object of file names by file ID

arryFileDates.push(file.getLastUpdated());
}

if (arryFileDates.length === 0) {//The length is zero so there is nothing
//to do
return;
}

arryFileDates.sort(function(a,b){return b-a});

Logger.log(arryFileDates);

newestDate = arryFileDates[0];
Logger.log('Newest date is: ' + newestDate);

newestFileID = objFilesByDate[newestDate];

Logger.log('newestFile: ' + newestFileID);
//return newestFile;

var ss = SpreadsheetApp.openById(newestFileID);
Logger.log('file name that is now open: ' + ss.getName());
};
};

关于javascript - 获取 Google 云端硬盘文件夹中的最新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28323703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com