简体   繁体   中英

I need to display a pdf in my google drive as a thumbnail ,in a google sheet..how to do it Using google script?

I have a folder in google drive which contains pdfs. Some of them contain images and some do not. I need to display each of those pdf files as thumbnails in a google sheet using a google script. Please help me if you know any method to do it...

You can create Thumbnail of pdf file and save it to drive. If you want resize thumb, you can use thumbnailLink.

  1. Read file pdf to get thumbnail: var file = DriveApp.getFileById('xxxx- file id');

  2. Get thumbnail blob: var thumbBlob = file.getThumbnail();

  3. Save thumbnail to Drive: var res = Drive.Files.insert({title: "temp.jpg"}, thumbBlob);

  4. Get thumbnailLink: Logger.log(file.thumbnailLink);

The thumbnailLink is https:// lh3.googleusercontent.com/.....=s220 . You have to change 220 to your desired width.

Sorry to post this as a new answer, but don't have enough reputation to comment on the above. Please advise if I am to post this in a different way/open a new thread??

The solution provided by @David Nguyen did not work for me. I'm trying to create the thumbnail of a PDF file in my drive. The following script doesn't go anywhere (because thumbBlob = null) as this file does not have a thumbnail in its metadata.

 function newtry() {
  //Read file pdf to get thumbnail:
  var file = DriveApp.getFileById('myFileUniqueID');
  //Get thumbnail blob:
  var thumbBlob = file.getThumbnail();
  //Save thumbnail to Drive: 
  var res = Drive.Files.insert({
    title: "temp.jpg"
  }, thumbBlob);
  //Get thumbnailLink:
  Logger.log(file.thumbnailLink);
}

To me, it looks like this script can only be used to save existing thumbnails as a separate image file, correct? But how can I (bulk) create thumbnails for all my drive files (that don't have yet)? Have been looking and trying for hours now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM