YouTube is one of the largest repositories of video content in the world. Whether you’re a researcher, content curator, teacher, or marketer, there are times when you want to automate YouTube searches, filter specific types of videos, and log the results for further use.
Fortunately, with the YouTube Data API and Google Apps Script, you can programmatically search YouTube and log search results directly into Google Sheets—without needing external tools, APIs, or manual browsing.
In this tutorial, we will cover how to:
Set up a Google Sheet to log YouTube search results
Enable and authorize the YouTube Data API
Write a Google Apps Script to perform YouTube searches
Store search results such as video title, ID, channel, publish date, and URLs
By the end of this article, you’ll have a working tool to search and analyze YouTube content using only Google tools.
Google Apps Script is a JavaScript-based platform that runs in the cloud and integrates directly with Google Workspace tools like Sheets, Docs, and Drive. When combined with the YouTube Data API, Apps Script becomes a lightweight and scalable way to search YouTube and process the data without building a standalone app.
Key advantages include:
No need to set up external servers or APIs
Native integration with Google Sheets
Easy access to authentication and quotas
Reusable scripts that can run on a schedule or on demand
To follow along, you’ll need:
A Google Account with access to YouTube
A Google Sheet to use as your dashboard
Access to Google Apps Script
Authorization to use the YouTube Data API
Create a new Google Sheet and name it something like YouTube Search Results.
Use the first row to label the columns as follows:
| Search Query | Video Title | Video ID | Channel Title | Published Date | Video URL |
|---|
This will help organize the search results and allow you to track multiple search terms.
From your Google Sheet, go to:
Extensions → Apps Script
This opens a new tab where you can write and manage your script. Rename the project to something meaningful like YouTube Search Tool.
To enable the API:
In the Apps Script editor, click on the left-hand Services icon.
Click + Add a service.
Search for YouTube Data API.
Click Add.
This adds the YouTube API client library to your project so you can call YouTube functions directly in your script.
Now add the following code to your Apps Script editor. This function searches YouTube using the search.list method and populates the Google Sheet with results.
function searchYouTubeAndLogResults() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var searchTerm = sheet.getRange("A2").getValue(); // Get query from cell A2
if (!searchTerm) {
Logger.log("Search term not found in A2");
return;
}
var results = YouTube.Search.list('snippet', {
q: searchTerm,
maxResults: 10,
type: 'video'
});
var items = results.items;
if (!items || items.length === 0) {
Logger.log("No results found.");
return;
}
// Clear old results (except header row)
sheet.getRange(3, 2, sheet.getLastRow(), 5).clearContent();
for (var i = 0; i < items.length; i++) {
var video = items[i];
var title = video.snippet.title;
var videoId = video.id.videoId;
var channelTitle = video.snippet.channelTitle;
var publishedAt = video.snippet.publishedAt;
var videoUrl = "https://www.youtube.com/watch?v=" + videoId;
// Log into sheet, starting from row 3
var row = i + 3;
sheet.getRange(row, 2).setValue(title);
sheet.getRange(row, 3).setValue(videoId);
sheet.getRange(row, 4).setValue(channelTitle);
sheet.getRange(row, 5).setValue(publishedAt);
sheet.getRange(row, 6).setValue(videoUrl);
}
}
This script reads the search query from cell A2, performs a YouTube search, and logs up to 10 video results starting from row 3 in the sheet.
To test the script:
Enter a search query into cell A2 (e.g., space documentaries).
In the Apps Script editor, select the function searchYouTubeAndLogResults.
Click the Run button.
The first time you run the script, it will ask for authorization. Grant the required permissions to allow the script to access your Google account and YouTube data.
After execution, the sheet should populate with video information based on the search query.
You can configure the script to run automatically every hour, day, or week using Google’s built-in triggers.
To do this:
Open the Apps Script editor.
Click the clock icon on the left (Triggers).
Click + Add Trigger.
Choose the function searchYouTubeAndLogResults.
Choose the desired time-based trigger (e.g., daily at 9:00 AM).
Save the trigger.
This automates the YouTube search and keeps your spreadsheet updated without manual effort.
There are several ways to expand or refine this tool:
Loop through multiple search terms in a column and create separate result blocks or sheets for each query.
Add optional filters like publishedAfter to only get recent videos.
publishedAfter: "2024-01-01T00:00:00Z"
Change maxResults to 25 or use pagination tokens (nextPageToken) for deeper search.
Change the type parameter from 'video' to 'channel' or 'playlist'.
Retrieve and display thumbnail URLs from video.snippet.thumbnails.default.url.
Authorization Error
Ensure you’ve added and enabled the YouTube Data API and completed the authorization steps.
No Results Found
Double-check the query spelling and ensure the content exists on YouTube.
Quota Limits
YouTube API has daily usage limits. Avoid frequent large queries unless necessary. You can monitor quota usage in the Google Cloud Console.
By combining Google Sheets, Google Apps Script, and the YouTube Data API, you can build a powerful search and analysis tool for YouTube content. This automation can help you streamline research, build content strategies, track competitor uploads, or monitor topic trends—right from a spreadsheet.
This approach is flexible, secure, and extendable. With a few enhancements, you can build a full-featured YouTube content aggregator tailored to your workflow.
Would you like a version of this tool that includes automatic playlist creation from search results? Let me know and I can help you build that next.