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?
MyDrive
, Client
and tpl
are folders.intake.doc
is a file.Client
and tpl
under MyDrive
using the parent folder of intake.doc
.tpl
is retrieved.If my understanding is correct, how about this answer? In this answer, your 2nd script was modified.
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.
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.
intake.doc
.
tpl
is retrieved.tpl
.
MyDrive
is retrieved.MyDrive
.
Client
and tpl
are retrieved.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
}
}
}
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.If I misunderstood your question and this was not the direction you want, I apologize.