How about this? When it retrieves the latest created file from PDF files, you can use drive.files.list
. The q
for "Search for Files" is "mimeType='application/pdf'"
. As the "orderBy", it uses "createdTime desc"
. By this, the first element of the response becomes the latest created file. Drive API is automatically enabled, and by UrlFetchApp
, it is not required to use Advanced Google Services.
var query = {
orderBy: "createdTime desc",
q: "mimeType='application/pdf'",
fields: "files(id)"
};
var querystr = Object.keys(query).map(function(e){return e + "=" + encodeURIComponent(query[e])}).join("&");
var res = JSON.parse(
UrlFetchApp.fetch(
"https://www.googleapis.com/drive/v3/files?" + querystr,
{headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}}
)
);
Logger.log(res.files[0].id) // The first element is the latest created file.