How to Shorten URLs in Google Sheets
By Urlzee Team · Published · 7 min read
Google Sheets has no built-in formula for shortening URLs, so you have two options. For a one-off list, copy the column into a bulk URL shortener and paste the results back. For a sheet you update regularly, add a short Apps Script that sends selected cells to a shortener API and writes the short links alongside.
Which method should you use?
Pick by how often you will repeat the job.
| Copy and paste into a bulk tool | Apps Script with the API | |
|---|---|---|
| Setup | None | About ten minutes, once |
| Code | None | One script, provided below |
| Best for | A single campaign or catalogue | Sheets that keep growing |
| Custom aliases | Yes, as a second column | Possible with a small change |
| Limits | Batch size by plan | API requests by plan |
Both create ordinary Urlzee links that appear in your dashboard with click counts.
How do you shorten a column without any code?
Use the bulk URL shortener as a round trip.
- Put your long URLs in one column, one per row. If you want custom endings, put the alias in the column immediately to the right.
- Select the cells and copy them.
- Sign in to Urlzee, open the bulk tool and paste. Cells copied from Sheets arrive with a tab between columns, which the tool reads as URL then alias.
- Run the batch, then download the results as CSV or copy them.
- Paste the short links into a new column next to the originals.
Each row is checked individually, so a bad URL is reported without stopping the others. A free account handles 10 URLs per batch and Pro handles 500. Tips for preparing the list are in the bulk URL shortening guide.
How do you set up Apps Script to shorten URLs?
Apps Script is the JavaScript environment built into Google Sheets. The script below adds a Urlzee menu to your spreadsheet. Select a column of URLs, choose the menu item, and each short link is written into the cell to the right.
Step 1: get an API key
Create a free Urlzee account, open Dashboard → API keys and create a key. It is displayed once, so copy it.
Step 2: store the key in Script properties
- In your spreadsheet, choose Extensions → Apps Script.
- In the editor's left sidebar, open Project Settings (the gear icon).
- Under Script properties, add a property named
URLZEE_API_KEYwith your key as the value, and save.
This keeps the key out of the code. It is not a vault, though: anyone with edit access to the spreadsheet can open the script project and read its properties. Do not add the key to a sheet you share widely. See API key security best practices.
Step 3: add the script
Go back to the Editor, delete the placeholder function in Code.gs and paste this:
const API_URL = 'https://urlzee.com/api/v1/links';
const PAUSE_MS = 6500; // Free plan: 10 requests per minute. Use 1100 on Pro.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Urlzee')
.addItem('Shorten selected URLs', 'shortenSelection')
.addToUi();
}
function shortenSelection() {
const key = PropertiesService.getScriptProperties().getProperty('URLZEE_API_KEY');
if (!key) {
throw new Error('Add URLZEE_API_KEY under Project Settings > Script properties.');
}
const range = SpreadsheetApp.getActiveRange();
if (range.getNumColumns() !== 1) {
throw new Error('Select a single column of URLs.');
}
const sheet = range.getSheet();
const values = range.getValues();
const outputColumn = range.getColumn() + 1;
for (let i = 0; i < values.length; i++) {
const url = String(values[i][0]).trim();
const cell = sheet.getRange(range.getRow() + i, outputColumn);
if (!url || cell.getValue() !== '') continue; // skip blanks and rows already done
const res = UrlFetchApp.fetch(API_URL, {
method: 'post',
contentType: 'application/json',
headers: { Authorization: 'Bearer ' + key },
payload: JSON.stringify({ url: url }),
muteHttpExceptions: true,
});
const status = res.getResponseCode();
let message = 'HTTP ' + status;
let shortUrl = '';
try {
const body = JSON.parse(res.getContentText());
if (body.data) shortUrl = body.data.short_url;
if (body.error) message = body.error.message;
} catch (e) {
// Response was not JSON; keep the status as the message.
}
if (status === 201) {
cell.setValue(shortUrl);
} else if (status === 429) {
cell.setValue('Stopped: ' + message);
break;
} else {
cell.setValue('Error: ' + message);
}
SpreadsheetApp.flush();
Utilities.sleep(PAUSE_MS);
}
}
Save the project.
Step 4: run it
- Reload the spreadsheet. A Urlzee menu appears after a moment.
- Make sure the column to the right of your URLs is empty, because that is where results go.
- Select the URL cells and choose Urlzee → Shorten selected URLs.
- The first time, Google asks you to authorise the script. It needs permission to edit the spreadsheet and to connect to an external service.
- Short links fill in row by row.
How does the script work?
It makes one API request per row and records the outcome.
UrlFetchApp.fetchsends aPOSTtohttps://urlzee.com/api/v1/linkswith the body{"url": "..."}and the headerAuthorization: Bearer uz_live_..., which is what the API documentation specifies.muteHttpExceptions: truestops Apps Script from throwing on error statuses, so the script can read the API's own error message and write it to the sheet.- A
201status means the link was created. The short URL is atdata.short_urlin the response. - Other statuses carry an
errorobject with amessage. A422means the URL was invalid or the destination is not allowed, and the message says which. - A
429means you have reached the per-minute rate or the monthly quota. The script writes the reason and stops instead of hammering the API. Utilities.sleepspaces the calls out. Free accounts may make 10 API requests per minute, so the default pause is 6.5 seconds. On Pro, which allows 60 per minute, changePAUSE_MSto1100.
Rows that already have something in the output column are skipped. That makes the script safe to run again on the same selection: it only fills the gaps.
How do you add custom aliases?
Read a second column and include it in the request. Assume aliases are in the column to the right of the URLs, and results should go one column further along. Select only the URL column as before, and make three edits inside shortenSelection.
First, move the output one column further right by replacing the outputColumn line:
const outputColumn = range.getColumn() + 2;
Second, read the alias by adding this line directly after the const url = ... line:
const alias = String(sheet.getRange(range.getRow() + i, range.getColumn() + 1).getValue()).trim();
Third, send it by replacing the payload line:
payload: JSON.stringify(alias ? { url: url, alias: alias } : { url: url }),
An alias must be 3 to 50 letters, numbers, hyphens or underscores. If it is already in use, the API answers 409 and the script writes "Error: That alias is already taken. Try another." in that row.
Why not a custom function like =SHORTEN(A2)?
Because a formula is recalculated, and shortening a URL is an action with side effects. Sheets decides when to re-run custom functions. Every run would send another API request and create another link for the same URL, using up your allowance and scattering duplicate links through your dashboard. Custom functions also have a short execution time limit, which a slow network call can exceed.
A menu action runs once, when you ask, and writes plain text. The short links stay put, and the sheet can be copied or exported without depending on the script.
What limits should you plan around?
Two sets apply: Urlzee's and Google's.
Urlzee. A free account includes 50 new links and 100 API requests per month. Pro raises that to 5,000 links and 10,000 requests, and Business to 50,000 and 100,000. Each row you shorten uses one request and one link. Details are on the pricing page.
Apps Script. A single script run is stopped after about six minutes. With the default 6.5 second pause, that is roughly 50 rows per run, which happens to match the free plan's monthly link allowance. For longer lists on a paid plan, lower PAUSE_MS or run the menu item again on the remaining rows; finished rows are skipped. Google also sets daily quotas on UrlFetchApp calls, which are published in the Apps Script documentation and are far above what this script uses.
What problems come up?
- The menu does not appear. Reload the spreadsheet.
onOpenonly runs when the file is opened. - "Add URLZEE_API_KEY…" The property name is misspelled or was not saved.
- "Error: Invalid or revoked API key." The key was copied incompletely or has been revoked. Create a new one.
- Errors on URLs that work in a browser. Urlzee does not shorten links to other shorteners, raw IP addresses or private hosts. Check the message.
- Results overwrote data. The script only writes to empty cells in the output column, but make sure that column is the one you expect before a large run.
Once the links are in the sheet, the same spreadsheet is a good place to keep the campaign names and channels beside them. How to build a campaign URL tracking spreadsheet shows a layout that works well with this script.
Frequently asked questions
- Does Google Sheets have a built-in function to shorten URLs?
- No. Sheets has no native URL-shortening formula. You either shorten the links in a separate tool and paste the results back, or add a small Apps Script that calls a URL shortener API.
- Can I use a custom function like =SHORTEN(A2) in Google Sheets?
- You can write one, but it is a poor fit. Sheets decides when custom functions recalculate, and each recalculation would call the API again and create another link. A menu action that writes plain values into cells is more predictable.
- Where do I store an API key in Google Apps Script?
- In Script properties, found under Project Settings in the Apps Script editor, and read with PropertiesService. Avoid pasting the key into the code itself, and remember that anyone with edit access to the spreadsheet can open the script project.
- How many links can I shorten from a spreadsheet on the free plan?
- A free Urlzee account allows 50 new links and 100 API requests per month, with at most 10 API requests per minute. The bulk tool accepts 10 URLs per batch on the free plan and 500 on Pro.