Google Apps 脚本,如何将记录的数据发送到电子表格?

Google Apps Script, how to send logged data to spreadsheet?

这可能不是很复杂,但我在弄清楚它时遇到了很多麻烦,所以任何帮助都会很棒,谢谢!

代码来自 Google Apps Script 示例脚本,我只是想稍微改变一下,以便数据进入活动电子表格。

function retrieveMyUploads() {
  var results = YouTube.Channels.list('contentDetails', {mine: true});
  for(var i in results.items) {
    var item = results.items[i];
    // Get the playlist ID, which is nested in contentDetails, as described in the
    // Channel resource: https://developers.google.com/youtube/v3/docs/channels
    var playlistId = item.contentDetails.relatedPlaylists.uploads;

    var nextPageToken = '';

    // This loop retrieves a set of playlist items and checks the nextPageToken in the
    // response to determine whether the list contains additional items. It repeats that 
    // process until it has retrieved all of the items in the list.
    while (nextPageToken != null) {
      var playlistResponse = YouTube.PlaylistItems.list('snippet', {
        playlistId: playlistId,
        maxResults: 25,
        pageToken: nextPageToken
      });

      for (var j = 0; j < playlistResponse.items.length; j++) {
        var playlistItem = playlistResponse.items[j];
        Logger.log('[%s] Title: %s',
                   playlistItem.snippet.resourceId.videoId,
                   playlistItem.snippet.title);

      }
      nextPageToken = playlistResponse.nextPageToken;
    }
 }
}

我相信你的目标如下。

  • 您想将脚本中 playlistItem.snippet.resourceId.videoId, playlistItem.snippet.title 的值放入 Spreadsheet。

在这种情况下,如何准备一个数组并将值放入数组,然后将数组放入Spreadsheet?当这反映到您的脚本中时,它会变成如下。

修改后的脚本:

在这种情况下,请将以下脚本复制并粘贴到Spreadsheet的脚本编辑器中。通过这种方式,值被放入活动 sheet.

function retrieveMyUploads() {
  var values = []; // <--- Added

  var results = YouTube.Channels.list('contentDetails', {mine: true});
  for(var i in results.items) {
    var item = results.items[i];
    // Get the playlist ID, which is nested in contentDetails, as described in the
    // Channel resource: https://developers.google.com/youtube/v3/docs/channels
    var playlistId = item.contentDetails.relatedPlaylists.uploads;

    var nextPageToken = '';

    // This loop retrieves a set of playlist items and checks the nextPageToken in the
    // response to determine whether the list contains additional items. It repeats that 
    // process until it has retrieved all of the items in the list.
    while (nextPageToken != null) {
      var playlistResponse = YouTube.PlaylistItems.list('snippet', {
        playlistId: playlistId,
        maxResults: 25,
        pageToken: nextPageToken
      });

      for (var j = 0; j < playlistResponse.items.length; j++) {
        var playlistItem = playlistResponse.items[j];
        values.push([playlistItem.snippet.resourceId.videoId, playlistItem.snippet.title]); // <--- Added
        Logger.log('[%s] Title: %s',
                   playlistItem.snippet.resourceId.videoId,
                   playlistItem.snippet.title);

      }
      nextPageToken = playlistResponse.nextPageToken;
    }
 }

  var sheet = SpreadsheetApp.getActiveSheet(); // <--- Added
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values); // <--- Added
}

注:

  • 在此修改后的脚本中,假设您当前的脚本运行良好。请注意这一点。

参考: