Friday, March 13, 2015

YouTube Google Forms and Apps Script BFFs

Last month, we announced several new ways to customize Google Forms. As of this week, three of those options are also available in forms created from Apps Script — embedding YouTube videos, displaying a progress bar, and showing a custom message if a form isn’t accepting responses.


Adding a YouTube video is as simple as any other Google Forms operation in Apps Script — from the Form object, just call addVideoItem(), then setVideoUrl(youtubeUrl). Naturally, you can also control the video’s size, alignment, and so forth.

To show a progress bar, call setProgressBar(enabled). Don’t even need a second sentence to explain that one. The custom message for a form that isn’t accepting responses is similarly easy: setCustomClosedFormMessage(message), and you’re done.

Want to give it a try yourself? Copy and paste the sample code below into the script editor at script.google.com, then hit Run. When the script finishes, click View > Logs to grab the URL for your new form, or look for it in Google Drive.

function showNewFormsFeatures() {
var form = FormApp.create(New Features in Google Forms);
var url = form.getPublishedUrl();

form.addVideoItem()
.setVideoUrl(http://www.youtube.com/watch?v=38H7WpsTD0M);

form.addMultipleChoiceItem()
.setTitle(Look, a YouTube video! Is that cool, or what?)
.setChoiceValues([Cool, What]);

form.addPageBreakItem();

form.addCheckboxItem()
.setTitle(Progress bars are silly on one-page forms.)
.setChoiceValues([Ah, that explains why the form has two pages.]);

form.setProgressBar(true);

form.setCustomClosedFormMessage(Too late — this form is closed. Sorry!);
// form.setAcceptingResponses(false); // Uncomment to see custom message.

Logger.log(Open this URL to see the form: %s, url);
}

Dan Lazin   profile | twitter

Dan is a technical writer on the Developer Relations team for Google Apps Script. Before joining Google, he worked as video-game designer and newspaper reporter. He has bicycled through 17 countries.