我正在学习如何通过Apps脚本使用YouTube数据API。我现在正尝试修改https://developers.google.com/youtube/v3/quickstart/apps-script此处提供的示例,以验证ID是否存在。
我稍微修改了它以查看视频ID,因为这很常见。
在@Tanaike的帮助下,基于我的最后一个问题,Tanaike帮助我创建了一个循环,以允许脚本以逗号分隔的方式添加ID。现在,我正在尝试添加验证。
function channelsListByVideoid(part,params){
var response = YouTube.Videos.list(part,params);
var video = response.items[0];
var dataRow = [video.id,video.status.uploadStatus, video.snippet.title, response.pageInfo.totalResults, video.snippet.channelId, video.snippet.channelTitle];
SpreadsheetApp.getActiveSheet();
SpreadsheetApp.getActiveSpreadsheet().getSheetByName().appendRow(dataRow);
function getvideo() {
var ui = SpreadsheetApp.getUi();
var videoId = ui.prompt("Enter the YouTube Video ID: ").getResponseText();
var values = videoId.split(",");
for (var i = 0; i < values.length; i++) {
channelsListByVideoid('snippet,status,contentDetails,statistics', {'id': values[i].trim()});
有时,诸如https://www.youtube.com/watch?v=--fPZOu_H8g之类的视频ID不可用。在apps-script示例中运行该ID将引发错误TypeError: Cannot read property 'id' of undefined
,这将阻止脚本运行。
我正在考虑如何使用video.list.response属性:
{
"kind": "youtube#videoListResponse",
"etag": etag,
"nextPageToken": string,
"prevPageToken": string,
"pageInfo": {
"totalResults": integer,
"resultsPerPage": integer
},
"items": [
video Resource
]
}
如果视频有效,则totalResults和resultPerPage将显示为1,如果无效,则显示为0:
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 0
我添加了```` response.pageInfo.totalResults,
Logically to me, I need to add a
````var response````
to my getvideo() and add the validation check within the loop.
I'm not clear if you can write it as simply as
如果response.pageinfo.totalResults = 1,则“有效”,否则为“无效”
and then how to allow the code to skip the error that is generated.
Thank you!