0
votes

Is it possible to get a current folder name when I make a new Doc file? My script shows for me all the folders recursively from the root, but not the current one:

In a Google Drive Api I can't find info about current folder :( Please help me.

// Log the name of every folder in the user's Drive.
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
//Logger.log(folder.getName());
DocumentApp.getUi().alert(folder.getName());
}
1
Your question is not clear, how are you selecting the current folder? When you create a new file using the scripts it's placed in the root of the drive unless you specify the destination folder. Your not using a recursive function here but a loop to iterate through all folders in your drive.James D
oh sorry - I can't create a new file using script, only via Button and mouse. One answer that below already helped me. Thank you.Oleksandr

1 Answers

0
votes

Yes. Try:

var id = DocumentApp.getActiveDocument().getId();//get id of the current document
DocumentApp.getUi().alert(DriveApp.getFileById(id) //getFile
  .getParents() //getParentFolders
  .next() //getFirstParentFolder
  .getName());//getNameOfThatFolder