如何使用Google脚本和表格创建YouTube播放列表

问题描述

我如何使这个Google表格脚本起作用以更新现有的YouTube播放列表。

function addVideoToYouTubePlaylist() {
  // Read the source videos from Google Sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDatarange().getValues();

  // Add your own playlist Id here
  var playlistId = "PLAYLIST_ID_HERE";

  // iterate through all rows in the sheet
  for (var d=1,l=data.length; d

解决方法

解决方案:

这是您要寻找的:

function updateYTPlaylist() {
  
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  const yt_video_ids = sh.getRange('A2:A'+sh.getLastRow()).getValues().flat([1]);
  
  const playlistId = "PLAYLIST_ID_HERE";
  
  yt_video_ids.forEach( vid => 
                       
     YouTube.PlaylistItems.insert({
      snippet: {
        playlistId: playlistId,resourceId: {
          kind: "youtube#video",videoId: vid
        }
      }
    },"snippet"));

   Utilities.sleep(2000);                                                             
}

说明:

我假设您在工作表中有一个名称为 Sheet1 的youtube视频ID列表,范围为 A2:A

explanation

上述代码将遍历此列表yt_video_ids,并将每个视频ID添加到由playlistId定义的所选播放列表中。

资源:

在Google脚本编辑器中,您需要点击资源 => 高级Google服务,然后启用 YouTube Data API v3 (自现在是最新版本。