|
| 1 | +// Note: Apps Script automatically requests authorization |
| 2 | +// based on the API's used in the code. |
| 3 | + |
| 4 | +function channelsListByUsername(part, params) { |
| 5 | + var response = YouTube.Channels.list(part, |
| 6 | + params); |
| 7 | + var channel = response.items[0]; |
| 8 | + var dataRow = [channel.id, channel.snippet.title, channel.statistics.viewCount]; |
| 9 | + SpreadsheetApp.getActiveSpreadsheet().appendRow(dataRow); |
| 10 | +} |
| 11 | + |
| 12 | +function getChannel() { |
| 13 | + var ui = SpreadsheetApp.getUi(); |
| 14 | + var channelName = ui.prompt("Enter the channel name: ").getResponseText(); |
| 15 | + channelsListByUsername('snippet,contentDetails,statistics', |
| 16 | + {'forUsername': channelName}); |
| 17 | +} |
| 18 | + |
| 19 | +function getGoogleDevelopersChannel() { |
| 20 | + channelsListByUsername('snippet,contentDetails,statistics', |
| 21 | + {'forUsername': 'GoogleDevelopers'}); |
| 22 | +} |
| 23 | + |
| 24 | +function onOpen() { |
| 25 | + var firstCell = SpreadsheetApp.getActiveSheet().getRange(1, 1).getValue(); |
| 26 | + if (firstCell != 'ID') { |
| 27 | + var headerRow = ["ID", "Title", "View count"]; |
| 28 | + SpreadsheetApp.getActiveSpreadsheet().appendRow(headerRow); |
| 29 | + } |
| 30 | + var ui = SpreadsheetApp.getUi(); |
| 31 | + ui.createMenu('YouTube Data') |
| 32 | + .addItem('Add channel data', 'getChannel') |
| 33 | + .addSeparator() |
| 34 | + .addItem('Add GoogleDevelopers data', 'getGoogleDevelopersChannel') |
| 35 | + .addToUi(); |
| 36 | +} |
0 commit comments