From ce2e01b99c95b6697cca25eb80361ff0fbc6ae4b Mon Sep 17 00:00:00 2001 From: a1ex-garofalo <141367682+a1ex-garofalo@users.noreply.github.com> Date: Thu, 3 Aug 2023 13:41:57 -0700 Subject: [PATCH] checkMatchingValues --- functions/checkMatchingValues | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 functions/checkMatchingValues diff --git a/functions/checkMatchingValues b/functions/checkMatchingValues new file mode 100644 index 0000000..9bd1942 --- /dev/null +++ b/functions/checkMatchingValues @@ -0,0 +1,22 @@ +// @a1ex.garofalo WorkspaceDevs https://developers.google.com/apps-script/guides/sheets/functions#optimization +/** + * Checks if values in one column on a certain sheet match values in another column in a different sheet + */ +function checkMatchingValues(sourceSheetName, sourceColumn, targetSheetName, targetColumn) { + var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sourceSheetName); + var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(targetSheetName); + + var sourceValues = sourceSheet.getRange(sourceColumn).getValues(); + var targetValues = targetSheet.getRange(targetColumn).getValues(); + + var sourceValuesFlat = sourceValues.flat().filter(String); + var targetValuesFlat = targetValues.flat().filter(String); + + for (var i = 0; i < sourceValuesFlat.length; i++) { + if (targetValuesFlat.indexOf(sourceValuesFlat[i]) === -1) { + return false; + } + } + + return true; +}