Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phillypb/63a62ea8ddf82cf5a99cabeee067fa49 to your computer and use it in GitHub Desktop.
Save phillypb/63a62ea8ddf82cf5a99cabeee067fa49 to your computer and use it in GitHub Desktop.
function replaceTextToImage() {
// ******************** COMPLETE THESE ITEMS ********************
var searchText = "";
var imageURL = "";
var rowHeight = 100;
var columnWidth = 100;
// ******************** COMPLETE THESE ITEMS ********************
// get spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get Data sheet
var dataSheet = ss.getSheetByName('Data');
// build image
var image = SpreadsheetApp
.newCellImage()
.setSourceUrl(imageURL)
.build();
// create TextFinder process to get all Ranges that need changing
var textFinder = dataSheet.createTextFinder(searchText).findAll();
// loop through each TextFinder item *****************************
textFinder.forEach(function getCell(word) {
// use TextFinder to get Row & Column numbers
var row = word.getRow();
var col = word.getColumn();
// get relevant cell via Range
var cell = dataSheet.getRange(row, col);
// insert image
cell.setValue(image);
// OPTIONAL: change cell size
dataSheet.setRowHeight(row, rowHeight);
dataSheet.setColumnWidth(col, columnWidth);
// OPTIONAL: centre image
cell.setVerticalAlignment("middle");
cell.setHorizontalAlignment("center");
});
// loop through each TextFinder item *****************************
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment