I am trying to insert YouTube comments into a spreadsheet.
The YouTube video that I want to get comments from is created by me, on the same google account. YouTube Data API v3 is enabled. When I run the function, an error occurs:
ReferenceError: YouTube is not defined (line 9, file "Code"),
but it seems fine for me. I have no idea what's wrong on line 9.
Here is the entire code:
function getComments() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var video_list = ['YouTube_ID'];
var PageToken = '';
for (var i = 0; i < video_list.length; i++) {
var video_id = video_list[i];
var video = YouTube.Videos.list('id, snippet, statistics', {id: video_id,});
if (i == 0) {
var sh = ss.getActiveSheet();
} else {
var sh = ss.insertSheet();
}
sh.setName(video.items[0].snippet.title);
var row = 2;
var col = 1;
sh.getRange(1, 1).setValue("name");
sh.getRange(1, 2).setValue("date");
sh.getRange(1, 3).setValue("comment");
while (true) {
var comment_list = YouTube.CommentThreads.list('id, replies, snippet', {
videoId: video_id,
pageToken: PageToken,
maxResults: 500,
});
for (var j = 0; j < comment_list.items.length; j++) {
sh.getRange(row, col).setValue(comment_list.items[j].snippet.topLevelComment.snippet.authorDisplayName);
sh.getRange(row, col + 1).setValue(comment_list.items[j].snippet.topLevelComment.snippet.publishedAt);
sh.getRange(row, col + 2).setValue(comment_list.items[j].snippet.topLevelComment.snippet.textDisplay);
row += 1;
if (typeof comment_list.items[j].replies !== "undefined") {
for (var k = 0; k < comment_list.items[j].replies.comments.length; k++) {
sh.getRange(row, col).setValue(comment_list.items[j].replies.comments[k].snippet.authorDisplayName);
sh.getRange(row, col + 1).setValue(comment_list.items[j].replies.comments[k].snippet.publishedAt);
sh.getRange(row, col + 2).setValue(comment_list.items[j].replies.comments[k].snippet.textDisplay);
row += 1;
}
}
}
PageToken = comment_list.nextPageToken
if (typeof PageToken == "undefined") {
break
}
}
}
}
The error message looks like:
