Google Apps Script Quickstart

Complete the steps described in the rest of this page, and in just a few minutes you'll have a simple Google Apps Script that makes requests to the YouTube Data API.

The sample application demonstrates how to add YouTube channel data to a spreadsheet.


To run this quickstart, you'll need:

  • Access to the internet and a web browser.
  • A Google account.
  • Access to Google Drive.

Step 1: Create the script

  1. Open Google Drive in your web browser.
  2. Click New > Google Sheets.
  3. In the new spreadsheet, click Tools > Script editor....
  4. Replace the contents of the script editor with the following code:
    // Note: Apps Script automatically requests authorization
    // based on the API's used in the code.
    function channelsListByUsername(part, params) {
      var response = YouTube.Channels.list(part,
      var channel = response.items[0];
      var dataRow = [, channel.snippet.title, channel.statistics.viewCount];
    function getChannel() {
      var ui = SpreadsheetApp.getUi();
      var channelName = ui.prompt("Enter the channel name: ").getResponseText();
                             {'forUsername': channelName});
    function getGoogleDevelopersChannel() {
                             {'forUsername': 'GoogleDevelopers'});
    function onOpen() {
      var firstCell = SpreadsheetApp.getActiveSheet().getRange(1, 1).getValue();
      if (firstCell != 'ID') {
        var headerRow = ["ID", "Title", "View count"];
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('YouTube Data')
      .addItem('Add channel data', 'getChannel')
      .addItem('Add GoogleDevelopers data', 'getGoogleDevelopersChannel')
  5. Click File > Save, name your project "Quickstart", and click OK.

Step 2: Turn on the YouTube Data API

  1. In the Apps Script editor, click Resources > Advanced Google Services.
  2. Locate YouTube Data API in the dialog and click the corresponding toggle, setting it to on.
  3. Click the Google API Console link in the box below the list of services:
  4. Enter "YouTube Data API v3" into the search box and click on the corresponding entry in the results.
  5. Click the Enable button.
  6. Return to the Apps Script code editor and click the OK button in the Advanced Google Services dialog. (The button appears below the link to the Google API Console shown in step 2.3.)

Step 3: Run the sample

  1. Reload your spreadsheet. If it is the first time you are loading the spreadsheet after adding the code, the first row should populate with ID, Title, and View count headers.
  2. In the menu bar, click YouTube Data > Add GoogleDevelopers data to add information about the GoogleDevelopers channel to your spreadsheet. (The YouTube Data menu should appear next to the standard menus – File, Edit, View, etc.)

    The first time you run the sample, it will prompt you to authorize access:
    1. Click the Continue button.
    2. If you are logged into multiple Google accounts, you will be asked to select one account to use for the authorization.
    3. Click the Allow button to authorize access to manage your spreadsheets in Google Drive and to view your YouTube account. The first permission enables the script to write data to the spreadsheet. The latter permission lets the script execute YouTube API requests on your behalf.
  3. In the menu bar, click YouTube Data > Add channel data to add data for a channel of your choice. When prompted, enter the channel name (e.g. "GoogleDevelopers" or "SaturdayNightLive") and click OK. The script retrieves data for that channel and adds it to the spreadsheet.

Further reading


ReferenceError: "[API NAME]" is not defined

This error occurs when the API hasn't been toggled on in the Apps Script code editor. Revisit Step 2.b and ensure the corresponding toggle is set to on.