Search code examples
google-apps-scriptgoogle-drive-api

Drive.Files.get(fileID).thumbnailLink Constantly Throwing Null Error


I am trying to convert a pdf to a png and to do so was following some other chains on here and got the following code from one of those. I've looked for alternative methods but it seems like everyone uses Apps Script Issue with Drive.Files.get(fileID).thumbnailLink. When I use this with my pdf instead of getting a string I get a null error. Any ideas why? In the grand scheme of things I'm trying to get a range of cells in google sheets as a png. Is there a better way to go about this? This is just the best I've come up with thus far.

The code I'm using to get a pdf to a png. The pdf I'm trying to change is in my drive, I changed the permissions so anyone with a link can edit, and I triple checked that I do have Drive API enabled. When i check the variable driveFileObj in the debugger it does look like it's finding the right file, so I don't think that's it either.

function anyFileToPng(fileId, destinationFolderID) {
  const driveFileObj = Drive.Files.get(fileId);  

  // Change the thumbnailUrl to get a larger file.
  Logger.log(driveFileObj.thumbnailLink)
  const thumbnailURL = driveFileObj.thumbnailLink.replace(/=s.+/, "=s2500")  


  // Take the filename and remove the extension part.
  const fileName = driveFileObj.title;
  const newFileName = fileName.substring(0, fileName.lastIndexOf('.')) || fileName 


  // Fetch the "thumbnail-Image".
  const pngBlob = UrlFetchApp.fetch(thumbnailURL).getBlob();
  
  // Save the file in some folder...
  const destinationFolder = DriveApp.getFolderById(destinationFolderID)
  destinationFolder.createFile(pngBlob).setName(newFileName+'.png');
}

I also tried DriveApp.getFileById(fileId).getThumbnail().getAs('image/png') which worked but the png was pretty blurry.


Solution

  • From your issue of When I use this with my pdf instead of getting a string I get a null error, I guess that you enabled Drive API v3 instead of v2. In the current stage, when Drive API is enabled at Advanced Google serviced, v3 is automatically set as the default version. I think that your showing script is for Drive API v2. So, when you change Drive API v3 to Drive API v2, this script might be able to be used.

    When you want to use your script with Drive API v3, please modify as follows.

    Modified script:

    Before you use this script, please confirm whether you have already enabled Drive API v3 again.

    In the case of Drive API v3, the default returned fields are "mimeType","kind","name","id". So, in order to retrieve the field of thumbnailLink, it is required to manually set the fields as follows. Also, in the case of Drive API v3, the filename is retrieved with the field of name. Please be careful about this.

    function anyFileToPng(fileId, destinationFolderID) {
      const driveFileObj = Drive.Files.get(fileId, { fields: "name,thumbnailLink" });
    
      // Change the thumbnailUrl to get a larger file.
      Logger.log(driveFileObj.thumbnailLink)
      const thumbnailURL = driveFileObj.thumbnailLink.replace(/=s.+/, "=s2500")
    
      // Take the filename and remove the extension part.
      const fileName = driveFileObj.name;
      const newFileName = fileName.substring(0, fileName.lastIndexOf('.')) || fileName
    
      // Fetch the "thumbnail-Image".
      const pngBlob = UrlFetchApp.fetch(thumbnailURL).getBlob();
    
      // Save the file in some folder...
      const destinationFolder = DriveApp.getFolderById(destinationFolderID)
      destinationFolder.createFile(pngBlob).setName(newFileName + '.png');
    }
    

    Note:

    • I'm worried that in the current stage, the image size might be less than =s2500 because of the current specification. There might be the maximum image size using =s####. Please be careful about this.

    Reference: