How to Display Thumbnails in Spreadsheet Cells

Asked 2 years ago, Updated 2 years ago, 58 views

The code below will complete successfully, but the thumbnails will not be displayed. What's the problem?

function dispThumbnail(){
   varsheet=SpreadsheetApp.getActiveSheet();
   varfile=DriveApp.getFilesByName("a01.jpg").next();
   sheet.getRange(1,1).setValue(file.getThumbnail());
   sheet.getRange(1,2).setValue(file.getId());
 }

By the way, Google Developers reference Class File getThumbnail() says `Return type=Blob, Brief description says Gets a thumbnail image for the file, or null if no thumbnail exists.

sheet.getRange(1,2).setValue(file.getId());

displays correctly.

google-apps-script

2022-09-30 20:48

1 Answers

Valid arguments for Range#setValue(value) are numeric, string, boolean order date, so return File#getTumbnail(value)Blob cannot be specified.

Instead, use Spreadsheet#insertImage(blob, column, row) to

function dispThumbnail(){
   varsheet=SpreadsheetApp.getActiveSheet();
   varfile=DriveApp.getFilesByName("a01.jpg").next();
   sheet.insertImage(file.getThumbnail(), 1,1);
   sheet.getRange(1,2).setValue(file.getId());
}

I should be able to write like this, but at this point getThumbnail() doesn't return Blob.

Issue4344-Google-apps-script-issues-DriveApp file.getThumbnail() always returns null

According to the bug report above, please use Drive Advanced Service for now.

Then, for example, like this.

function myFunction(){
  varsheet=SpreadsheetApp.getActiveSheet();
  varfile=DriveApp.getFilesByName("a01.png").next();
  sheet.insertImage(Drive.Files.get(file.getId()) .thumbnailLink,1,1);
  sheet.getRange(1,2).setValue(file.getId());
}

You can specify the URL obtained by thumbnailLink as =image().


2022-09-30 20:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.