2

I'm trying to have a video play based on a random number generated in a spreadsheet. since google sheets doesn't allow you to embed youtube videos directly I'm trying to do it through a custom dialogue box.

Here's what I have so far:

Google Script Code

    var PlayDes = sheet.getRange('g2').getValue();
    GameSheet.getRange('D4').setValue(TotYds1)
     var html = HtmlService.createHtmlOutputFromFile('Youtube')
        .setWidth(560)
        .setHeight(315);
    SpreadsheetApp.getUi() 
       .showModalDialog(html, PlayDes);

Then the HTML code is:

    <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/7WSQGD874Yc?start=8&end=18" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<input type="button" value="Close" onclick="google.script.host.close()" />

I'm looking to change the video URL in the html code based on the value that's pulled from cell G2 in a spreadsheet.

1

2 Answers 2

1

Thanks to @JSmith for the help here's the solution I found... Google Script side

 function retrieveValue(){
  spreadsheet = SpreadsheetApp.openById('sheet ID')
  sheet = spreadsheet.getSheetByName('Data')
  var PlayDes = sheet.getRange('g2').getValue();
 return (sheet.getRange('g2').getValue());

Then on the html side

<script>
  //*********************************** retrieve the URL from spreadsheet**********************************
var myValue
  function Vidurl(value)
  {
   myValue=value
   document.write('<iframe width="560" height="315" src= '+myValue+' frameborder="0" allow="autoplay; encrypted-media" allowfullscreen><\/iframe>');
  }
  google.script.run.withSuccessHandler(Vidurl).retrieveValue(); 
</script>

One issue I had was the browser didn't like my script and was labeling it as dangerous script which I then had to over ride.

Sign up to request clarification or add additional context in comments.

Comments

0

Let's say your cell is g2

create a script in your IDE like so:

function retrieveValue()
{
  ...//grab spreadsheet and sheet
  return (sheet.getRange('g2').getValue());
}

then call it on your HTML client side as so:

 <iframe id="myVideo" width="560" height="315" src="NEED VARIABLE HERE" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
    <script>
      var myValue = null;
      function onSuccess(value)
      {
        $("#myVideo").attr('src', value);
      }
      google.script.run.withSuccessHandler(onSuccess).retrieveValue();
    </script>

NOTE

google.script.run is an asynchronous call so you'll have to wait the call is finished to get the desired value on the client side.

11 Comments

forgive my ignorance, but once I have my variable passed from G2 how do I go about adding it to the iframe I'm trying to create? <iframe width="560" height="315" src="NEED VARIABLE HERE" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
well use JQuery there's a lot of documentation about this kind of thing . an example with img tag
@ShavedApeBaby just edited my post to show you how this could be made
for the retrieval isn't working. I know that the correct string is being returned at the end of the retirieveValue function, but its not making it to the html function. The variable myValue is null after the script is run
@ShavedApeBaby can you share with me your spreadsheet please. I'll make it work. Or an example one.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.