Last Modified Date and User for Google Drive Download

1,083 views
Skip to first unread message

Dave White

unread,
Dec 13, 2022, 5:05:33 PM12/13/22
to Google Apps Script Community
Hi All--

Newbie here...great that I found this group. Im brushing up on my very rusty javascript/Google apps scripting...hoping to be at least pointed in the right direction.

I have a script tasked to download a big (shared) Google Drive directory, having it imported into Google Sheets. I can get everything except for the last modified file **user**. metadata:

G-Drive lists foldersfiles by name, owner, size and last modified date. There's additional metadata listed however: 1) modified 2) modified by me 3) viewed by me. I've been through the get parameters in the documentation and cant find a call to grab only the last modified (user). 

Any help is here definitely appreciated.
-Dave

Here's some reference code:

// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
  var childFolders = parent.getFolders();
  // List folders inside the folder
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    var folderId = childFolder.getId();
    data = [
      parentName + "/" + childFolder.getName(),
      childFolder.getName(),
      "Folder",
      childFolder.getDateCreated(),
      childFolder.getUrl(),
      childFolder.getLastUpdated(),
      childFolder.getDescription(),
      childFolder.getSize()/1024,
      childFolder.getOwner().getEmail()
    ];
    // Write
    sheet.appendRow(data);
    // List files inside the folder
    var files = childFolder.getFiles();
    while (listAll & files.hasNext()) {
      var childFile = files.next();
      data = [
        parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
        childFile.getName(),
        "Files",
        childFile.getDateCreated(),
        childFile.getUrl(),
        childFile.getLastUpdated(),
        childFile.getDescription(),
        childFile.getSize()/1024,
        childFile.getOwner().getEmail(),
      ];

cwl...@gmail.com

unread,
Dec 26, 2022, 12:28:13 PM12/26/22
to Google Apps Script Community
You will probably have to use the Drive API (advanced usage). You can create a simple function whose sole purpose is to retrieve this info. Just pass it the fileId, and it returns the info:

//for Drive API v2
function getLastModifyingUser(fileId) {
  return Drive.Files.get(fileId).lastModifyingUser.emailAddress  
 }

First add the Drive Api v2 service.
I would then modify your code by adding async in front of the word function, adding the above "lookup function", and adding:

async function getChildFolders(parentName, parent, data, sheet, listAll) {
.....
.....
   while (listAll & files.hasNext()) {
      var childFile = files.next();
      var lastModifyingUser = await getLastModifyingUser(childFile.getId()) //this makes the code stop and wait for the response
      data = [
        parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
        childFile.getName(),
        "Files",
        childFile.getDateCreated(),
        childFile.getUrl(),
        childFile.getLastUpdated(),
        lastModifyingUser,
        childFile.getDescription(),
        childFile.getSize()/1024,
        childFile.getOwner().getEmail(),
      ];
Reply all
Reply to author
Forward
0 new messages