Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the folder ID of the current Google Apps spreadsheet

In a script bound to a Google Sheet, how can I learn the ID (not the name) of the folder containing the current sheet? I want to do this:

var files = DriveApp.getFolderById('0B9momRZYw4DVTlJVeXNpX25BemM').getFiles();

but using a variable that represents the folder ID, like this:

var files = DriveApp.getFolderById(folderId).getFiles();

The problem is I cannot figure out how to obtain the parent folder's ID. I realize the current sheet may reside in several different folders (mine doesn't), but surely there is some way to get the ID of the folder?

like image 324
murph Avatar asked Mar 09 '23 21:03

murph


1 Answers

This is quite straightforward using DriveApp getParents() method. Code goes like this :

function getParentFolder(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var file = DriveApp.getFileById(ss.getId());
  var folders = file.getParents();
  while (folders.hasNext()){
    Logger.log('folder name = '+folders.next().getName());
  }
}

Use folders.next().getId() to get the folder(s) ID(s).

enter image description here

like image 189
Serge insas Avatar answered Apr 19 '23 23:04

Serge insas