Translate

Create YouTube Playlist with Google Script and Sheets


Creating and managing YouTube playlists is often a manual task—searching for videos, adding them one-by-one, and organizing them through the YouTube interface. However, with the power of Google Apps Script and Google Sheets, you can fully automate this process. Whether you're curating educational content, generating playlists for marketing campaigns, or managing personal video collections, automating playlist creation can save significant time and effort.

In this guide, we will walk through how to use Google Sheets as a control panel for playlist creation, and Google Apps Script to interface with the YouTube Data API. By the end, you’ll have a spreadsheet-powered tool that allows you to dynamically create and populate YouTube playlists with just a few clicks.


What You’ll Learn

  • How to set up a Google Sheet for playlist management

  • How to enable and authenticate the YouTube Data API

  • How to write a Google Apps Script to create a YouTube playlist

  • How to add videos to that playlist using their YouTube URLs or IDs


Prerequisites

Before we start writing code, make sure you have the following:

  1. A Google Account with an active YouTube channel

  2. A Google Sheet to work from

  3. Access to Google Apps Script

  4. Authorization to enable and use the YouTube Data API


Step 1: Prepare Your Google Sheet

Start by creating a new Google Sheet and label it something like YouTube Playlist Manager.

Set up your spreadsheet with the following columns:

Video Title YouTube URL or ID
Intro to AI https://www.youtube.com/watch?v=abc123xyz
Machine Learning Basics https://www.youtube.com/watch?v=xyz456abc

You can add as many rows as needed. These URLs or IDs will be used by the script to populate your playlist.


Step 2: Open the Script Editor

Go to the menu in your Google Sheet:

Extensions → Apps Script

This opens a new Google Apps Script project associated with the spreadsheet.


Step 3: Enable the YouTube Data API

Before you can make YouTube API calls, you must enable the API in your Apps Script project.

  1. In the Apps Script editor, click on the left menu item: Services

  2. Click the + Add a service button

  3. Scroll down to YouTube Data API, and click Add

Now your script can access the YouTube API directly.


Step 4: Write the Script to Create a Playlist

In the script editor, paste the following code:

function createPlaylist() {
  var playlistTitle = "My Auto Playlist"; // Change as needed
  var playlistDescription = "This playlist was created using Google Sheets and Apps Script.";

  var resource = {
    snippet: {
      title: playlistTitle,
      description: playlistDescription
    },
    status: {
      privacyStatus: "private" // Can be "public" or "unlisted"
    }
  };

  var response = YouTube.Playlists.insert(resource, "snippet,status");
  Logger.log("Created playlist with ID: " + response.id);
  return response.id;
}

Save the script. This function creates a new playlist on your YouTube channel.


Step 5: Add Videos to the Playlist

Now, create a function to pull video IDs from the spreadsheet and add them to the playlist.

function addVideosToPlaylist() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var playlistId = createPlaylist(); // Or use a static playlist ID if desired

  for (var i = 1; i < data.length; i++) {
    var videoUrl = data[i][1];
    var videoId = extractVideoId(videoUrl);
    
    if (videoId) {
      var resource = {
        snippet: {
          playlistId: playlistId,
          resourceId: {
            kind: "youtube#video",
            videoId: videoId
          }
        }
      };
      
      try {
        YouTube.PlaylistItems.insert(resource, "snippet");
        Logger.log("Added video ID: " + videoId);
      } catch (e) {
        Logger.log("Error adding video ID " + videoId + ": " + e.message);
      }
    }
  }
}

function extractVideoId(url) {
  var idMatch = url.match(/(?:v=|\/)([0-9A-Za-z_-]{11})/);
  return idMatch ? idMatch[1] : null;
}

This function reads from your sheet, extracts the video ID, and adds each video to the newly created playlist.


Step 6: Run and Authorize the Script

To run the script:

  1. Go to the dropdown menu in the Apps Script editor

  2. Choose addVideosToPlaylist

  3. Click the run button

The first time you run it, you will be asked to authorize the script. Google will warn you that the script is unverified. Proceed only if you trust your own script.

Click through to allow the necessary permissions. This is required to let the script access your YouTube account and modify playlists.


Step 7: Check the Playlist

Once the script finishes, open YouTube and go to Your Channel → Playlists.

You should see the new playlist you created. Click it to view the list of videos added from your spreadsheet.


Tips and Customizations

  1. Dynamic Playlist Titles: Pull the title from a cell in your spreadsheet rather than hardcoding it.

  2. Add to Existing Playlist: Replace createPlaylist() with your static playlist ID if you don’t want to create a new one every time.

  3. Error Handling: Improve error logging by writing error messages back into your spreadsheet next to the failed entries.

  4. Rate Limits: Be aware that YouTube API enforces quota usage. Adding many videos in a short time may hit daily limits.

  5. Public Playlists: Change privacyStatus from "private" to "public" or "unlisted" as needed.


Common Errors and How to Fix Them

Error: Access Not Configured

  • You forgot to enable the YouTube Data API in the Apps Script services tab.

Error: Script is Unverified

  • You need to authorize the script and accept the permissions it requires.

Video Not Added

  • The video URL may be invalid or the video might be restricted (e.g., private or blocked in your country).


Conclusion

Automating YouTube playlist creation using Google Sheets and Apps Script is a powerful way to manage video content at scale. With just a spreadsheet and a few lines of code, you can dynamically curate playlists, streamline collaboration, and integrate YouTube into broader content management workflows.

This method is especially useful for educators, marketers, content curators, and developers managing multiple video assets. By combining the flexibility of Google Sheets with the capabilities of the YouTube Data API, you gain full control over how and when videos are added to playlists.

With a bit of customization, this setup can be expanded into a full YouTube content management system.

Would you like a downloadable template sheet and script that you can copy and deploy? Let me know and I can generate it for you.