ตัวอย่างโค้ด Apps Script

หากต้องการเรียกใช้โค้ดตัวอย่าง คุณต้องเปิดใช้ YouTube Data API และ YouTube Analytics API (v2) ใน Apps Script คู่มือเริ่มต้นใช้งาน Data API จะอธิบายวิธีเพิ่มบริการ

ส่งออกข้อมูล YouTube Analytics ไปยัง Google ชีต

ฟังก์ชันนี้ใช้ YouTube Analytics API เพื่อดึงข้อมูลเกี่ยวกับช่องของผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์ ซึ่งจะสร้าง Google ชีตใหม่ในไดรฟ์ของผู้ใช้พร้อมข้อมูล

ส่วนแรกของตัวอย่างนี้แสดงการเรียก YouTube Analytics API แบบง่าย ฟังก์ชันนี้จะดึงข้อมูลรหัสช่องของผู้ใช้ที่ใช้งานอยู่ก่อน ฟังก์ชันจะใช้รหัสดังกล่าวเพื่อเรียกใช้ YouTube Analytics API เพื่อดึงข้อมูลยอดดู การกดชอบ ไม่ชอบ และการแชร์ในช่วง 30 วันที่ผ่านมา API จะแสดงผลข้อมูลในออบเจ็กต์การตอบกลับซึ่งมีอาร์เรย์ 2 มิติ

ส่วนที่ 2 ของตัวอย่างจะสร้างสเปรดชีต สเปรดชีตนี้จะอยู่ใน Google ไดรฟ์ของผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์ โดยมีชื่อเป็น "รายงาน YouTube" และมีช่วงวันที่ในชื่อ ฟังก์ชันจะป้อนข้อมูลสเปรดชีตด้วยการตอบกลับของ API จากนั้นล็อกคอลัมน์และแถวที่จะกำหนดแกนของแผนภูมิ ระบบจะเพิ่มแผนภูมิคอลัมน์แบบซ้อนสำหรับสเปรดชีต

  function spreadsheetAnalytics() {     // Get the channel ID     var myChannels = YouTube.Channels.list('id', {mine: true});     var channel = myChannels.items[0];     var channelId = channel.id;        // Set the dates for our report     var today = new Date();     var oneMonthAgo = new Date();     oneMonthAgo.setMonth(today.getMonth() - 1);     var todayFormatted = Utilities.formatDate(today, 'UTC', 'yyyy-MM-dd')     var oneMonthAgoFormatted = Utilities.formatDate(oneMonthAgo, 'UTC', 'yyyy-MM-dd');        // The YouTubeAnalytics.Reports.query() function has four required parameters and one optional     // parameter. The first parameter identifies the channel or content owner for which you are     // retrieving data. The second and third parameters specify the start and end dates for the     // report, respectively. The fourth parameter identifies the metrics that you are retrieving.     // The fifth parameter is an object that contains any additional optional parameters     // (dimensions, filters, sort, etc.) that you want to set.     var analyticsResponse = YouTubeAnalytics.Reports.query({       "startDate": oneMonthAgoFormatted,       "endDate": todayFormatted,       "ids": "channel==" + channelId,       "dimensions": "day",       "sort": "-day",       "metrics": "views,likes,dislikes,shares"     });        // Create a new Spreadsheet with rows and columns corresponding to our dates     var ssName = 'YouTube channel report ' + oneMonthAgoFormatted + ' - ' + todayFormatted;     var numRows = analyticsResponse.rows.length;     var numCols = analyticsResponse.columnHeaders.length;        // Add an extra row for column headers     var ssNew = SpreadsheetApp.create(ssName, numRows + 1, numCols);        // Get the first sheet     var sheet = ssNew.getSheets()[0];        // Get the range for the title columns     // Remember, spreadsheets are 1-indexed, whereas arrays are 0-indexed     var headersRange = sheet.getRange(1, 1, 1, numCols);     var headers = [];        // These column headers will correspond with the metrics requested     // in the initial call: views, likes, dislikes, shares     for(var i in analyticsResponse.columnHeaders) {       var columnHeader = analyticsResponse.columnHeaders[i];       var columnName = columnHeader.name;       headers[i] = columnName;     }     // This takes a 2 dimensional array     headersRange.setValues([headers]);        // Bold and freeze the column names     headersRange.setFontWeight('bold');     sheet.setFrozenRows(1);        // Get the data range and set the values     var dataRange = sheet.getRange(2, 1, numRows, numCols);     dataRange.setValues(analyticsResponse.rows);        // Bold and freeze the dates     var dateHeaders = sheet.getRange(1, 1, numRows, 1);     dateHeaders.setFontWeight('bold');     sheet.setFrozenColumns(1);        // Include the headers in our range. The headers are used     // to label the axes     var range = sheet.getRange(1, 1, numRows, numCols);     var chart = sheet.newChart()                      .asColumnChart()                      .setStacked()                      .addRange(range)                      .setPosition(4, 2, 10, 10)                      .build();     sheet.insertChart(chart);      }