Skip to content

Instantly share code, notes, and snippets.

@elibroftw
Created April 23, 2023 22:20
Show Gist options
  • Save elibroftw/bd6acb3894f53288bf4759ddc2ec5a70 to your computer and use it in GitHub Desktop.
Save elibroftw/bd6acb3894f53288bf4759ddc2ec5a70 to your computer and use it in GitHub Desktop.
Google Sheets script to remove duplicate rows related to MAC, IP addresses, and Semantic Versioning
function isNewerVersion(oldVer, newVer) {
const oldParts = oldVer.split('.');
const newParts = newVer.split('.');
for (var i = 0; i < newParts.length; i++) {
const a = ~~newParts[i]; // parse int
const b = ~~oldParts[i]; // parse int
if (a > b) return 1;
if (a < b) return -1;
}
return 0
}
function removeDuplicates() {
const sheet = SpreadsheetApp.getActiveSheet()
const range = sheet.getDataRange();
const values = range.getValues().slice(1); // skip header
let newRows = {};
let ips = {};
// remove rows with no MAC or Version
values.forEach(row => {
let mac = row[0].toString();
let ip = row[1].toString();
// check if mac is a duplicate, use MD5 as well in case user is using old version of MC
// check for duplicate ip address
if (ip in ips) mac = ips[ip];
// hash mac if unhashed
else if (mac.includes(':')) mac = MD5(mac);
if (mac in newRows) {
// set row to latest version
if (isNewerVersion(row[2], newRows[mac][2])) {
// keep row with the latest version
newRows[mac] = row.slice(0, 5);
}
} else if (mac !== '') { // add to newRows if MAC is defined
// set row[0] to hashed mac
row[0] = mac;
newRows[mac] = row.slice(0, 5);
ips[ip] = mac;
}
});
newRows = Object.values(newRows);
newRows.sort((left, right) => isNewerVersion(left[2], right[2]));
// delete all rows (except header);
sheet.deleteRows(2, values.length); // index starts at 1
// add cleaned values
const newRange = sheet.getRange(2, 1, Object.keys(newRows).length, 5);
newRange.setValues(newRows);
// set font weight no normal since it might be bold
newRange.setFontWeight('normal');
}
function MD5(input) {
const rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
let txtHash = '';
for (i = 0; i < rawHash.length; i++) {
let hashVal = rawHash[i];
if (hashVal < 0) {
hashVal += 256;
}
if (hashVal.toString(16).length == 1) {
txtHash += '0';
}
txtHash += hashVal.toString(16);
}
return txtHash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment