Create a bulk URL shortener in Google Sheets using the Bitly API and Google Apps Script. Add long URLs in a column, then use a script to send each to Bitly’s API, retrieve the shortened version, and display it in another column—perfect for managing multiple links efficiently.
- Step 1: Click on the
Copy
button to copy the code snippet. - Step 2: Paste the copied code into your project’s script editor.
Apps Scripts Blog
Apps Scripts Code
function shortenUrls() { var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1"); // Replace "Sheet1" with the name of your sheet var dataRange = sheet.getDataRange(); var data = dataRange.getValues(); var urlColumn = 1; // Replace "1" with the column number of the URLs you want to shorten for (var i = 0; i < data.length; i++) { var url = data[i][urlColumn - 1]; var apiUrl = "https://api-ssl.bitly.com/v4/shorten"; var headers = { "Authorization": "Your API Key", "Content-Type": "application/json" }; var payload = { "long_url": url }; var options = { "method": "post", "headers": headers, "payload": JSON.stringify(payload) }; var response = UrlFetchApp.fetch(apiUrl, options); var json = JSON.parse(response.getContentText()); var shortenedUrl = json.link; sheet.getRange(i + 1, urlColumn + 1).setValue(shortenedUrl); // Replace "+ 1" with the column number where you want to put the shortened URLs } }