Home > database >  How to check if a file exists (by id) in GAS
How to check if a file exists (by id) in GAS

Time:10-30

I am aware of q&a on how to check if file exists by name (using hasnext()). I need to check however by file ID, preferably without the advanced Drive API.

Disclosure: I wrote a solution based on error handling:

function ifFileExists(id){
  try {DriveApp.getFileById(id);return true;}
  catch(e) {return false}
}

But this feels like clumsy. Anyone knows a better solution?

CodePudding user response:

I believe your goal is as follows.

  • You want to check whether the file ID is existing using Google Apps Script.
  • You don't want to use DriveApp.getFileById(id) and Drive API.

In this case, as a workaround, how about checking it using the thumbnail link? Ref In this case, when the file ID is existing, the sign-in HTML with the status code of 200 is returned. On the other hand, when the file ID is not existing, an error like Error 404 (Not Found) occurs. I thought that this situation might be able to be used for your situation. When this is reflected in a sample script, how about the following sample script? In this sample script, UrlFetchApp is used.

Sample script:

function myFunction() {
  const fileId = "###"; // Please set the file ID.

  const res = UrlFetchApp.fetch(`https://drive.google.com/thumbnail?id=${fileId}`, { muteHttpExceptions: true }).getResponseCode() == 200 ? true : false;

  console.log(res);
}
  • In this case, when res is true and false, it indicates that the file ID is existing and not existing, respectively.

  • In this case, Drive API is not required to be enabled and only the scope of https://www.googleapis.com/auth/script.external_request is used.

  • When your script is modified, how about the following modified script?

      function ifFileExists(id){
        return UrlFetchApp.fetch(`https://drive.google.com/thumbnail?id=${id}`, { muteHttpExceptions: true }).getResponseCode() == 200 ? true : false;
      }
    

Reference:

CodePudding user response:

There is nothing wrong on using DriveApp.getFileById(id) to check if a file "exists" based on it's id, other than the file might actually exists but the Google Apps Script effective user hasn't access to that file.

The above because the reference documentation specify that this method will throw an exception, so it's perfectly file to use try...cacth to catch that execption.

You might want to do something more sofisthicated like having a default handler for the expected exception and another for unexpected exceptions. If your scripts users is a small group that all have set their Google account language to English it is something easy, but if you are planning to write and addon, this could be hard as Google Apps Script exceptions only return a string message and it depends on the effective account language. Fortunately someone already did a lot of work to handel this, created a library and shared in GitHub --> https://github.com/RomainVialard/ErrorHandler

  • Related