Created
July 22, 2018 23:53
-
-
Save Sea-n/093055162ea9bacd9c915ee417763f10 to your computer and use it in GitHub Desktop.
Check Duplicates for Google Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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