I need to store all file ID's from a string of URL's. When a Google Forms user uploads multiple files in a form response, the Google Drive URL's to all these files are spit out into a single cell in a sheet in a format like:
https://drive.google.com/open?id=1zeUiH8oO_Y2lnvcV1ZeNr7b3eNQP8aEH,
https://drive.google.com/open?id=1qmKl9klRTPPKO52owEuU7qMgYbMCuxs3, https://drive.google.com/open?id=1FqH5NOpWyvcQ-XZWwcBImUJ5UBy98UHC
(These are made up URL's FYI)
My goal is to read this cell and store the ID's in an array, like this example: [1zeUiH8oO_Y2lnvcV1ZeNr7b3eNQP8aEH, 1qmKl9klRTPPKO52owEuU7qMgYbMCuxs3, 1FqH5NOpWyvcQ-XZWwcBImUJ5UBy98UHC]
.
I found a thread that provided a regex to isolate the ID string in a single URL, so I tried to adapt it to use multiple URL's. Unfortunately, I'm not quite sure what I'm doing due to my nominal experience with JavaScript and especially with JavaScript regex, so don't judge if I made a dumb mistake on my code :) .
Thanks for the help everyone. Here's my code:
function getIdFromUrls(urls) {
var idArray = [];
var parts = urls.split(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/);
if (urls.indexOf('?id=') >= 0){
idArray = (parts[6].split("=")[1]).replace("&usp","");
return idArray;
} else {
idArray = parts[5].split("/");
//Using sort to get the id as it is the longest element.
var sortArr = idArray.sort(function(a,b){return b.length - a.length});
idArray = sortArr[0];
Logger.log(idArray);
return idArray;
}
}
Here's the log:
[19-06-24 07:46:20:532 EDT] 1zeUiH8oO_Y2lnvcV1ZeNr7b3eNQP8aEH, https://drive.google.com/open?id
Expected Result:
[19-06-24 07:46:20:532 EDT] [1zeUiH8oO_Y2lnvcV1ZeNr7b3eNQP8aEH, 1qmKl9klRTPPKO52owEuU7qMgYbMCuxs3, 1FqH5NOpWyvcQ-XZWwcBImUJ5UBy98UHC]