This script provides a custom function called GROUP_KEYWORD
specifically designed for Google Sheets. It allows you to group keywords based on their similarity to ad groups. The function utilizes the Levenshtein distance algorithm to calculate the difference between strings and assign each keyword to the ad group with the smallest distance.
Before using the GROUP_KEYWORD
function in Google Sheets, make sure you have the following:
- A Google Sheets document.
- Basic knowledge of working with Google Sheets and custom functions.
- Understanding of JavaScript programming language.
-
Open your Google Sheets document.
-
In the menu bar, click on "Extensions" and then select "Apps Script". This will open the Apps Script editor.
-
In the Apps Script editor, create a new script file or open an existing one.
-
Copy the code of the
GROUP_KEYWORD
andLEVENSHTEIN
functions into the script file. -
Save the script file by clicking on the floppy disk icon or pressing
Ctrl + S
. -
Close the Apps Script editor.
-
Now you can use the
GROUP_KEYWORD
function directly in your Google Sheets document. -
In a cell where you want the result to appear, enter the following formula:
=GROUP_KEYWORD(keyword, adGroups, allKeywords)
Replace
keyword
,adGroups
, andallKeywords
with the appropriate values or cell references.For example:
=GROUP_KEYWORD(A2, B2:B4, D2:D10)
A2
contains the keyword to be grouped.B2:B4
represents the range of ad groups.D2:D10
represents the range of all keywords.
-
Press Enter to execute the formula. The cell will display the name of the ad group that has the closest match to the keyword.
-
Adjust the
threshold
value in theGROUP_KEYWORD
function if necessary, based on your requirements.
-
The code provided is specifically designed for Google Sheets and uses the custom function feature provided by Google Apps Script.
-
Ensure that the
LEVENSHTEIN
function is available in the same script file as theGROUP_KEYWORD
function. TheLEVENSHTEIN
function calculates the Levenshtein distance between two strings and is used internally by theGROUP_KEYWORD
function. -
Make sure the input arrays (
adGroups
andallKeywords
) contain the correct data format. Each ad group or keyword should be represented as an array with the respective value at index 0. -
You can modify or extend the code according to your specific requirements. The provided implementation serves as a starting point and can be customized as needed.
-
Remember to handle any potential errors or exceptions that may occur during the execution of the code.
This script is provided under the MIT License. Feel free to modify, distribute, and use it in your Google Sheets documents.