I really need to update my YouTube videos. So I decided to work on a way to automate that process. There is still a ways to go, but I hope there are some useful morsels of info nestled in this series.

Want the code? Good! Here it is.

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('YouTube Dashboard')
    .addItem('Update Sheet', 'updateSheet')
    .addItem('Update YouTube', 'updateYouTube')
    .addToUi();
}

function updateSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var videoList = getAllVideos();

  var values = videoList.map(function(video) {
    var image = '=IMAGE("' + video.snippet.thumbnails['default'].url + '", 4, 50, 70)';
    Logger.log(video.snippet);
    return [false, image, video.id, video.snippet.title, video.snippet.description];
  });

  if (!values.length) {
    ss.toast('No videos');
    return;
  }

  var frozenRow = sheet.getFrozenRows() + 1;
  sheet
    .getRange(frozenRow, 1, values.length, values[0].length)
    .clearContent()
    .setValues(values);
}

function onChange(e) {
  var range = e.range;
  var value = e.value;
  var source = e.source;
  var sheet = source.getActiveSheet();
  var modified = sheet.getRange(range.getRow(), 1).getValue();

  //has this row already been edited?
  if (modified) {
    return;
  }

  //make sure it is the correct sheet
  if (sheet.getName() !== 'VideoList') {
    return;
  }

  //make sure it is column 4 or 5
  var isEditableColumn = range.getColumn() === 4 || range.getColumn() === 5;
  if (!isEditableColumn) {
    return;
  }

  sheet.getRange(range.getRow(), 1).setValue(true);
  source.toast('row modified');
}

function updateYouTube() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('VideoList');
  var frozenRow = sheet.getFrozenRows() + 1;
  var rows = sheet.getRange(frozenRow, 1, sheet.getLastRow() - 1, sheet.getLastColumn());

  var modifiedRows = rows.getValues().filter(function(row) {
    return row[0] === true;
  });

  var loopCount = 0;
  var allVideoSnippets = [];

  while (loopCount < Math.ceil(modifiedRows.length / 50)) {
    var start = loopCount * 50;
    var current50 = modifiedRows.slice(start, start + 50);
    var currentIds = current50.map(function(row) {
      return row[2];
    });

    var videoSnippets = YouTube.Videos.list('snippet', {
      id: currentIds.join(','),
    });

    allVideoSnippets = allVideoSnippets.concat(videoSnippets.items);

    loopCount++;
  }

  allVideoSnippets.forEach(function(item, idx) {
    item.snippet.title = modifiedRows[idx][3];
    item.snippet.description = modifiedRows[idx][4];
    YouTube.Videos.update(item, 'snippet');
  });

  ss.toast('Done updating ' + allVideoSnippets.length + ' videos');
}

Comment below with suggestions.