Google apps spreadsheet parents folder ID

浪尽此生 提交于 2019-12-01 21:06:43

问题


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


回答1:


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.

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.




回答2:


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


来源:https://stackoverflow.com/questions/17582161/google-apps-spreadsheet-parents-folder-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!