Google Apps Script 빠른 시작

이 페이지의 나머지 부분에 설명된 단계를 완료하면 YouTube Data API에 요청을 보내는 간단한 Google Apps Script가 몇 분 안에 생성됩니다.

샘플 애플리케이션은 스프레드시트에 YouTube 채널 데이터를 추가하는 방법을 보여줍니다.

기본 요건

이 빠른 시작을 실행하려면 다음이 필요합니다.

  • 인터넷 및 웹브라우저 액세스
  • Google 계정
  • Google Drive 액세스

1단계: 스크립트 만들기

  1. 웹브라우저에서 Google Drive를 엽니다.
  2. 새로 만들기 > Google Sheets 를 클릭합니다.
  3. 새 스프레드시트에서 확장 프로그램 > Apps Script 를 클릭합니다.
  4. 스크립트 편집기의 콘텐츠를 다음 코드로 바꿉니다.
    // 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. 저장 을 클릭합니다.
  6. 왼쪽 상단에서 제목 없는 프로젝트를 클릭하고 빠른 시작을 입력한 후 이름 바꾸기를 클릭합니다.

2단계: YouTube Data API 사용 설정

  1. 왼쪽에서 편집기 를 클릭합니다.
  2. 왼쪽의 '서비스' 옆에 있는 서비스 추가 를 클릭합니다.
  3. YouTube Data API 를 클릭한 후 추가 를 클릭합니다.

3단계: 샘플 실행

  1. 스프레드시트를 다시 로드합니다. 코드를 추가한 후 스프레드시트를 처음 로드하는 경우 첫 번째 행에 ID, 제목, 조회수 헤더가 채워집니다.
  2. 메뉴 바에서 YouTube Data > GoogleDevelopers 데이터 추가 를 클릭하여 GoogleDevelopers 채널에 관한 정보를 스프레드시트에 추가합니다. (YouTube Data 메뉴는 표준 메뉴(파일, 수정, 보기 등) 옆에 표시됩니다.)

    샘플을 처음 실행하면 액세스 권한을 부여하라는 메시지가 표시됩니다.

    1. 권한 검토 를 클릭합니다.
    2. 계정을 선택합니다.
    3. 허용 을 클릭합니다.
  3. 메뉴 바에서 YouTube Data > 채널 데이터 추가 를 클릭하여 원하는 채널의 데이터를 추가합니다. 메시지가 표시되면 채널 이름(예: 'GoogleDevelopers' 또는 'SaturdayNightLive')을 입력하고 확인 을 클릭합니다. 스크립트는 해당 채널의 데이터를 가져와서 스프레드시트에 추가합니다.

추가 자료

문제 해결

ReferenceError: '[API 이름]'이(가) 정의되지 않았습니다.

이 오류는 Apps Script 코드 편집기에서 API가 사용 설정되지 않은 경우 발생합니다. 2.b단계를 다시 방문하여 해당 전환이 사용 으로 설정되어 있는지 확인합니다.