Certainly! Here is a Google Apps Script that will check each cell in column A for any form of the word "dispatch," "license," "fmcsa," or any U.S. state name or abbreviation. It will then mark the adjacent cell in column B with the matching keyword(s).
function markKeywords() {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
// Get data from column A
var dataRange = sheet.getRange(1, 1, lastRow);
var data = dataRange.getValues();
// Prepare regex patterns
var dispatchPattern = /\bdispatch\w*/i;
var licensePattern = /license/i;
var fmcsaPattern = /fmcsa/i;
// State names and abbreviations
var stateNames = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois',
'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland',
'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana',
'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania',
'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah',
'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
var stateAbbreviations = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA',
'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS',
'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA',
'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'];
// Lowercase state names for case-insensitive matching
var lowerStateNames = stateNames.map(function(name) { return name.toLowerCase(); });
// Build regex patterns for state names and abbreviations
var stateNamesPattern = new RegExp('\\b(' + lowerStateNames.join('|') + ')\\b', 'i');
var stateAbbrevsPattern = new RegExp('\\b(' + stateAbbreviations.join('|') + ')\\b', 'i');
// Prepare output array for column B
var output = [];
// Loop through each row in column A
for (var i = 0; i < data.length; i++) {
var cellValue = data[i][0]; // Value from column A
if (!cellValue) {
output.push(['']); // If cell is empty, leave column B blank
continue;
}
var text = cellValue.toString();
var lowerText = text.toLowerCase();
var matches = [];
// Check for "dispatch" variants
if (dispatchPattern.test(text)) {
matches.push('dispatch');
}
// Check for "license"
if (licensePattern.test(text)) {
matches.push('license');
}
// Check for "fmcsa"
if (fmcsaPattern.test(text)) {
matches.push('fmcsa');
}
// Check for state names
if (stateNamesPattern.test(lowerText)) {
var stateNameMatch = lowerText.match(stateNamesPattern);
matches.push(stateNameMatch[0]);
}
// Check for state abbreviations
if (stateAbbrevsPattern.test(text)) {
var stateAbbrevMatch = text.match(stateAbbrevsPattern);
matches.push(stateAbbrevMatch[0]);
}
// Write matches to output
if (matches.length > 0) {
output.push([matches.join(', ')]);
} else {
output.push(['']); // No matches found
}
}
// Write output to column B
var outputRange = sheet.getRange(1, 2, output.length, 1); // Column B
outputRange.setValues(output);
}
How to use the script:
- Open your Google Sheets document.
- Click on
Extensions in the top menu, then select Apps Script. This will open the Apps Script editor.
- Copy and paste the script above into the editor, replacing any existing code.
- Save the script by clicking on the floppy disk icon or pressing
Ctrl+S (Windows) or Cmd+S (Mac).
- Return to your spreadsheet. You may need to refresh the page.
- To run the script, you can either:
- Click on
Extensions > Apps Script > markKeywords, or
- Assign the function to a button or menu item in your sheet.
Note: The script assumes that your data starts in row 1 of column A and that you want the results in column B. If your data starts on a different row or you have headers, you'll need to adjust the getRange functions accordingly.
This script will check each cell in column A for:
- Any word starting with "dispatch" (e.g., "dispatch," "dispatcher," "dispatching")
- The word "license"
- The word "fmcsa"
- Any U.S. state name (case-insensitive)
- Any U.S. state abbreviation (case-insensitive)
If a cell contains any of these keywords, it will list them in the adjacent cell in column B, separated by commas.