Check for existing value in column, Google Spreadsheet scripting

I recently took on the task of scripting out some spreadsheet validation in Google Drive. After looking into some scripts, reading through some API’s and testing various methods, I came up with a solution that I feel is not only simple and fairly easy to use, but is also flexible and reusable enough that it warrants sharing.

So here it is, a data validation script:

function onEdit( event )
{
// Create a boolean for found errors /mrg
var any_errors = 0 ;
// Create an array of columns we want to check, for each sheet we want to check /mrg
var computers_cols = [1, 2, 3, 4] ;
var monitors_cols = [1, 2, 3, 5] ;
var peripherals_cols = [1, 2, 3, 4] ;
var printers_cols = [1, 2, 3, 5] ;
var locations_cols = [1, 2] ;
// Create an empty array for holding the current sheet's allowed validation cols /mrg
var sheet_cols = [];
// Store the edited sheet.
var sheet_active = event .source .getActiveSheet() ;
var current_sheet = sheet_active.getName();
// Store the edited range.
var range_active = event .source .getActiveRange() ;
// Now set the appropriate columns for the given sheet /mrg
switch (current_sheet) {
case "Computers":
sheet_cols = computers_cols;
break;
case "Monitors":
sheet_cols = monitors_cols;
break;
case "Peripherals":
sheet_cols = peripherals_cols;
break;
case "Printers":
sheet_cols = printers_cols;
break;
case "Location":
sheet_cols = locations_cols;
break;
}
// Store the row, column and value of the edited cell, as well as the cell itsself /mrg
var column_edited = range_active .getColumn() ;
var value_edited = range_active .getValue() ;
var row_edited = range_active .getRow() ;
var cell = event.source.getActiveCell();
// Check if there is a value and if the column is one we want to enforce /mrg
if((!(value_edited == "")) && (column_edited in sheet_cols))
{
// Store a range consisting of the column containing the edited cell.
var range_column_edited = sheet_active .getRange(
1 , column_edited ,
sheet_active .getMaxRows() , 1
) ;
// Store an array consisting of the values in the column.
var values_column_edited = range_column_edited .getValues() ;
var i = 0 ;
// Compare each value to the edited cell.
for( var r = 0 ; r < values_column_edited .length ; r++ ) { i = r+1 ; //get the cell used for comparison /mrg var change_cell = range_column_edited.getCell(i, 1) ; //don't check the edited cell against itself /mrg if( r+1 == row_edited ) continue ; if( values_column_edited[r] == value_edited ) { any_errors = 1; //Hi-lite matched cell in yellow /mrg change_cell.setBackground("#ffee00") ; //Alert the user Browser .msgBox('You have entered duplicate values, please correct the following - ' + 'value entered : "' + value_edited + '"\r\n duplicates\r\n row ' + i + ' : "' + values_column_edited[r] + '"' ) ; } else { //No error, make sure the cell is white /mrg if(change_cell.getBackground() != "#ffffff") { change_cell.setBackground("#ffffff"); } } } //If there are any errors, set the BG color of the edited cell to yellow /mrg if(any_errors == 1) { cell.setBackground("#ff0000"); } else { cell.setBackground("#ffffff"); } } }