- Create a new Google Sheets file
- Go to Extensions → Apps Script
- Remove all the code and copy-paste the following instead
- Get a Clearbit API Key here → https://clearbit.com/blog/company-name-to-domain-api
- Copy and paste the API key into your Project at line 6. It should look like this now: const clearbit_api_key = "sk_eaoifjeaojfoieajof"
- Click on “run” and review permissions
- Save your project and go back to your sheet
- Use =getDomain(), like classic Google Sheets Formula
The new formula in Google Sheets:
The code to paste in Apps Script inside Google Sheets:
// Set-up
// 1. Replace `YOUR_API_KEY` with your Clearbit API Key.
const clearbit_api_key = "YOUR_API_KEY"
// 2. Save the Apps Script project
//
// 3. In your sheet, simply use the function =getDomain({companyName})
/**
* Get domain from companyName.
*
* @param {string} companyName.
* @return domainName.
* @customfunction
*/
function getDomain(companyName) {
var url = "https://company.clearbit.com/v1/domains/find?name=" + companyName;
var options = {
"muteHttpExceptions": true,
"headers": {
"authorization": "Bearer "+clearbit_api_key
}
}
var response = UrlFetchApp.fetch(url, options);
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
if (responseCode === 200) {
return JSON.parse(response).domain;
} else {
Logger.log(Utilities.formatString("Request failed for %s, got %d: %s", name, responseCode, responseBody));
return false;
}
}