Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all videos of a YouTube channel in Google Apps Script

I'm using YouTube Data API to list first 25 video titles and ids of a channel in google spreadsheet, but for some reason I'm only getting the Title of the channel.

Here are my codes:

function searchByChannel() {
    var results = YouTube.Channels.list('id,snippet', {
        id: 'UC-9-kyTW8ZkZNDHQJ6FgpwQ',
        maxResults: 25
    });

    var title = "";
    var id = "";
    var lr = 0;
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet1 = ss.getSheetByName("Sheet1");
    for (var i = 0; i < results.items.length; i++) {
        var item = results.items[i];
        title = item.snippet.title;
        id = item.id.videoId;
        lr = sheet1.getLastRow() + 1;
        sheet1.getRange(lr, 1).setValue(title);
        sheet1.getRange(lr, 2).setValue(id);
        lr++;
    }
}

I'm new to YouTube data api so I don't know what I'm doing wrong here. How can I list 25 video ids and titles in my spreadsheet using google apps script?

like image 945
Shihan Khan Avatar asked Nov 25 '25 08:11

Shihan Khan


1 Answers

The documentation for the YouTube.Channels states that it will return information about the Channel, nothing about the videos, that's why your getting the channel's info.

YouTube.Search on the other hand gather info about videos, and you can feed it a channelId parameter that will seek into a specified channel, as such, your result code should look like:

  var results = YouTube.Search.list('id,snippet', {
    channelId:'UC-9-kyTW8ZkZNDHQJ6FgpwQ',
    maxResults: 25
  });
like image 195
Kriggs Avatar answered Nov 28 '25 15:11

Kriggs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!