As datasets grow larger, it becomes increasingly difficult to identify common values and overlap between different columns of data. In such cases, using a Google Sheets script can save you a lot of time and effort. In this tutorial, we'll show you how to use a Google Sheets script to compare and find common values between two columns of data.
Step 1: Open a Google Sheet Document
First, open a Google Sheet document and create two columns of data that you want to compare. For example, you may have a list of customer names in column A and a list of leads in column B. The goal is to identify any overlap between the two columns.
Step 2: Create a Google Sheets Script
Click on "Tools" in the menu bar, and then click on "Script editor". Copy and paste the following script into the script editor:
function compareColumns() {
var sheet = SpreadsheetApp.getActiveSheet();
var valuesA = sheet.getRange("A1:A").getValues();
var valuesB = sheet.getRange("B1:B").getValues();
var valuesC = [];
// Loop through all values in column A
for (var i = 0; i < valuesA.length; i++) {
var valueA = valuesA[i][0];
// Loop through all values in column B
for (var j = 0; j < valuesB.length; j++) {
var valueB = valuesB[j][0];
// Check if the values in columns A and B match
if (valueA == valueB) {
valuesC.push([valueA]);
break;
}
}
}
// Write the common values to column C
sheet.getRange(1, 3, valuesC.length, 1).setValues(valuesC);
}