Felipe Chagas
Felipe Chagas

Reputation: 470

Get file ID by its folder and name

I'm copying a specific file to a specific folder, here's the code:

  var fileID = "123abc";
  var folderID = "abc123";

  var name = "Conferencia " + Utilities.formatDate(source.getRange(2,4).getValue(), "GMT", "dd-MM-yyyy");

  var file = DriveApp.getFileById(fileID);  
  var folder = DriveApp.getFolderById(folderID);


  var newFile = file.makeCopy(name,folder);
  var newUrl = DocumentApp.openById(newFile.getId());
  Logger.log(newUrl);

This code works perfectly, except by the var newUrl = DocumentApp.openById(newFile.getId());. This line should record the ID of the new file but it won't work.

How to make it work?

Upvotes: 2

Views: 4277

Answers (1)

user6655984
user6655984

Reputation:

var newUrl = DocumentApp.openById(newFile.getId());

means that newUrl is now a Document object, pointing to your Google Document contained in that file (presumably it's a Google Document file, otherwise there's an error already). It's not an Id or URL or a string of any sort.

If you want to get the URL of the file, use the getUrl method:

var newUrl = newFile.getUrl();

If you want to get the Id (which is just the last part of the URL), then it's

var newId = newFile.getId();

These are strings which can later be used in DocumentApp.openByUrl or DocumentApp.openById if you want to use them; these methods will return a Document object.

Upvotes: 2

Related Questions