با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
مراحل توضیح داده شده در ادامه این صفحه را تکمیل کنید و تنها در چند دقیقه یک اسکریپت ساده Google Apps خواهید داشت که درخواست هایی را به YouTube Data API ارسال می کند.
برنامه نمونه نحوه افزودن داده های کانال YouTube را به صفحه گسترده نشان می دهد.
پیش نیازها
برای اجرای این شروع سریع، به موارد زیر نیاز دارید:
در صفحه گسترده جدید، روی Extensions>Apps Script کلیک کنید.
محتوای ویرایشگر اسکریپت را با کد زیر جایگزین کنید:
//Note:AppsScriptautomaticallyrequestsauthorization//basedontheAPI's used in the code.functionchannelsListByUsername(part,params){varresponse=YouTube.Channels.list(part,params);varchannel=response.items[0];vardataRow=[channel.id,channel.snippet.title,channel.statistics.viewCount];SpreadsheetApp.getActiveSpreadsheet().appendRow(dataRow);}functiongetChannel(){varui=SpreadsheetApp.getUi();varchannelName=ui.prompt("Enter the channel name: ").getResponseText();channelsListByUsername('snippet,contentDetails,statistics',{'forUsername':channelName});}functiongetGoogleDevelopersChannel(){channelsListByUsername('snippet,contentDetails,statistics',{'forUsername':'GoogleDevelopers'});}functiononOpen(){varfirstCell=SpreadsheetApp.getActiveSheet().getRange(1,1).getValue();if(firstCell!='ID'){varheaderRow=["ID","Title","View count"];SpreadsheetApp.getActiveSpreadsheet().appendRow(headerRow);}varui=SpreadsheetApp.getUi();ui.createMenu('YouTube Data').addItem('Add channel data','getChannel').addSeparator().addItem('Add GoogleDevelopers data','getGoogleDevelopersChannel').addToUi();}
روی پروژه Untitled در بالا سمت چپ کلیک کنید، Quickstart را تایپ کنید و روی تغییر نام کلیک کنید.
مرحله ۲: YouTube Data API را روشن کنید
در سمت چپ، روی codeویرایشگر کلیک کنید.
در سمت چپ، در کنار «سرویسها»، روی « add یک سرویس» کلیک کنید.
YouTube Data API را کلیک کنید، سپس روی Add کلیک کنید.
مرحله 3: نمونه را اجرا کنید
صفحه گسترده خود را دوباره بارگیری کنید. اگر اولین باری است که صفحه گسترده را پس از افزودن کد بارگیری میکنید، ردیف اول باید با سرصفحههای ID ، عنوان و تعداد مشاهده پر شود.
در نوار منو، روی YouTube Data > Add Google Developers کلیک کنید تا اطلاعات مربوط به کانال GoogleDevelopers را به صفحه گسترده خود اضافه کنید. (منوی YouTube Data باید در کنار منوهای استاندارد - فایل، ویرایش، مشاهده و غیره ظاهر شود)
اولین باری که نمونه را اجرا میکنید، از شما میخواهد که دسترسی را مجاز کنید:
روی بررسی مجوزها کلیک کنید.
یک حساب کاربری انتخاب کنید.
روی Allow کلیک کنید.
در نوار منو، روی دادههای YouTube > افزودن دادههای کانال کلیک کنید تا دادههای کانال مورد نظر خود را اضافه کنید. هنگامی که از شما خواسته شد، نام کانال را وارد کنید (به عنوان مثال "GoogleDevelopers" یا "SaturdayNightLive") و روی OK کلیک کنید. اسکریپت داده های آن کانال را بازیابی می کند و به صفحه گسترده اضافه می کند.
این خطا زمانی رخ می دهد که API در ویرایشگر کد Apps Script روشن نشده باشد. مرحله 2.b را مجدداً مشاهده کنید و مطمئن شوید که کلید مربوطه روی روشن تنظیم شده است.
تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis guide provides a quickstart for creating a Google Apps Script that interacts with the YouTube Data API to add YouTube channel data to a Google Sheet.\u003c/p\u003e\n"],["\u003cp\u003eThe script retrieves channel ID, title, and view count and populates a spreadsheet with this information, including the ability to add data for a specified channel or the GoogleDevelopers channel.\u003c/p\u003e\n"],["\u003cp\u003eUsers need a Google account, internet access, a web browser, and access to Google Drive to follow the steps and can turn on the YouTube Data API directly within the Apps Script editor.\u003c/p\u003e\n"],["\u003cp\u003eThe guide offers a method to run a sample script, which first prompts for authorization, then allows the user to select from a menu to add data either for the GoogleDevelopers channel or for a custom-selected channel.\u003c/p\u003e\n"],["\u003cp\u003eA troubleshooting section is available to help resolve common issues, such as the "[API NAME]" is not defined error, and also provides further reading with documentation on the topic.\u003c/p\u003e\n"]]],["This guide details how to create a Google Apps Script to fetch and add YouTube channel data to a Google Sheet. The script, once set up, can retrieve channel ID, title, and view count. Key actions include: creating a new Google Sheet, pasting provided code into the Apps Script editor, enabling the YouTube Data API service, and running script options to add either GoogleDevelopers channel data or a user-specified channel's data to the sheet.\n"],null,["# Google Apps Script Quickstart\n\nComplete the steps described in the rest of this page, and in just a few\nminutes you'll have a simple [Google Apps Script](/apps-script/overview)\nthat makes requests to the YouTube Data API.\n\nThe sample application demonstrates how to add YouTube channel data to a\nspreadsheet.\n\nPrerequisites\n-------------\n\nTo run this quickstart, you'll need:\n\n- Access to the internet and a web browser.\n- A Google account.\n- Access to Google Drive.\n\nStep 1: Create the script\n-------------------------\n\n1. Open [Google Drive](https://drive.google.com) in your web browser.\n2. Click **New** \\\u003e **Google Sheets**.\n3. In the new spreadsheet, click **Extensions** \\\u003e **Apps Script**.\n4. Replace the contents of the script editor with the following code: \n\n ```gdscript\n // Note: Apps Script automatically requests authorization\n // based on the API's used in the code.\n\n function channelsListByUsername(part, params) {\n var response = YouTube.Channels.list(part,\n params);\n var channel = response.items[0];\n var dataRow = [channel.id, channel.snippet.title, channel.statistics.viewCount];\n SpreadsheetApp.getActiveSpreadsheet().appendRow(dataRow);\n }\n\n function getChannel() {\n var ui = SpreadsheetApp.getUi();\n var channelName = ui.prompt(\"Enter the channel name: \").getResponseText();\n channelsListByUsername('snippet,contentDetails,statistics',\n {'forUsername': channelName});\n }\n\n function getGoogleDevelopersChannel() {\n channelsListByUsername('snippet,contentDetails,statistics',\n {'forUsername': 'GoogleDevelopers'});\n }\n\n function onOpen() {\n var firstCell = SpreadsheetApp.getActiveSheet().getRange(1, 1).getValue();\n if (firstCell != 'ID') {\n var headerRow = [\"ID\", \"Title\", \"View count\"];\n SpreadsheetApp.getActiveSpreadsheet().appendRow(headerRow);\n }\n var ui = SpreadsheetApp.getUi();\n ui.createMenu('YouTube Data')\n .addItem('Add channel data', 'getChannel')\n .addSeparator()\n .addItem('Add GoogleDevelopers data', 'getGoogleDevelopersChannel')\n .addToUi();\n }\n https://github.com/youtube/api-samples/blob/07263305b59a7c3275bc7e925f9ce6cabf774022/apps-script/quickstart.gs\n ```\n5. Click Save .\n6. Click **Untitled project** at the top left, type **Quickstart** , and click **Rename**.\n\nStep 2: Turn on the YouTube Data API\n------------------------------------\n\n1. At the left, click **Editor** code.\n2. At the left, next to \"Services,\" click Add a service add.\n3. Click **YouTube Data API** , then click **Add**.\n\nStep 3: Run the sample\n----------------------\n\n1. 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.\n2. In the menu bar, click **YouTube Data** \\\u003e **Add GoogleDevelopers data**\n to add information about the GoogleDevelopers channel to your spreadsheet.\n (The YouTube Data menu should appear next to the standard menus --\n File, Edit, View, etc.)\n\n \u003cbr /\u003e\n\n\n The first time you run the sample, it will prompt you to authorize access:\n\n 1. Click **Review permissions**.\n 2. Choose an account.\n 3. Click **Allow**.\n3. In the menu bar, click **YouTube Data** \\\u003e **Add channel data** to add data\n for a channel of your choice. When prompted, enter the channel name\n (e.g. \"GoogleDevelopers\" or \"SaturdayNightLive\") and click **OK**. The\n script retrieves data for that channel and adds it to the spreadsheet.\n\nIt worked! **Great!** Check out the further reading section below to learn more.\nI got an error **Bummer.** Check out our [troubleshooting](#troubleshooting) section below for some common errors and solutions. Thanks for letting us know and we'll work to fix this quickstart.\n\nFurther reading\n---------------\n\n- [Google Apps Script Advanced Services documentation](/apps-script/guides/services/advanced)\n- [YouTube Data API reference documentation](/youtube/v3/docs)\n\nTroubleshooting\n---------------\n\n#### ReferenceError: \"\\[API NAME\\]\" is not defined\n\nThis error occurs when the API hasn't been toggled on in the Apps Script code\neditor. Revisit Step 2.b and ensure the corresponding toggle is set to **on**."]]