Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active November 19, 2017 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/0f452b2f951dddb57363dbb816487c33 to your computer and use it in GitHub Desktop.
Save tanaikech/0f452b2f951dddb57363dbb816487c33 to your computer and use it in GitHub Desktop.
Retrieving latest created file from PDF files on Google Drive

Retrieving latest created file from PDF files on Google Drive

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment