Putting Image into Cell of Spreadsheet using Google Apps Script
These are sample scripts for putting an image into a cell of a Spreadsheet using Google Apps Script.
Sample 1
In this sample, the image is put into a cell using thumbnailLink retrieved by Drive API. So, when you test this, please enable Drive API at Advanced Google services. The image is put into cell “A1”.
function sample1() {
const fileId = "###"; // Please set the file ID of the PNG image file on Google Drive.
const url = Drive.Files.get(fileId).thumbnailLink.replace("=s220", "=s1000");
const image = SpreadsheetApp.newCellImage().setSourceUrl(url).build();
const range = SpreadsheetApp.getActiveSheet().getRange("A1");
range.setValue(image);
const value = range.getValue();
console.log(value.getUrl()); // ---> null
console.log(value.getContentUrl()); // --> Exception: Unexpected error while getting the method or property getContentUrl on object SpreadsheetApp.CellImage.
}
Sample 2
In this sample, the image is put into a cell using the data URL. The image is put into cell “A1”. In this case, I believe that when the data URL is used, this method will be able to be used for various situations.