首页 > 解决方案 > 如何从 Google 表格上的频道 ID 获取 Youtube 频道信息?

问题描述

我想从 Google 表格上的频道 ID 收集 Youtube 频道信息。

目前我正在使用下面的代码按频道名称收集频道信息......效果很好。

我现在想要的是添加另一个选项来通过输入频道 ID 而不是频道名称来收集相同的信息。

知道我必须在这里更改/添加什么:

代码示例:

// Note: Apps Script automatically requests authorization
// based on the API's used in the code.

function channelsListByUsername(part, params) {
var response = YouTube.Channels.list(part,
                                   params);
var channel = response.items[0];
var dataRow = [channel.snippet.title, channel.id, 
channel.statistics.viewCount, channel.statistics.subscriberCount,];
SpreadsheetApp.getActiveSpreadsheet().appendRow(dataRow);
}


function getChannel() {
var ui = SpreadsheetApp.getUi();
var channelName = ui.prompt("Enter the channel name: ").getResponseText();
channelsListByUsername('snippet,contentDetails,statistics',
                     {'forUsername': channelName});
}


 function onOpen() {
 var firstCell = SpreadsheetApp.getActiveSheet().getRange(1, 1).getValue();
 if (firstCell != 'Title') {
 var headerRow = ["Title", "ID", "View counts"];
 SpreadsheetApp.getActiveSpreadsheet().appendRow(headerRow);
 }
 var ui = SpreadsheetApp.getUi();
 ui.createMenu('YouTube Data')
 .addItem('Add channel name', 'getChannel')
 .addToUi();
 }

标签: youtubeyoutube-apiyoutube-data-apigoogle-spreadsheet-apiyoutube-javascript-api

解决方案


推荐阅读