Google Sheets – Inserting an Image Using Google Scripts

google sheetsgoogle-apps-script

Does anyone know if it is possible to insert an image from a URL into a Google Sheet using Google Scripts?

Best Answer

Solution 1 (recommended):

Use directly the insertImage(url, column, row) which accepts the url of the image:

function imgToSheet() {
  const img_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_logo.svg.png';
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet1"); //change to the name of your sheet
  // Insert the image in cell B1.
  sh.insertImage(img_url, 2, 1); 
}

Solution 2:

You can use fetch(url) to fetch an image from a url and then insertImage(blobSource, column, row) to insert the image to the sheet.

Code snippet:

function insertImageOnSpreadsheet() {
  const img_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_logo.svg.png';
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet1");//change to the name of your sheet
  const response = UrlFetchApp.fetch(img_url);
  const rd = response.getContent();
  // Insert the image in cell B1.
  const blob = Utilities.newBlob(rd, 'image/png', 'MyImageName');
  sh.insertImage(blob, 2, 1);
}

Limitations:

Unlike Solution 1, the maximum supported blob size for Solution 2 is 2MB.


Solution 3 - Google Sheets Formula:

You can use IMAGE which can locate the image inside a particular cell.

=IMAGE("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_logo.svg.png")