How do I isolate Drive file ID from URL using regex?

I am trying to get the file name for a file I only have the Google Drive URL for. I am currently using a Google Sheets regexextract function to extract the file ID from the URL, and then using the script to find the file based on the ID, but would prefer to do the regex within the script.

I've looked through various posts on here to try to figure it out with no luck. Hopefully someone can spell out what I have done wrong in trying to incorporate the regex.

  var sheet = SpreadsheetApp.getActive().getSheetByName("Test1");
  var link1 = sheet.getRange("N2:N").getValues();
  var regex_ids = new RegExp("/file/d/[a-zA-Z0-9]g");
  var links = regex_ids.exec(link1);
  var filenames = []; 
  for (var i = 0; i < links.length; i++) {
    var url = links[i][0];
    if (url != "") {
      var filename = DriveApp.getFileById(links[i][0]).getName();
  var startRow = 2; // print in row 2 since row 1 is the header row
  var fileNameColumn = 18; // Column B = column 2
  var destination = sheet.getRange(startRow, fileNameColumn, filenames.length, filenames[0].length);

Currently I am stuck with error "TypeError: Cannot read property "length" from null. (line 7, file "Code")" because the regex is not configured correctly.

1 answer

  • answered 2019-06-25 05:13 TheMaster


    • Invalid Syntax:

      • Regexp() accepts a regex string and a flag string as arguments, while the code provides a concatenated regex flag string.
      • exec() accepts a string argument, while the code provides a 2D array.
    • Insufficient regex:

      • Filename IDs also contain underscores _.
      • Regex should capture() only the ID. The regex provided also captures /file/d
      • ID contains more than 1 character. Use +


      var link1 = sheet.getRange("N2:N" + sheet.getLastRow()).getValues();//modified
      var regex_ids = /\/file\/d\/([^\/]+)/;//or new RegExp("/file/d/([a-zA-Z0-9_]+)","g");() =capture ids
      //var links = regex_ids.exec(link1);
      var filenames = []; 
      for (var i = 0; i < link1.length; i++) {//modified;loop through values2D array 
        var url = link1[i][0];//modified; 
        var preId = regex_ids.exec(url);//added;
        var id;
        if (preId && (id=preId[1])) {//modified; [1] = first capture group
          var filename = DriveApp.getFileById(id).getName();//modified
        } else {