0
votes

Does anyone know hot to get the name of a file on drive using an URL?

At the moment I have the below code but it just hangs for some reason :(.

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var jobID = 262;
  var jobFormUrl = 'URL';
  var jobFormId = getIdFromUrl(jobFormUrl);
  var filename = DriveApp.getFileById(jobFormId).getName();

  ss.getSheetByName('Job ID '+jobID).getRange(2,2).setValue('=hyperlink("'+jobFormUrl+'","'+filename+'")');
}

function getIdFromUrl(url) {
  return url.match(/[-\w]{25,}/);
}
2
"[...] but it doesn't work"? Try to describe what is not working exactly. - fuma
@fuma Sorry about that. I've edited the post. - CristianCapsuna

2 Answers

1
votes

The getIdFromUrl() function is returning an array whereas it should return a string representing the ID of the file.

function getIdFromUrl(url) {
  return url.match(/[-\w]{25,}/)[0];
}
0
votes

Moving the quotes from line 8 to the variable definition fixes this. So instead of var jobFormUrl = 'URL'; it should be var jobFormUrl = '"URL"';