如何在 google sheet 侧边栏插件中向单元格添加超链接

How to add hyperlink to cell in google sheet sidebar addon

这个 shared spreadsheet 有一个边栏(通过自定义菜单 扩展菜单 打开)

边栏显示电子表格中的单元格列表。 当前硬编码 A1A2、...(在脚本编辑器中 SideBarTemplate.html

是否可以link到特定的单元格?

侧栏中显示的最后一行这是对 A1 的超链接测试 尝试使用带有 link 到 A1 (href="#gid=1744285851&range=A1") 的 href 标签 但这不起作用。

边栏图像:

气体代码:

function onOpen(){

  addMenu();
  
}

function addMenu(){
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'ShowSideBar', functionName: 'showSideBar'}
    ];
  spreadsheet.addMenu ('Extensions Menu', menuItems);
}


function showSideBar(){
  // Display a sidebar with custom HtmlService content.
  var htmlTemplate = HtmlService
                   .createTemplateFromFile("SideBarTemplate");
  
  htmlOutput = htmlTemplate.evaluate();
 
  htmlOutput.setTitle('Ext. Side Bar');
  
  SpreadsheetApp.getUi().showSidebar(htmlOutput);
  
}

HTML 模板:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
.style1 {
  color: blue;
  
}
.style2 {
color: red;
}
</style>
  </head>
  <body>
    <?var cells = ["A1","A2"]
    for (var i = 0; i< cells.length;i++){?>
   
    <div class="style1">Cell: <?=cells[i]?> <p class="style2">Required: How to make the blue text to hyper link to cell <?=cells[i]}?>
    </p></div>
    
    <a href="#gid=1744285851&range=A1">This is HyperLink test to A1</a>
  </body>
</html>

由于,侧边栏框架沙箱属性缺少allow-top-navigation,因此无法导航顶部框架(包括哈希更改)。

解决方法:

  • 使用服务器端 range.activate() methods. Using google.script.run,您可以在 google 张中更改活动范围:

    //client side
    a1buttonElement.addEventListener('click',()=>google.script.run.select("a1"))
    
    //server side
    const select = e => SpreadsheetApp.getActive().getActiveSheet().getRange(e).activate()
    
  • 绕过沙箱限制的变通方法是使用 web-app。在这种情况下,Google 张纸应由 web-app 打开。如果它是由 webapp 打开的,我们可以利用 window.opener api 写入 top 框架。参见