I'm trying to create a reference to the current DriveApp File object (i.e. the actual Google Drive container for the script itself). Here's what I'm trying to do:
// Assume that thisFile is a FileIterator pointing to THIS file.
// My question in a nutshell: How do I get thisFile?
DriveApp.getFileById( thisFile.getId() );
I want to do this instead of DriveApp.getFileById(id) because I don't want to have to "hard code" the ID string for the script file. That way, if I make a copy of the Google Apps Script project, I don't have to edit anything in the Code.gs or Script Properties. Any suggestions?
I'm trying to create a reference to the current DriveApp File object (i.e. the actual Google Drive container for the script itself). Here's what I'm trying to do:
// Assume that thisFile is a FileIterator pointing to THIS file.
// My question in a nutshell: How do I get thisFile?
DriveApp.getFileById( thisFile.getId() );
I want to do this instead of DriveApp.getFileById(id) because I don't want to have to "hard code" the ID string for the script file. That way, if I make a copy of the Google Apps Script project, I don't have to edit anything in the Code.gs or Script Properties. Any suggestions?
Share Improve this question asked Jul 18, 2014 at 21:37 pumpkin_programmerpumpkin_programmer 831 silver badge6 bronze badges2 Answers
Reset to default 7An Apps Script can be standalone or have one of 4 containers (nowadays): Google Sheets, Docs, Forms and Sites. If you know before-hand the type of the container, then it's easy to get its id
. e.g.
var id = SpreadsheetApp.getActiveSpreadsheet().getId();
var id = DocumentApp.getActiveDocument().getId();
var id = FormApp.getActiveForm().getId();
var id = SitesApp.getActiveSite().getId();
If you don't know the container type (which would be weird), you could wrap these in various try-catch
es and find out.
Assuming it's not a Google Site, then you can safely get this id and use it to retrieve the DriveApp
File representation of the container.
var file = DriveApp.getFileById(id);
But this file
is just an object of the DriveApp
API, it's not really a this
reference, as in the programming meaning. You could have just as easily used DocsList
or advanced Drive
service to retrieve a different representation of the same container.
Lastly, I do not know how to grab the id of a standalone script dynamically (from within itself). I suspect it's not possible. But I don't see how that could be useful either. --edit If you want just to copy, why not grab a "fixed" source script directly?
If you click on your Apps Script file, open up the details window on the right, and look at the file details for an Apps Script, you'll see a type property. Hover your pointer over the words "Google Apps Script" for the type, and it should show:
application/vnd.google-apps-script
I think you can search files for type. searchFiles(params)
Google Documentation - searchFiles() method
Also see:
Search Parameters
I think you would search by mime-type:
mimeType = 'application/vnd.google-apps-script'
So, you could find all Apps Script files, in the users drive, then if you could set a unique property on the Apps Script file, you could search for that also.
Or search by fullText
, which includes the file description, and put a unique description in the file. That way you would be sure to find the apps script file that you created. Then, once search has found the file, you have your reference.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744354233a4570151.html
评论列表(0条)