ReferenceError: YouTube is not defined / insert YouTube comments to a Spreadsheet

Viewed 393

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:

enter image description here

1 Answers

Modification points:

  • From your script and error message, I thought that the reason of your error message of YouTube is not defined is due to disabling YouTube Data API v3 at Advanced Google services. About this, can you confirm whether YouTube Data API v3 has already been enabled at Advanced Google services again? Ref If it had never been enabled yet, please enable it and test your script again.

  • And, when I saw your script, I thought that the process cost of the script will be high. Because setValue is used in the loops. Ref In order to reduce the process cost of your script, I would like to suggest the following flow.

    1. Retrieve all values and put them to an array.
    2. Put the array to Spreadsheet.
  • It seems that maxResults of the method of "Videos: list" is 50.

When above points are reflected to your script, it becomes as follows.

Modified script:

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
  }
}

Note:

  • In this script, when the same sheet names are existing, an error occurs. So please be careful this.

References:

Related