Search code examples
google-apps-scriptdrive

Get a Drive folder by url in Apps Script


I am trying to automate processes from a from. Basically somebody solicits documentatio for a specific client (school). I need the copy of a file created in an existing folder for that shcool. Thing is, when we fill out the solicitation form, we ask for the drive folder url, and I can't seem to get the id from that url, nor access the folder from the url either:

    function intialEv() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
  var lastRow = ss.getLastRow();
  var data = ss.getRange(lastRow,1,lastRow,11).getValues();
  var folderUrl = data[0][7];
  var date = new Date().getMonth();
  var lang = data[0][8];
  var contact = data[0][10];
  var schoolName = data[0][3];
  var coord = data[0][5];

  // Make a copy of a decoy sheet, setting same name with school and date, copy being created in the destination folder signaled.
  var newSheet = DriveApp.getFileById('1K_QuWNa4OPjlC0S3R76XBILQkqd6kh54hRcXVqruVto').makeCopy("Evaluación Previa_" + schoolName + "_" + date, folderUrl);

When I log folderUrl it returns the entire url. Is there a way I can extract the id? Or even more quickly, get the folder in question by url?


Solution

  • Do your folder URLs look like this?

    https://drive.google.com/drive/folders/0BzBleEfbQeCuUWs3UFwySTJ7LTf

    If they do, try this:

    const folder = DriveApp.getFolderById(folderUrl.replace(/^.+\//, ''));

    Related issue:

    Reference: