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.

Prerequisites

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,
                                           params);
      var channel = response.items[0];
      var dataRow = [channel.id, channel.snippet.title, channel.statistics.viewCount];
      SpreadsheetApp.getActiveSpreadsheet().appendRow(dataRow);
    }
    
    function getChannel() {
      var ui = SpreadsheetApp.getUi();
      var channelName = ui.prompt("Enter the channel name: ").getResponseText();
      channelsListByUsername('snippet,contentDetails,statistics',
                             {'forUsername': channelName});
    }
    
    function getGoogleDevelopersChannel() {
      channelsListByUsername('snippet,contentDetails,statistics',
                             {'forUsername': 'GoogleDevelopers'});
    }
    
    function onOpen() {
      var firstCell = SpreadsheetApp.getActiveSheet().getRange(1, 1).getValue();
      if (firstCell != 'ID') {
        var headerRow = ["ID", "Title", "View count"];
        SpreadsheetApp.getActiveSpreadsheet().appendRow(headerRow);
      }
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('YouTube Data')
      .addItem('Add channel data', 'getChannel')
      .addSeparator()
      .addItem('Add GoogleDevelopers data', 'getGoogleDevelopersChannel')
      .addToUi();
    }
    
  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

Troubleshooting

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.

Send feedback about...

YouTube Data API
YouTube Data API