For programmatic SEO, sometimes, you might need to create slugs right inside Google Sheets, and here’s how to do that. I will be using a Google Sheets formula that slugifies any text that you provide.
First, you can have a look at this quick video to understand how exactly this works.
Now, let’s take a look at how the formula shown in the video works.
Google Sheets Formula to Convert Text into Slugs
Below is the exact Google Sheets formula that converts any provided text to slugs:
=LOWER(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(TRIM(A2),"[^a-zA-Z0-9]+","-"), "-{2,}", "-"), "^-+|-+$", ""))
The best thing about the formula is that it doesn’t just replace the spaces with dashes but can also handle special characters like *, $, @, #, etc. And of course, you will have to replace A2 with the cell address of where your text is present.
The formula does the following things:
- Converts uppercase letters to lowercase
- Replaces spaces with dashes
- Removes any special characters
- Handles single and double quotes
And now, if you’re interested in learning more about how exactly the formula works, here is a detailed explanation of that:
- TRIM(A2): This function removes any leading and trailing white spaces from the text in cell A2.
- REGEXREPLACE(TRIM(A2),”[^a-zA-Z0-9]+”,”-“): The REGEXREPLACE function uses regular expressions to replace certain parts of a text. In this case, it replaces any sequence of characters that are not alphanumeric (any character that is not a letter from A-Z in either upper or lower case or a number from 0-9) with a hyphen (-).
- REGEXREPLACE(…, “-{2,}”, “-“): The formula then takes the result of the previous function and performs another REGEXREPLACE. This time, it replaces all instances where there are two or more hyphens in a row (“-“) with a single hyphen.
- REGEXREPLACE(…, “^-+|-+$”, “”): The final REGEXREPLACE function removes any hyphens that are at the start or the end of the text. “^-+” matches one or more hyphens at the start of a string, and “-+$” matches one or more hyphens at the end of a string.
- =LOWER(…): This function converts all letters in the string to lowercase. The result is what’s called a “slug”, a user- and SEO-friendly short label used in URLs.
In short, the formula trims extra spaces replaces all non-alphanumeric characters with hyphens, replaces multiple hyphens with a single one, removes hyphens at the start or end of the string, and changes all characters to lowercase to create a clean URL slug.
For pSEO, while WordPress or most other platforms can directly convert text to slugs, it’s a good practice to do that in your dataset itself. Because it helps you while setting up internal linkings.
That’s it.
If you need any additional help regarding this, kindly feel free to let me know in the comments below.
Leave a Reply