Pages

Thursday, 27 December 2018

Split file upload Url

The following Google Apps Script code is designed to extract the file ID from a Url of a document that has been uploaded via a Google Form. You can use the function to feed in a Url (typically acquired from a spreadsheet of data), split it to get the ID, then instruct 'DriveApp' to get the file so you can do something with it.

An example Url has been included below for demo purposes only, with 'split' being used to separate the Url via the equals sign (=). The result of this is now 2 components of which the latter is the ID we want (so 1ZvhkehvoL99EmmzUsy481givp31odMaO in this example) so we can assign this to a variable. Finally, we can now use 'getFileById' to open it.

function splitUrlFile(Url) {
// remove this line when you incorporate into your script
var Url = 'https://drive.google.com/open?id=1ZvhkehvoL99EmmzUsy481givp31odMaO';
// split the Url via the equals sign to break into two
var split = Url.split('=');
// the file ID is the second part of the split (first part being zero)
var uploadID = split[1];
Logger.log('Upload file ID is: ' + uploadID);
// instruct DriveApp to open the file by its ID
var file = DriveApp.getFileById(uploadID);
Logger.log('File is: ' + file);
}
Split Url File Upload.gs

No comments:

Post a Comment