Get YouTube Subscriber Count from YouTube Channel Url

Use this code to turn youtube urls with handles into subscriber counts. Make sure before you use the code that you do the steps including Activate YouTube API in services, add GCP number, create oAuth Consent Screen, Enable YouTube API, and Create your own API key.

Code.gs

// ✅ Activate YouTube API
// ✅ Add Google Cloud Project
// ✅ Enable YouTube API
// ✅ Create API Key
APIKEY = "ENTERYOURAPIKEYHERE"
/** @customfunction */
function GET_SUBS(channelUrl) {
  const handle = channelUrl.match(/youtube\.com\/@([\w.-]+)/)
  const handleUrl = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=channel&q=" + handle + "&key=" + APIKEY
  const handleResponse = UrlFetchApp.fetch(handleUrl)
  const channelIdData = JSON.parse(handleResponse.getContentText())
  const channelId = channelIdData.items[0].snippet.channelId
  const url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" + channelId + "&key=" + APIKEY
  const response = UrlFetchApp.fetch(url)
  const data = JSON.parse(response.getContentText())
  return data.items[0].statistics.subscriberCount
}