Skip to content

Instantly share code, notes, and snippets.

@Sea-n
Created July 22, 2018 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sea-n/093055162ea9bacd9c915ee417763f10 to your computer and use it in GitHub Desktop.
Save Sea-n/093055162ea9bacd9c915ee417763f10 to your computer and use it in GitHub Desktop.
Check Duplicates for Google Sheet
var checkSheet = 'Seat';
function onEdit(e) {
if (e.source.getSheetName() == checkSheet)
checkDuplicates();
}
function checkDuplicates() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
var numRows = dataRange.getNumRows();
var numColumns = dataRange.getNumColumns();
var formats = dataRange.getBackgrounds();
var counts = {};
var modified = false;
for (var i = 0; i < numRows; i++) {
for (var j = 0; j < numColumns; j++) {
if (formats[i][j] == '#ff0000') {
modified = true;
formats[i][j] = 'WHITE';
}
if (data[i][j] != '') {
counts[data[i][j]] = 1 + (counts[data[i][j]] || 0);
}
}
}
for (var i = 0; i < numRows; i++) {
for (var j = 0; j < numColumns; j++) {
if (data[i][j] != '' && counts[data[i][j]] > 1) {
modified = true;
formats[i][j] = 'RED';
}
}
}
if (modified) {
SpreadsheetApp.setActiveSheet(sheet.getSheetByName(checkSheet))
dataRange.setBackgroundColors(formats);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment