Last active
August 17, 2020 04:44
-
-
Save wyhinton/0aecc135b646b7a7d145d75208beb8c9 to your computer and use it in GitHub Desktop.
A script for validating data values in a Google Spreadsheet. When retrieving a sheet as JSON, its best that the sheet contains no empty values. Thus this script highlights empty cells, and provides an error message with those cells locations.
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
function checkForEmptyCells() { | |
var ss=SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet=ss.getSheets()[1]; | |
var selection=sheet.getDataRange(); | |
var columns=selection.getNumColumns(); | |
var rows=selection.getNumRows(); | |
var emptyCells = []; | |
var headerCells = sheet.getRange(2, 1,1, columns) | |
var debugRange= sheet.getRange(1,1); | |
var dataCells = sheet.getRange(3, 2, rows-2, columns-1) | |
function debug(){ | |
debugRange.setValue('') | |
debugRange.setValue('DATA VALIDATION COMPLETE' + '\n') | |
var errorString = '' | |
if (emptyCells.length > 0){ | |
var emptyErrorMessage = debugRange.getValue() + '⚠ERROR: No Empty Cells Allowed, If Cell Empty Fill with NA: ' + emptyCells.join() | |
errorString = emptyErrorMessage | |
debugRange.setBackground('red') | |
} else { | |
errorString = debugRange.getValue() + '✓No Errors Found' | |
debugRange.setBackgroundRGB(191, 255, 181) | |
} | |
debugRange.setValue(errorString); | |
} | |
//Perform Function f on Cell in Range | |
function forEachRangeCell(range, f) { | |
const numRows = range.getNumRows(); | |
const numCols = range.getNumColumns(); | |
for (let i = 1; i <= numCols; i++) { | |
for (let j = 1; j <= numRows; j++) { | |
const cell = range.getCell(j, i) | |
f(cell) | |
} | |
} | |
} | |
//Check values of data Cells, change color correspondingly, push addresses of error cells to list | |
forEachRangeCell(dataCells, (cell)=>{ | |
var cellValue = cell.getValue(); | |
if (cellValue === 'NA'){ | |
cell.setBackground('yellow'); | |
} else if (cell.isBlank()) { | |
emptyCellLocation = cell.getA1Notation() | |
emptyCells.push(emptyCellLocation) | |
cell.setBackground("red") | |
} else { | |
cell.setBackgroundRGB(191, 255, 181) | |
} | |
}) | |
debug() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment