0

How to open a URL in a new tab that is launched by selecting a check box?

I have a table as in the screenshot attached, I want me to open the link from the cell on the right in a new tab after changing the checbox enter image description here

I tried to use this script,

function openTab() {
  var selection = SpreadsheetApp.getActiveSheet().getActiveCell().getValue();
  var html = "<script>window.open('" + selection + "');google.script.host.close();</script>";
  var userInterface = HtmlService.createHtmlOutput(html);

  SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Tab');
}

but I don't know how to:

  1. tag the script to only be active on one selected sheet
  2. get the link from the cell next to it
Rubén
  • 34,714
  • 9
  • 70
  • 166
Robert
  • 1
  • 2

1 Answers1

0

Opening a new tab by checking a checkbox

Note: this must be an installable trigger. Caution: do not name it onEdit

function onMyEdit(e) {
  const sh = e.range.getSheet();
  if(sh.getName() == "Sheet0" && e.range.columnStart == 1 && e.range.rowStart == 1 && e.value == "TRUE") {
    e.range.setValue("FALSE");
    openUrl();
  }
}
function openUrl(){
  let html = '<h1>Dummy Dialog</h1><script>window.onload = function(){google.script.run.withSuccessHandler(function(url){window.open(url,"_blank");google.script.host.close();}).getMyUrl();}</script>';
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),"Dummy Dialog");
}

function getMyUrl() {
  console.log('getting url');
  return "http://www.google.com";
}

Demo:

enter image description here

Cooper
  • 59,616
  • 6
  • 23
  • 54