Skip to content

Function implementation in Google Sheets to massively shorten links.

Notifications You must be signed in to change notification settings

Lucassis7/bitly-shortener-for-google-sheets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Script Shortener Links on Google Sheets - API Bitly

The objective of this step by step is to use Google Sheets to shorten mass links through the API provided by Bitly, only needing to provide all the URLs that you want to shorten and using the Google Sheets "drag" functionality.

O objetivo deste passo a passo é utilizar o Google Sheets para encurtar links em massa através da API disponibilizada pelo Bitly, necessitando apenas prover todas as URLs que deseja-se encurtar e utilizando da funcionalidade do Google Sheets de "arrasto".

Step 01:

🇺🇸 English Create a sheet (or use a existent one) on Google Sheets;
🇧🇷 Português Criar uma planilha (ou planilha já criada) no Google Sheets;
📸 Image | Imagem
Nova Planilha | New Sheet

new-sheet

Step 02:

🇺🇸 English Search on nav bar "Extensions" > "Apps Script" and create a new project (e.g.: bitlyGenerator.gs);
🇧🇷 Português Na planilha busque no menu superior Extensões > Apps Script e crie um novo projeto (ex.: bitlyGenerator.gs);
📸 Image | Imagem
Acessando Apps Script | Opening Apps Script

extension-apps-script

Organizando o Ambiente de Trabalho | Organizing Workspace

new-app-script

Step 03:

🇺🇸 English Copy the code below and save it for use;
🇧🇷 Português Copiar o código disponibilizado no arquivo e salvá-lo para utilização;
📸 Image | Imagem
Criando a Função | Creating a Function

app-script-function

Step 04:

🇺🇸 English On the bitly website, with an active account, open the development tab (Settings > Developer Settings > API > Enter Password > Generate Token) and generate the API authentication token;
🇧🇷 Português No site bitly, com a conta ativa, abrir a aba development (Settings > Developer Settings > API > Enter Password > Generate Token) e gerar o token de autenticação da API;
📸 Image | Imagem
Acessando o Bitly | Accessing Bitly

bitly-account

Token API Bitly - Parte 1 | Token API Bitly - Part 1

bitly-token-api

Token API Bitly - Parte 2 | Token API Bitly - Part 2

bitly-token-password

Token API Bitly - Parte 3 | Token API Bitly - Part 3

bitly-token-generate

Token API Bitly - Parte 4 | Token API Bitly - Part 4

bitly-token

Step 05:

🇺🇸 English In the worksheet, use the function named in Apps Script in the cell through =getBitly(arg1;arg2), where the first argument refers to the URL to be shortened, and the second argument refers to the token accessed in Step 04;
🇧🇷 Português Na planilha utilizar a função nomeada no Apps Script na célula por meio de =getBitly(arg1;arg2), onde o primeiro argumento se refere a URL que se deseja encurtar, e o segundo argumento se refere ao token acessado no Passo 04;
📸 Image | Imagem
Aplicando a Função na Planilha | Applying the Function on the Sheet

bitly-token

Utilizando o "arrastar" | Using "drag" function

bitly-token

Resultado dos Sites encurtados | Result of shortened Sites

bitly-token

🇧🇷 Código | 🇺🇸 Script:

function  getBitly(URL, Token) {
	let  bitlyurl = "https://api-ssl.bitly.com/v4/shorten";
	let  bitlyarr = [];
	if (Array.isArray(URL)){
		for (let  i = 0; i < URL.length; i++) {
			if (URL[i] == "") {
				bitlyarr.push("Empty Cell");
				continue;
			}
			if(URL[i].toString().match(/^http[\ss]\:\/\//gi)){
				let  l_url = URL[i].toString();
			} else {
				l_url = "https://" + URL[i];
			}
			let  long_url = {
				long_url: l_url
			};
			let  options = {
				headers: {Authorization: 'Bearer '+ Token},
				muteHttpExceptions: true,
				contentType: 'application/json',
				method: 'POST',
				payload : JSON.stringify(long_url)
			};
			let  response = UrlFetchApp.fetch(bitlyurl, options);
			if (response.getResponseCode() == 200 || response.getResponseCode() == 201){
				let  response_Json = JSON.parse(response.getContentText());
				let  bitlyShort = response_Json.link;
				bitlyarr.push(bitlyShort);
			} else {
				bitlyarr.push(response.getContentText());
			}
		}
	} else {
		if (URL == "") {
			return"Empty Cell"
		}
		let  long_url = {
			long_url: URL
		};
		let  options = {
			headers: {Authorization: 'Bearer '+ Token},
			muteHttpExceptions: true,
			contentType: 'application/json',
			method: 'POST',
			payload : JSON.stringify(long_url)
		};
		let  response = UrlFetchApp.fetch(bitlyurl, options);
		if (response.getResponseCode() == 200 || response.getResponseCode() == 201){
			let  response_Json = JSON.parse(response.getContentText());
			let  bitlyShort = response_Json.link;
			bitlyarr.push(bitlyShort);
		} else {
			bitlyarr.push(response.getContentText());
		}
	}
	return  bitlyarr;
}

About

Function implementation in Google Sheets to massively shorten links.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published