1

I'm looking to modify YouTube API Apps Script to allow comma separated values in a prompted; or alternative method.

I have been learning how to pull data from the YouTube API through Apps Script using the following example: https://developers.google.com/youtube/v3/quickstart/apps-script

I am able to execute the code but now I'm looking to learn how to be able to add multiple string values to the prompt. In the example, you add a channel name (string) and it will output an ID and view count. I would like to see how you can allow the script to take multiple values.

Add: RedBull, JimmyKimmelLive, ESPN as an example. I believe the call needs to be modified in the below code with the getResponseText. Although, I'm not sure if you would have to call it a different way to allow more than one value.

  var ui = SpreadsheetApp.getUi();
  var channelName = ui.prompt("Enter the channel name: ").getResponseText();
  channelsListByUsername('snippet,contentDetails,statistics',
                         {'forUsername': channelName});
stirfryguy
  • 13
  • 2

1 Answers1

2
  • You want to input the multiple values to the prompt.
    • Sample value is RedBull, JimmyKimmelLive, ESPN.
  • You want to retrieve each value from RedBull, JimmyKimmelLive, ESPN, and give them to channelsListByUsername().

If my understanding is correct, how about this modification? Please think of this as just one of several answers. The flow of this modified script is as follows.

  1. Input the value of RedBull, JimmyKimmelLive, ESPN to the prompt.
  2. Parse the inputted value using split().
  3. Give each value of the parsed value to channelsListByUsername().
    • At that time, I used trim() for this situation.

Modified script:

var ui = SpreadsheetApp.getUi();
var channelName = ui.prompt("Enter the channel name: ").getResponseText();
var values = channelName.split(",");
for (var i = 0; i < values.length; i++) {
  channelsListByUsername('snippet,contentDetails,statistics', {'forUsername': values[i].trim()});
}

Note:

  • In this sample script, the delimiter of inputted value is ,. If you want to other patterns, please tell me.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This is perfect and it works! Just so I understand. You create a values variable and split in this case is similar to text-to-columns function in Excel. the Comma is the "split" to separate values. Then you loop based on the number of inputs within values. – stirfryguy Jun 06 '19 at 00:34
  • @stirfryguy Thank you for replying. I'm glad your issue was resolved. And, your understanding are correct. In the loop, when the value gives to the function ``channelsListByUsername()``, I used ``trim()`` for removing the spaces of top and last character. – Tanaike Jun 06 '19 at 00:39
  • one more question if you have the time. Sometimes a channel ID will no longer be active which will throw an error. I was thinking that adding a validation into the loop would work but not sure if that logic is possible. I was looking at the below response. If totalresults = 0 then skip otherwise continue adding the IDs `pageInfo": { "totalResults": integer, "resultsPerPage": integer` – stirfryguy Jun 11 '19 at 15:14
  • @stirfryguy Thank you for replying. Unfortunately, from your reply comment, I couldn't understand about your new issue. I apologize for my poor English skill. I would like to correctly understand your new issue. So can you post it as new question by including more information? It will help users including me think of your new issue and solution. If you can cooperate to resolve your issue, I'm glad. – Tanaike Jun 11 '19 at 22:20
  • thank you, just created a new question: https://stackoverflow.com/questions/56563450/creating-a-youtube-video-or-channel-id-validation-in-apps-script – stirfryguy Jun 13 '19 at 09:52
  • @stirfryguy Thank you for your response. I saw your new question. But I cannot understand about your question yet. When I could correctly understand your question and could find the solution, I would like to answer to it. If I have any questions for your new question, can I ask you about them? I apologize I couldn't resolve your new question soon. This is due to my poor skill. I deeply apologize for this. – Tanaike Jun 13 '19 at 11:52