Skip to content

Instantly share code, notes, and snippets.

@ryanmoralesaz
Created November 3, 2025 18:16
Show Gist options
  • Select an option

  • Save ryanmoralesaz/a1ca5609e7e9d7c820a67ee4f309ea72 to your computer and use it in GitHub Desktop.

Select an option

Save ryanmoralesaz/a1ca5609e7e9d7c820a67ee4f309ea72 to your computer and use it in GitHub Desktop.
Cross highilight google apps script
/**
* OPTIMIZED Cross-Highlighting - No Lag Version
* Only highlights cells within your actual data range
*/
function onSelectionChange(e) {
var ss = e.source;
var sheet = ss.getActiveSheet();
var activeCell = ss.getActiveRange();
// Only work with single cell selections
if (activeCell.getNumRows() > 1 || activeCell.getNumColumns() > 1) {
return;
}
// Get the actual data range instead of max rows/columns
var dataRange = sheet.getDataRange();
var lastRow = dataRange.getLastRow();
var lastCol = dataRange.getLastColumn();
// Add some padding (optional - adds 5 extra rows/cols beyond data)
lastRow = Math.min(lastRow + 5, sheet.getMaxRows());
lastCol = Math.min(lastCol + 5, sheet.getMaxColumns());
// Clear previous highlights from the data range only
sheet.getRange(1, 1, lastRow, lastCol).setBackground(null);
var activeRow = activeCell.getRow();
var activeCol = activeCell.getColumn();
// Highlight row (only up to last column with data)
if (activeRow <= lastRow) {
sheet.getRange(activeRow, 1, 1, lastCol).setBackground("lightyellow");
}
// Highlight column (only up to last row with data)
if (activeCol <= lastCol) {
sheet.getRange(1, activeCol, lastRow, 1).setBackground("lightyellow");
}
// Highlight the active cell
activeCell.setBackground("yellow");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment