'Google Apps Script to open a URL based on a cell

I have a cell B2 which shows dynamic hyperlinks depending on user's selection and I would like to use that cell as a reference to my script (which opens url on a button). How can I modify this script in order to get the link from the B2 cell?

//change the value of url to your desired url.
function openUrl( url="https://google.com" ){
  var html = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
  +'if(document.createEvent){'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ a.click() }'
  +'close();'
  +'</script>'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  +'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
  +'</html>')
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );
}


Solution 1:[1]

From I would like to use that cell as a reference to my script (which opens url on a button). and How can I modify this script in order to get the link from the B2 cell?, when your script is modified, how about the following modification?

From:

function openUrl( url="https://google.com" ){

To:

function openUrl(){
  var url = SpreadsheetApp.getActiveSheet().getRange("B2").getValue();

References:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanaike