3

I would like to display easily an image on Gsheet cell based from URL of image stored in Gdrive.

I have tried with the Gsheet function =IMAGE("URL") and it does not work.

The objective is to display an image picture as the example below (example of picture in 5th row was done manually)

Example of image displayed

Mohamed H
  • 159
  • 1
  • 4
  • 13

2 Answers2

7
  • You want to put the image in your Google Drive to your Google Spreadsheet.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Pattern 1:

In this pattern, the image is put using =IMAGE("URL").

When =IMAGE("URL") is used, the image is required to publicly shared. So please share the images with publicly shared as On - Anyone with the link.

And also, please modify the endpoint as follows.

From:

https://drive.google.com/open?id=###

to:

https://drive.google.com/uc?export=download&id=###
  • In this case, you can put the image with =IMAGE("https://drive.google.com/uc?export=download&id=###") after the image is shared publicly.

Pattern 2:

If you don't want to share publicly the images, how about this pattern? In this pattern, the image is put as the blob without sharing publicly.

Here, please check the following sample script.

Sample script:

var fileId = "###";  // Please set the file ID of the image.

var sheet = SpreadsheetApp.getActiveSheet();
var blobSource = DriveApp.getFileById(fileId).getBlob();
var image = sheet.insertImage(blobSource, 1, 1);
image.setWidth(100).setHeight(100);
sheet.setColumnWidth(1, 100).setRowHeight(1, 100);
  • When you run the script, the image is put to the cell "A1". And the image size is resized to 100 x 100 pixels. And then, the row and column size is changed for the image size.
  • This is a simple sample script. So please modify this for your actual situation.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Added:

From your replying, it was found that the image size is over than the limitation size (1,048,576 pixels^2) Ref The reason of your current is is this.

In this case, in order to put the image, it is required to resize the image. The following sample script puts the image by resizing image size. For this, I used a Google Apps Script library. So please install it to the script editor.

Sample script:

var fileId = "###";  // Please set the file ID of the image.

var sheet = SpreadsheetApp.getActiveSheet();
var blobSource = DriveApp.getFileById(fileId).getBlob();
var obj = ImgApp.getSize(blobSource);
var height = obj.height;
var width = obj.width;
if (height * width > 1048576) {
  var r = ImgApp.doResize(fileId, 512);
  blobSource = r.blob;
}
var image = sheet.insertImage(blobSource, 1, 1);
image.setWidth(100).setHeight(100);
sheet.setColumnWidth(1, 100).setRowHeight(1, 100);
  • In this sample script, when the image size is over than 1,048,576 pixels^2, the image is resized and put to the Spreadsheet.
  • This is a simple sample script. So please modify it for your actual situation.

References:

Tanaike
  • 139,542
  • 10
  • 71
  • 111
  • I have followed your recommendation (Pattern 2) and I have added your script sample in my project, and I have met an error during running of script on the following line: `var image = sheet.insertImage(blobSource, 1, 1);` I have tried several solution from stackoverflow as [link1](https://stackoverflow.com/questions/45220820/adding-image-to-sheet-from-drive) and [link2](https://stackoverflow.com/questions/41020598/insert-image-into-spresheet-cell-from-drive-using-google-apps-script) and I have not succeeded – Mohamed H Feb 19 '20 at 22:08
  • Thank you for your feedback I have tested the first pattern and the solution is OK only if the file is shared with Public access as you mention previously. However, I cannot shared publicly the picture due to company compliance rules. I will check the second solution and will give you my feedback – Mohamed H Feb 19 '20 at 22:15
  • @Mohamed H Thank you for replying. I apologize for the inconvenience. About the pattern 1, I could understand about your actual situation. About the pattern 2, about `I have met an error during running of script on the following line: var image = sheet.insertImage(blobSource, 1, 1);`, unfortunately, I couldn't image the detail error message from it. This is due to my poor skill. I deeply apologize for this. Can you provide the error message? By the way, the owner of file of the URL is you? – Tanaike Feb 19 '20 at 23:30
  • the error message is in french _Erreur liée à un service : Feuilles de calcul (ligne 6, fichier "InsertImageInCell")_ about the line in script `var image = sheet.insertImage(blobSource, 1, 1);`. Yes I am the owner of Ghseet. – Mohamed H Feb 21 '20 at 08:23
  • @Mohamed H Thank you for replying. Unfortunately, I cannot understand about french. This is due to my poor skill. I deeply apologize for this. Can I ask you about the meaning of the error message? And in your script, can I ask you about the value of `fileId` of `DriveApp.getFileById(fileId).getBlob();` you tested? Because if you are using URL for it, an error occurs. But if I misunderstood your situation, I apologize. – Tanaike Feb 21 '20 at 09:35
  • @Mohamed H By the way, can I ask you about the image size of the file you want to put? There is the limitation for the image which can be put to Spreadsheet. About this, you can see the information at https://gist.github.com/tanaikech/9414d22de2ff30216269ca7be4bce462 Also please be careful this. – Tanaike Feb 22 '20 at 01:36
  • the image size are about Dimensions ‪4 000 pix x 3 000‬ pix. The image are photos from smartphone. – Mohamed H Feb 25 '20 at 11:03
  • @ Tanaike about error message, Gtranslate : _Exception: Service-related error: Worksheets (line 6, "InsertImageInCell" file)_ . The ID value is set as `var fileId = '1K4A2DBAVVJwhECsTMrlAKlwo_nuE94mZ'` from the URL image _https://drive.google.com/open?id=1Rs36UEEE4R3ClVVXAMuZtSfBcBzTK9pp_ I would like to know how to test this line in script. Thank you – Mohamed H Feb 25 '20 at 11:19
  • @Mohamed H Thank you for replying. From your replying, I could confirm the reason of your issue. The issue was the size of the image. There is the limitation area for putting the image to Spreadsheet. The limitation area is `1,048,576 pixels^2`. In your case, `4 000 pix x 3 000‬ pix = 12,000,000` is over than that. By this, such error occurs. So can you test the script by reducing the size of image? For example, when you want to resize the image using script, you can also use the script for resizing image. https://stackoverflow.com/a/58985061/7108653 – Tanaike Feb 25 '20 at 22:48
  • @Mohamed H I proposed a sample script for putting the image by resizing. Could you please confirm it? If that was not useful, I apologize again. – Tanaike Feb 25 '20 at 23:20
0
=image("https://drive.google.com/uc?export=download&id="&right(A2;33);1). 

This works for me

Suraj Rao
  • 28,850
  • 10
  • 94
  • 99