Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Google Apps Script to add a YouTube video submitted via a form to a YouTube playlist. Read more
// this function handles the form submit
function addVideoToPlaylist(e){
// 'Youtube url' is the question title from our form. If you change your question change this value
// youtube_parser() takes the url and strips the video_id
var id = youtube_parser(e.namedValues['Youtube url'][0]);
var desc = e.namedValues['Comment'][0]; // if you would like to add a note with playlist item add form field
addToPlaylist(id, desc);
// This function is taken from the YouTube API doc (Javascript example)
// Add a video to a playlist. The "startPos" and "endPos" values let you
// start and stop the video at specific times when the video is played as
// part of the playlist. However, these values are not set in this example.
function addToPlaylist(id, desc, startPos, endPos) {
// this is my playlist id and you need to replace with your own
// To get this in your YouTube channel create or go to you playlist
// and the list id will be in your browser address bar. Basically everything
// after
var playlistId ="PLlMXkWgzMBtOZGp6beg10WqKb66wIk9eu";
// the API requires a resource object. This next bit builds it
var details = {
videoId: id,
kind: 'youtube#video'
if (startPos != undefined) {
details['startAt'] = startPos;
if (endPos != undefined) {
details['endAt'] = endPos;
if (desc == undefined) { // HT @mambelli
var desc = "";
var part= 'snippet,contentDetails';
var resource = {
snippet: {
playlistId: playlistId,
resourceId: details
note: desc
// cheating slightly and doing no error handling. When you setup the form submit you can choose to recieve
// notification of errors. The form response will be submitted to the spreadsheet regardless.
var request = YouTube.PlaylistItems.insert(resource, part);
// Little helper..
function youtube_parser(url){
var regExp = /^.*((\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[7].length==11){
return match[7];
throw("Url incorrecta");

This comment has been minimized.

Copy link

@mambelli mambelli commented Dec 5, 2014

On line 35 you need if (desc == undefined) { , otherwise the comment will never be added. M


This comment has been minimized.

Copy link
Owner Author

@mhawksey mhawksey commented Dec 11, 2014

@mambelli thanks for that. Updated


This comment has been minimized.

Copy link

@geebarb geebarb commented Oct 20, 2015

I don't know if you look here anymore, but I have tried repeatedly to get this to work, and it doesn't. I have an issue on line 5 with namedValues.


This comment has been minimized.

Copy link

@ekman12 ekman12 commented Dec 24, 2015

I'm getting the same problem. The error message I am receiving is: Cannot read property "namedValues" from undefined. (line 5, file "Code")

Do I need to define namedValues somewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.