Set Cell Images From Urls

setCellImagesFromUrls(sheet, urlCol, imageCol) reads image URLs from one column and writes SpreadsheetApp.newCellImage() objects into another. Blank cells and URLs that fail to build are skipped (those image cells are left as they were). Successful images are written in batches of contiguous rows.

When to use

  • A catalog or roster with a URL column that should show thumbnails in-sheet
  • Refreshing product images after an import
  • Any sheet where =IMAGE() is not enough and you want Cell Image values

How to use this snippet

/** * Write Cell Images from a URL column into an image column, starting at row 2. * urlCol and imageCol are 1-based. Blank or invalid URLs are skipped. * Returns the number of images written. * * @param {GoogleAppsScript.Spreadsheet.Sheet} sheet * @param {number} urlCol * @param {number} imageCol * @return {number} */ function setCellImagesFromUrls(sheet, urlCol, imageCol) { var last = sheet.getLastRow(); if (last < 2) return 0; var urls = sheet.getRange(2, urlCol, last - 1, 1).getDisplayValues(); var built = []; for (var i = 0; i < urls.length; i++) { var url = String(urls[i][0] || '').trim(); if (!url || !/^https?:\/\//i.test(url)) { built.push(null); continue; } try { built.push(SpreadsheetApp.newCellImage().setSourceUrl(url).build()); } catch (e) { built.push(null); } } var set = 0; var i = 0; while (i < built.length) { if (!built[i]) { i++; continue; } var chunk = [[built[i]]]; var start = i; i++; while (i < built.length && built[i]) { chunk.push([built[i]]); i++; } sheet.getRange(2 + start, imageCol, chunk.length, 1).setValues(chunk); set += chunk.length; } return set; }

Example

function fillProductThumbs() { var sheet = SpreadsheetApp.getActive().getSheetByName('Products'); var written = setCellImagesFromUrls(sheet, 3, 4); // C = URL, D = image Logger.log('Wrote ' + written + ' images'); }

Notes

  • urlCol and imageCol are 1-based column numbers. Row 1 is treated as a header and is not written.
  • URLs must be http:// or https:// and fetchable by Google (a signed-in-only Drive link will usually fail). Failures are skipped, not thrown, so one bad row does not stop the rest.
  • The write is a batch setValues per contiguous run of good URLs, so skipped cells in between are not cleared.
  • Overwrite happens only on rows that produce a new image. Re-running on the same good URLs rebuilds those cells.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free snippets — plus optional Co-Pilot when you want a boost (Day Pass — $3, or 1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension