0

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]

Alex S.
  • 61
  • 7

1 Answers1

1

I would map through the array and create a new array with the url ids. I found the getParameterByName function under this question.

function getParameterByName(name, url) {
    if (!url) console.error('url is not defined');
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
    results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
};

let idArray = []; //create empty array
const urlArray = [
    'https://drive.google.com/open?id=1zeUiH8oO_Y2lnvcV1ZeNr7b3eNQP8aEH',
    'https://drive.google.com/open?id=1qmKl9klRTPPKO52owEuU7qMgYbMCuxs3', 
    'https://drive.google.com/open?id=1FqH5NOpWyvcQ-XZWwcBImUJ5UBy98UHC'
];

//map through array and push the returned ids to the new Array, you could use any for-loop to do this
urlArray.map(url => {
    const id = getParameterByName('id', url);
    idArray.push(id);
});
Andrea V
  • 26
  • 2
  • 1
    Thanks! I never would've thought of this. I went ahead and adapted it to my code by creating the urlArray with the split() function, and changed the array map to a for-loop. Works great. – Alex S. Jun 24 '19 at 14:18