Search code examples
google-apps-scriptyoutube-data-api

Allow comma separated values in a prompt: Youtube API Example


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});

Solution

    • 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.