- 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:
Updated: September 30, 2023
As the current method, I think that the following patterns might also be able to be used.
Script 1:
In this script, the file content retrieved from Google Drive is used as an object of SpreadsheetApp.CellImage. In this case, when the image size is large, an error might occur. Please be careful about this.
const fileId = "###fileId###": // Please set the file ID of the image file on Google Drive.
const file = DriveApp.getFileById(fileId);
const dataUrl = `data:${file.getMimeType()};base64,${Utilities.base64Encode(file.getBlob().getBytes())}`;
const img = SpreadsheetApp.newCellImage().setSourceUrl(dataUrl).build();
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A1").setValue(img);
When this script is run, an image is put into a cell "A1".
Script 2:
In this script, the thumbnail of file content retrieved from Google Drive is used as an object of SpreadsheetApp.CellImage. In this case, even when the image size is large, this script can be used, because the image size is resized.
const fileId = "###fileId###": // Please set the file ID of the image file on Google Drive.
const imageUrl = `https://drive.google.com/thumbnail?sz=w1000&id=${fileId}`; // Ref: https://stackoverflow.com/a/31504086
const bytes = UrlFetchApp.fetch(imageUrl, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getContent();
const dataUrl = `data:${MimeType.PNG};base64,${Utilities.base64Encode(bytes)}`;
const img = SpreadsheetApp.newCellImage().setSourceUrl(dataUrl).build();
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A1").setValue(img);
// DriveApp.getFiles(); // This comment line is used for automatically detecting a scope of https://www.googleapis.com/auth/drive.readonly This is used for ScriptApp.getOAuthToken()
When this script is run, an image is put into a cell "A1".
References: