Home > Software engineering >  In Google Sheets, how do I get the folder name where the current Spreadsheet is located and write it
In Google Sheets, how do I get the folder name where the current Spreadsheet is located and write it

Time:09-03

I have a Spreadsheet in a folder in google Drive, let's call the folder "FolderX"

I opened AppScript in the Spreadsheet and wrote this function:

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); 
}

(I got that function from enter image description here

This is an expected behavior stated in the enter image description here

Another Reference: enter image description here I just tweaked the code a bit to set the value on the currently selected cell.

Using Button:

enter image description here

CodePudding user response:

function getFolder() {
  const ss = SpreadsheetApp.getActive();//Logger outputs the name of this spreadsheet folder
  const id = ss.getId();
  const file = DriveApp.getFileById(id);
  const f = file.getParents().next()
  Logger.log(f.getName())
}
  • Related