Here is what I'm trying to do: given a Google document URL, I want to get the document ID to create a copy on Google Drive. I know I can achieve that by some regex or replacing on the URL, but as there are several different forms to represent the same document in a URL, I wanted to find a generic solution.
Currently, that's the best I could think:
function getFileIdFromUrl(url) { try { return getDocIdFromUrl(url); } catch (e) { return getSpreadsheetIdFromUrl(url); } } function getDocIdFromUrl(url) { var doc = null; try { doc = DocumentApp.openByUrl(url); } catch (e) { doc = DocumentApp.openByUrl(url + "/edit"); } return doc.getId(); } function getSpreadsheetIdFromUrl(url) { var spreadsheet = null; try { spreadsheet = SpreadsheetApp.openByUrl(url); } catch (e) { spreadsheet = SpreadsheetApp.openByUrl(url + "/edit"); } return spreadsheet.getId(); } function copy(url) { // may throw an exception if the URL is invalid or private var id = getFileIdFromUrl(url); var file = DriveApp.getFileById(id); file.makeCopy().setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); }
The problem is that my solution only covers documents and spreadsheets, I would like to do the same with any uploaded file, for example:
https://docs.google.com/file/d/0B-FYu_D7D7x4REdtRVEzVH0eU0/edit
In short, I wanted something like that:
DriveApp.getFileByUrl(url).makeCopy();
Does anyone know if it's possible?
Any safe solution to extract the file ID from the file URL would fit as well for me.
Thanks
File ID (text): Unique ID of the file. The ID of a file can be found by navigating to the Google Drive web. Right click on the file you need, and select get sharable link . The File ID is the last part of that link, after id= .
The easiest way to find this ID is to look in the address bar on your web browser when you open the Sheet. You can find the ID for any Google Sheet or Doc in the address bar when that Sheet or Doc is open in your web browser.
If you want to 'show' the URL, just change this line like this : var link = app. createAnchor(href, href). setId("link");
It is also possible to get a file by the file's URL. The ID of a file is in the url, so using the ID instead of the entire URL means that the parameter is shorter. Storing the URL rather than the ID takes up more space.
To extract id from url spreadsheets I use the code below. It works with google spreadsheet and Excel in Drive. Maybe works with other docs too. function getIdSheetFromUrl_ (url) { var id = url.split ('id=') ; if (!id) { id = url.split ('/d/') ; id = id.split ('/edit') ; // here we have the id } return DriveApp.getFileById (id); }
google.script.url is an asynchronous client-side JavaScript API that can query URLs to obtain the current URL parameters and fragment. This API supports the google.script.history API.
For other non-expert people like me, in Google Sheets: 1 Go to tools>script editor. 2 Clear everything, then paste the script. 3 At the "THIS_SHOULD_BE_YOUR_FOLDER_ID" you have to insert your Folder ID (Just the ID, but leave the "". You can find the ID if you open your folder ...
DriveApp is indeed missing a getFileByUrl
(and also folder for that matter). You may want to open an enhancement request on Apps Script issue tracker.
But what I do on my scripts (since these openByUrl
functions are somewhat new), is to get the id using a regex. Like this.
function getIdFromUrl(url) { return url.match(/[-\w]{25,}/); }
This regex works for any google url I've tried: Drive url for folders and files, Fusion Tables, Spreadsheets, Docs, Presentations, etc. It just looks for anything in a string that "looks like" a Google key. That is, any big enough string that has only (google key) valid characters in it.
Also, it works even if it receives the ID directly, instead of the URL. Which is useful when you're asking the link from the user, as some may paste the id directly instead of the url and it still works.
--edit
There are some other answers and comments that address some edge cases that I never encountered myself but might happen, like trying to get a folder-id on a nested folder URL, or when you have G-Suite domain that is 25+ characters long. For those cases, you might want to use a more strict regex.
From a quick look at the suggestions below I recommend the following /[-\w]{25,}(?!.*[-\w]{25,})/
because it is still very simple and should address these cases.
The url is something like this and file id is present in this pattern "/d/XXXXXXXX/" for almost all GoogleDrive/Docs links:
https://drive.google.com/file/d/0B3tB9BU9FRnpcTJmS2FoaktsQzA/view
Using below function, we can get the '/d/fileid/' and then truncate '/d/' from begining and '/' from end.
public static string getIdFromUrl(string url) { Regex r = new Regex(@"\/d\/(.+)\/", RegexOptions.IgnoreCase); Match m = r.Match(url); return m.ToString().TrimStart('/', 'd').Trim('/'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With