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

2024-01-01

如何使这个 google Sheets 脚本能够更新现有的 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);                                                             
}

解释:

我假设您在带有名称的工作表中有一个 YouTube 视频 ID 列表Sheet1在范围中A2:A像那样:

上述代码将迭代此列表yt_video_ids它将把每个视频 ID 添加到选定的播放列表中,定义为playlistId.

资源:

从谷歌脚本编辑器中,您需要单击资源 => 高级谷歌服务然后启用YouTube 数据 API v3(到目前为止这是最新版本)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Google 脚本和表格创建 YouTube 播放列表 的相关文章

随机推荐