我的Google App脚本中有以下几行,但现在不推荐使用File 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);`
我需要我的脚本转到我的驱动器上的文件夹,在此文件夹中获取最新的Google电子表格文件并将其打开。
答案 0 :(得分:2)
以下是获取文件夹中最新文件的代码:
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()) {
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());
}
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());
};
};
答案 1 :(得分:1)
不用担心,您可以使用DriveApp
代替。有关可在驱动器文件上使用的所有可能方法,请参阅此页:https://developers.google.com/apps-script/reference/drive/drive-app
希望有所帮助!