Google App Script - DriveApp - getParents() not working as expected

Viewed 2090
MyDrive
|_ Client
|_ tpl
   |_ intake.doc

I'm trying to get a reference to the MyDrive folder.

  var tpl_intakeDoc = DriveApp.getFileById(docIDs.tpl_intake);
  var parent = tpl_intakeDoc.getParents()[0];
  Logger.log(parent.getName());

This gives me the following error:

TypeError: Cannot call method "getName" of undefined. (line 50, file "Code")

Similarly, trying to iterate through the parent folders (Client and tpl) only shows tpl for some reason.

  var parents = tpl_intakeDoc.getParents();
  while (parents.hasNext()) {
    var parents = parents.next();
    Logger.log(folder.getName());
  }

All that is logged is:

[19-06-14 04:14:11:764 MDT] tpl

where I would expect to see something like:

[19-06-14 04:14:11:764 MDT] tpl

[19-06-14 04:14:11:764 MDT] Client

What am I doing wrong?

2 Answers
  • MyDrive, Client and tpl are folders.
  • intake.doc is a file.
  • You want to retrieve the folders of Client and tpl under MyDrive using the parent folder of intake.doc.
  • You want to know about the reason that when your 2nd script is run, only tpl is retrieved.

If my understanding is correct, how about this answer? In this answer, your 2nd script was modified.

Modification points:

  • In your 2nd script in your question, I think that an error occurs when the script is run. So please modify as follows.

    var tpl_intakeDoc = DriveApp.getFileById(docIDs.tpl_intake); // Added
    var parents = tpl_intakeDoc.getParents();
    while (parents.hasNext()) {
      var parent = parents.next(); // Modified
      Logger.log(parent.getName()); // Modified
    }
    
  • In above script, the parent folder of intake.doc is retrieved. So only tpl is returned.

  • In order to retrieve the folders of Client and tpl, the parent folder of tpl is required to be retrieved. And retrieve the folders under the parent folder of tpl.

The flow of this script is as follows.

  1. Retrieve the parent folder of intake.doc.
    • tpl is retrieved.
  2. Retrieve the parent folder of tpl.
    • MyDrive is retrieved.
  3. Retrieve the folders under MyDrive.
    • Client and tpl are retrieved.

Modified script:

var tpl_intakeDoc = DriveApp.getFileById(docIDs.tpl_intake);
var parent = tpl_intakeDoc.getParents(); // tpl
if (parent.hasNext()) {
  var parentOnParent = parent.next().getParents(); // MyDrive
  if (parentOnParent.hasNext()) {
    var MyDrive = parentOnParent.next().getFolders();
    while (MyDrive.hasNext()) {
      var folder = MyDrive.next();
      Logger.log(folder.getName()) // tpl, Client
    }
  }
}

Note:

  • If MyDrive is the root folder, and if you want to retrieve the folders of tpl and Client from the root folder, I think that Alberto Molina's answer is useful for this.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

To get a reference to your “MyDrive” folder, the root of your drive, you would use the function

var rootFolder = DriveApp.getRootFolder();

which will return a folder object as if you searched a folder by ID. Below is further documentation on the DriveApp class, which will help you with any other doubt.

Documentation URL: https://developers.google.com/apps-script/reference/drive/drive-app#getrootfolder

Related