Skip to content

Instantly share code, notes, and snippets.

@cddouglass
Created September 24, 2023 01:53
Show Gist options
  • Save cddouglass/7e62e0bad9aa277692c507a4d84f9201 to your computer and use it in GitHub Desktop.
Save cddouglass/7e62e0bad9aa277692c507a4d84f9201 to your computer and use it in GitHub Desktop.
let settings = input.config({ title: "Validate emails", description: "This script will list all invalid emails for a field you pick.", items: [ input.config.table("table", { label: "Table" }), input.config.field("field", { parentTable: "table", label: "Email field", }), ],});
let { table, field } = settings;
// To validate a string of text we need to use a regular expression.// Regular expressions are complex — read more about them here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions// This regular expression checks to see if an email address is in the correct format.let regexp = /[A-z0-9.+-_]+@[A-z0-9-]+\.[A-z]+/g;
// Now let's grab all of the records from the table the user selected by calling the selectRecordsAsync method.// We only need the cell values in the selected field, so we'll specify that with the fields argument.let queryResult = await table.selectRecordsAsync({ fields: [field],});
// Before we move on, let's create an empty array that can hold onto the validation results that we'll need to display later.let results = [];
// With the records ready to check and our regular expression defined, we can use a for loop to loop through every// record and check if the email address is valid.for (let record of queryResult.records) { // Let's first grab the primary field for identification purposes and store it in a variable. let name = record.name; // Now we'll do the same for the contents of the field we want to validate. If the cell is empty, getCellValue returns null. let cellValue = record.getCellValue(field); if (typeof cellValue !== "string") continue;
let validation; if (cellValue) { // If the cell value exists, we'll use the match method to validate it against our regular expression. validation = cellValue.match(regexp); } else { // If the cell value does not exist, we'll treat it as invalid. validation = null; }
let resultText; if (validation === null) { let result = { Record: name, "Cell value": cellValue, }; // Push invalid results on our results array. results.push(result); }}
if (results.length === 0) { output.text( `No invalid emails found. (${queryResult.records.length} records validated)` );} else { output.text( `${results.length} invalid emails found. (${queryResult.records.length} records validated)` ); // Show all invalid emails output.table(results);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment