Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google apps spreadsheet parents folder ID

I have a google spreadsheet that is keeping track of the files and their names that are inside the folder where the spreadsheet is.
I have looked and can not find how to get the location or ID of that sheet
In MS CMD script it would be "%~dp0".
How to do this in google apps spreadsheet script.

 function FnctnMenuUpdateGetLocation() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
  var SSID=ss.get.getId();
  var file = DocsList.getFileById(SSID);

  Browser.msgBox(SSID);
  Browser.msgBox(add);
}

https://docs.google.com/spreadsheet/ccc?key=0AmsQN3N9km70dGwtLTJXVVBzbmJhdmE5OFpkbTVxelE&usp=sharing ya, I am working in it
this is a live sheet

From what I have how do you get the address of the spreadsheet in google drive
from the spread sheet I will be having it add folder/dir and file that I need

since google docs is not google drive how is the interface from Spreadsheet to the google drive

like image 773
kc0hwa Avatar asked Jul 10 '13 22:07

kc0hwa


2 Answers

This works as of June 2018

This answer also assumes that you are only interested in the first folder in the list of parents.

var spreadsheetId =  SpreadsheetApp.getActiveSpreadsheet().getId();
var spreadsheetFile =  DriveApp.getFileById(spreadsheetId);
var folderId = spreadsheetFile.getParents().next().getId();
like image 131
Walter Stabosz Avatar answered Oct 29 '22 13:10

Walter Stabosz


Both the Document Service and Drive Service can report which folder(s) a spreadsheet is in.

Here's your function, updated to show the name of a folder that contains the spreadsheet, using both services.

enter image description here

function FnctnMenuUpdateGetLocation() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var SSID=ss.getId();

   var fileInDocs = DocsList.getFileById(SSID);
   var folderInDocs = fileInDocs.getParents()[0].getName();

   var fileInDrive = DriveApp.getFolderById(SSID);
   var folderinDrive = fileInDrive.getParents().next().getName();

   Browser.msgBox("Docs says: " + folderInDocs +
                  ", Drive says: " + folderinDrive);
}

Document Service's .getParents() method returns an array of folder objects, while Drive's returns an iterator, so the details of how to retrieve a specific parent folder(s) once you've got all of them differs.

Note that a spreadsheet may be "contained" in multiple folders... in this case, we've assumed that the first folder in the list is the only one - that assumption may be invalid.

like image 27
Mogsdad Avatar answered Oct 29 '22 14:10

Mogsdad