HomeGoogle DocsSpreadsheetHow to Use ENCODEURL in Google Sheets to Create Clean URLs

How to Use ENCODEURL in Google Sheets to Create Clean URLs

Published on

In Google Sheets, we can use the ENCODEURL function to encode text for use in the query string of a URL efficiently. This web category function helps ensure that special characters are properly formatted and can be safely included in a web address. Let’s first understand the query string.

A URL consists of several parts, including:

  • Protocol: The communication method (e.g., https://)
  • Domain name: The website’s address (e.g., example.com)
  • Path: The specific page or resource (e.g., /products/)
  • Query string: Optional data sent to the server (e.g., ?q=search+term)
  • Fragment identifier (or hash): Optional reference to a section within the page (e.g., #section1)

In the URL https://infoinspired.com/#search/q=xlookup%20function, we can break down the components as follows:

  • Protocol: https://
  • Domain name: infoinspired.com
  • Path: /
  • Fragment identifier: #search/q=xlookup%20function

The query string itself is part of the fragment identifier and consists of q=xlookup%20function.

The encoded text, such as the search term used in the query string, is xlookup%20function. We can use the ENCODEURL function in Google Sheets to dynamically generate this part of the URL within a Google Sheets formula.

Key points:

Query string vs. fragment identifier: The query string is processed by the web server, while the fragment identifier is handled by the browser.

ENCODEURL Function and Character Encoding

Syntax:

ENCODEURL(text)

Argument:

  • text: The text to be encoded (required).

When special characters are present in your query string, encoding them is necessary to safely include them in a URL. The ENCODEURL function handles the character encoding aspect in Google Sheets.

The encoding convention involves representing each special character with a % sign followed by two hexadecimal digits.

Common Encodings:

  • Space: %20
  • Forward slash (/): %2F
  • Ampersand (&): %26
  • Equal sign (=): %3D
  • Question mark (?): %3F
  • Plus sign (+): %2B
  • Other special characters have corresponding percent-encoded values.

Examples of ENCODEURL in Google Sheets

We can hardcode the text (part of the query string) within the formula or refer to a cell. Let’s start with a hardcoded example:

=ENCODEURL("North, South, and West") // Returns "North%2C%20South%2C%20and%20West"

You can enter this text, i.e., “North, South, and West,” in a cell, for example, in A1, and refer to that using the ENCODEURL formula as follows:

=ENCODEURL(A1)

If you want to encode multiple texts, use the ARRAYFORMULA function with the ENCODEURL formula:

=ArrayFormula(ENCODEURL(A1:A3))
ENCODEURL Function Basic Example in Google Sheets

How to Use the ENCODEURL Function in Real-Life Scenarios with Google Sheets

Let’s consider a scenario where you want to search this blog for various Google Sheets-related queries. Let’s see how to use Google Sheets to generate the URLs dynamically.

Steps:

  • Enter the following URL in cell B2: https://infoinspired.com/#search/
  • Then, enter the search terms one by one in cells C2:C, for example, “xlookup” in C2, “map lambda function” in cell C3, and so on.
  • Enter the following formula, where ENCODEURL is part of it, in cell D2 and drag it down.
=$B$2&"q="&ENCODEURL(C2)

Formula Breakdown:

  • $B$2: The reference of the cell containing the base URL. Dollar signs are used to lock both its row and column, ensuring it stays fixed when the formula is copied.
  • "q=": A literal string used to specify the “query” parameter in the URL’s query string.
  • ENCODEURL(C2): Encodes the text (search term) in cell C2, replacing special characters with their percent-encoded equivalents for URL compatibility.
  • & (Ampersands): Used to join or concatenate the different parts together into a single text string.

Alternatively, you can use the following array formula in cell D2, which does not require dragging it down.

=ArrayFormula($B$2&"q="&ENCODEURL(C2:C4))
ENCODEURL Function Real-Life Example in Google Sheets

These are hyperlinks. Click on them to open the URL in your browser and search this site. You can use the ENCODEURL function in similar scenarios in Google Sheets.

Remember:

Monitor for any necessary updates to the formula or base URL, as website URLs and search functions might evolve. Adapt the formula to match your specific blog’s query string structure.

Conclusion

From the above examples, you can understand two important points about the use of the ENCODEURL function in Google Sheets.

Remember that ENCODEURL is specifically for encoding text within the query string of URLs, not entire URLs. Please refer to the formula in cell D2 above.

It’s not always necessary to encode pre-formatted, static URLs (commonly used for permanent links, and navigation menus), such as https://infoinspired.com/category/google-docs/.

If you want to see one more real-life use of the ENCODEURL function, please check my guide on pre-filling Google Docs Forms from Google Sheets.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

More like this

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.