ReferenceError: YouTube is not defined / insert YouTube comments to a Spreadsheet
ReferenceError: YouTube is not defined / insert YouTube comments to a Spreadsheet
我正在尝试将 YouTube 评论插入电子表格。
我想从中获得评论的 YouTube 视频是我在同一个 google 帐户上创建的。
YouTube 数据 API v3 已启用。当我使用运行函数时,出现错误:
ReferenceError: YouTube is not defined (line 9, file "Code")
,
但我觉得还不错。我不知道第 9 行有什么问题。
完整代码如下:
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
}
}
}
}
错误消息如下所示:
修改点:
根据您的脚本和错误消息,我认为您的错误消息 YouTube is not defined
的原因是由于在高级 [=65= 中禁用了 YouTube 数据 API v3 ] 服务。关于这一点,您能否确认YouTube数据API v3是否已经在高级Google服务中再次启用? Ref 如果它从未启用过,请启用它并再次测试您的脚本。
而且,当我看到你的脚本时,我认为脚本的处理成本会很高。因为在循环中使用了setValue
。 Ref为了降低脚本的处理成本,我建议采用以下流程。
- 检索所有值并将它们放入数组。
- 将数组放到 Spreadsheet.
好像“Videos:list”方法的maxResults
是50
当以上几点反映到你的脚本中,就会变成下面这样。
修改后的脚本:
function getComments() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var video_list = ['YouTube_ID'];
var PageToken = '';
var values = []; // Added
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);
values.push(["name", "date", "comment"]); // Added
while (true) {
var comment_list = YouTube.CommentThreads.list('id, replies, snippet', {videoId: video_id, pageToken: PageToken, maxResults: 50});
for (var j = 0; j < comment_list.items.length; j++) {
var snippet = comment_list.items[j].snippet.topLevelComment.snippet;
values.push([snippet.authorDisplayName, snippet.publishedAt, snippet.textDisplay]); // Added
var replies = comment_list.items[j].replies;
if (replies) {
for (var k = 0; k < replies.comments.length; k++) {
var comments = replies.comments[k].snippet;
values.push([comments.authorDisplayName, comments.publishedAt, comments.textDisplay]); // Added
}
}
}
PageToken = comment_list.nextPageToken;
if (typeof PageToken == "undefined") {
break
}
}
sh.getRange(1, 1, values.length, values[0].length).setValues(values); // Added
}
}
注:
- 在此脚本中,当存在相同的 sheet 名称时,会发生错误。所以请注意这一点。
参考文献:
- Videos: list
google-apps-script
- 我认为这些链接可能会有用。
我正在尝试将 YouTube 评论插入电子表格。
我想从中获得评论的 YouTube 视频是我在同一个 google 帐户上创建的。 YouTube 数据 API v3 已启用。当我使用运行函数时,出现错误:
ReferenceError: YouTube is not defined (line 9, file "Code")
,
但我觉得还不错。我不知道第 9 行有什么问题。
完整代码如下:
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
}
}
}
}
错误消息如下所示:
修改点:
根据您的脚本和错误消息,我认为您的错误消息
YouTube is not defined
的原因是由于在高级 [=65= 中禁用了 YouTube 数据 API v3 ] 服务。关于这一点,您能否确认YouTube数据API v3是否已经在高级Google服务中再次启用? Ref 如果它从未启用过,请启用它并再次测试您的脚本。而且,当我看到你的脚本时,我认为脚本的处理成本会很高。因为在循环中使用了
setValue
。 Ref为了降低脚本的处理成本,我建议采用以下流程。- 检索所有值并将它们放入数组。
- 将数组放到 Spreadsheet.
好像“Videos:list”方法的
maxResults
是50
当以上几点反映到你的脚本中,就会变成下面这样。
修改后的脚本:
function getComments() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var video_list = ['YouTube_ID'];
var PageToken = '';
var values = []; // Added
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);
values.push(["name", "date", "comment"]); // Added
while (true) {
var comment_list = YouTube.CommentThreads.list('id, replies, snippet', {videoId: video_id, pageToken: PageToken, maxResults: 50});
for (var j = 0; j < comment_list.items.length; j++) {
var snippet = comment_list.items[j].snippet.topLevelComment.snippet;
values.push([snippet.authorDisplayName, snippet.publishedAt, snippet.textDisplay]); // Added
var replies = comment_list.items[j].replies;
if (replies) {
for (var k = 0; k < replies.comments.length; k++) {
var comments = replies.comments[k].snippet;
values.push([comments.authorDisplayName, comments.publishedAt, comments.textDisplay]); // Added
}
}
}
PageToken = comment_list.nextPageToken;
if (typeof PageToken == "undefined") {
break
}
}
sh.getRange(1, 1, values.length, values[0].length).setValues(values); // Added
}
}
注:
- 在此脚本中,当存在相同的 sheet 名称时,会发生错误。所以请注意这一点。
参考文献:
- Videos: list
google-apps-script
- 我认为这些链接可能会有用。