Ahmed Mohamed
Ahmed Mohamed

Reputation: 161

Get Folder Name in google apps script

I am currently creating a script file bounded to a spreadsheet, where this spreadsheet is stored in a folder. Now, I need the name of the folder in which this spreadsheet exists. So how do I achieve this?

Upvotes: 1

Views: 1526

Answers (1)

Marios
Marios

Reputation: 27390

Solution:

The following script will get the name of the parent folder of the bound spreadsheet and set it to cell A1. Adjust the script to your needs.

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  const ss_id = ss.getId();
  const file = DriveApp.getFileById(ss_id);
  const parent_folders = file.getParents()
  const folder_name = parent_folders.next().getName(); //desired result
  sh.getRange('A1').setValue(folder_name); 
}

folder_name is the name of the parent folder of the existing spreadsheet.

Upvotes: 4

Related Questions