如何在下拉选项中添加Link?

How Add the Link in the Dropdown options?

您好,我正在处理我在这个论坛上获得的脚本。

选项在 A 列中提到,链接在 B 列中添加,在下拉列表中如果我单击选项 1,我想在另一个选项卡的 B 列中打开相应的 link。

Code.gs

function doGet(e) {
  return HtmlService.createTemplateFromFile("Index.html")
    .evaluate()
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function getSelectOptions()
{
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Options');
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var options=[];
  for(var i=0;i<vA.length;i++)
  {
    options.push(vA[i][0]);
  }
  return vA;
}

function showSidebar()
{
  var userInterface=HtmlService.createHtmlOutputFromFile('Index');
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'The Drop Down with No Options now has options.');
}

Index.html

<!DOCTYPE html>
<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>
    $(function() {
        $('#txt1').val('');
        google.script.run
          .withSuccessHandler(updateSelect)
          .getSelectOptions();
      });
    function updateSelect(vA)
    {
      var select = document.getElementById("sel1");
      select.options.length = 0; 
      for(var i=0;i<vA.length;i++)
      {
        select.options[i] = new Option(vA[i],vA[i]);
      }
    }
    console.log("My code");
  </script>
  </head>
  <body>
   <select id="sel1" style="width:125px;height:35px;margin:10px 0 10px 0;">
   </select>
 </body>
</html>

我不知道如何将 link 嵌入到选项中。应该为单个列定义另一个函数还是可以在单个函数中完成?

注意:我不是程序员。

您可以使用 onChange 事件在用户选择选项时打开 link

为此:

  • 使用语法 new Option(text, value)。要将选项和 link 作为文本和值传递,您需要访问相应的嵌套数组元素:
select.options[i] = new Option(vA[i][0],vA[i][1]);

样本:

<!DOCTYPE html>
<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>
    $(function() {
        $('#txt1').val('');
        google.script.run
          .withSuccessHandler(updateSelect)
          .getSelectOptions();
      });
    function updateSelect(vA)
    {
      var select = document.getElementById("sel1");
      select.options.length = 0; 
      for(var i=0;i<vA.length;i++)
      {
        select.options[i] = new Option(vA[i][0],vA[i][1]);
      }
    }
    console.log("My code");
    
    function myFunction(){
      var value = document.getElementById("sel1").value;
      window.open(value, '_blank');
    }
  </script>
  </head>
  <body>
   <select id="sel1" style="width:125px;height:35px;margin:10px 0 10px 0;" onchange="myFunction()"> 
   </select>
 </body>
</html>