Wednesday, October 10, 2012

Google Spreadsheet Script to get the Parent Folder

Question:

I need to write a custom Spreadsheet function (apps script) that shows the name of the folder it is in. 
I tried getParents but that is not available to SpreadsheetApp.getActiveSheet().


Solution:

Following is the script that give you the name of parent folder of the active spreadsheet:


function getParentFolder() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  
  var file = DocsList.getFileById(ssId);
  var fileId = file.getId();
  
  var ParentFolders = file.getParents();
  
  Browser.msgBox(ParentFolders[0].getName());
}

Note: To run this code you will have to authorize the script, and after authorization you will be able to use the above function.


And If you are not much familiar with scripts then check out the following link:




I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.

I also take up private or confidential projects:

If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 

Thanks,
Kishan,

No comments:

Post a Comment