侧边栏功能

时间:2019-04-25 13:49:06

标签: google-apps-script

我继续在Google表格中开发侧边栏功能。 我在侧边栏中创建流以及使用链接或按钮创建一些输出消息方面正在寻求帮助。

问题1)-我创建了带有向前和向后箭头的边栏,当我在边栏中单击其他html文件时,我想将它们链接到该文件,因此创建了可以在同一位置来回移动的流程或对话框顺序。即..我单击下一步,函数openTheSidebar2()运行

我有一个搜索下拉菜单功能,在下面的代码中给出了主要风险类别。

问题2)-我希望这些功能在被选中时能够提供一个简单的段落(每个类别的说明)。我需要为每个类别创建一个不同的html文件吗?我希望我可以在同一HTML文件中为每个类别添加每个消息。 超链接是完全错误的方法吗?

非常感谢您的帮助

code.gs

function onOpen() {
  menu();
}


function menu() {
  SpreadsheetApp.getUi().createMenu('Risk Menu')
  .addItem('Generate Risk Waiver', 'createNewCase')
  .addSubMenu(SpreadsheetApp.getUi().createMenu('Risk Help')
             .addItem('Risk Guidance', 'openTheSidebar')
             .addItem('Risk Title', 'openTheSidebar2'))

  .addToUi();
}


function openTheSidebar() {
  var userInterface=HtmlService.createTemplateFromFile('example3').evaluate()
  .setTitle('Risk Rating');
  SpreadsheetApp.getUi().showSidebar(userInterface);
}

function openTheSidebar2() {
  var userInterface2=HtmlService.createTemplateFromFile('example4').evaluate()
    .setTitle('Primary Risk Category');
  SpreadsheetApp.getUi().showSidebar(userInterface2);
}

function openTheSidebar3() {
  var userInterface3=HtmlService.createTemplateFromFile('example').evaluate()  
  .setTitle('Cause');
  SpreadsheetApp.getUi().showSidebar(userInterface3);
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

function getRowColumn() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getActiveCell();
  var rObj={row:rg.getColumn() ,column:rg.getRow()};
  return rObj;
}

function getCellA1() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getActiveCell();
  var rObj={A1:rg.getA1Notation()};
  return rObj;
}

function onCheckOpenSideBar(e) {


  if(e.range.getSheet().getName()!='Option 1')return;
  if(e.range.rowStart==2 && e.range.columnStart==24) {
    if(e.value=='TRUE') {
      openTheSidebar();
      e.range.getSheet().getRange(e.range.rowStart,e.range.columnStart).setValue("FALSE");
    }
  } 
   if(e.range.getSheet().getName()!='Option 1')return;
   if(e.range.rowStart==2 && e.range.columnStart==8) {
    if(e.value=='TRUE') {
      openTheSidebar2();
      e.range.getSheet().getRange(e.range.rowStart,e.range.columnStart).setValue("FALSE");
    }
   }

   if(e.range.getSheet().getName()!='Option 1')return;
   if(e.range.rowStart==2 && e.range.columnStart==1) {
    if(e.value=='TRUE') {
      openTheSidebar3();
      e.range.getSheet().getRange(e.range.rowStart,e.range.columnStart).setValue("FALSE");
    }
   }}

function createOnEditTrigger() {
  ScriptApp.newTrigger('onCheckOpenSideBar').forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();

}

example4.html

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #3e8e41;
}

#myInput {
  border-box: box-sizing;
  background-image: url('searchicon.png');
  background-position: 14px 12px;
  background-repeat: no-repeat;
  font-size: 16px;
  padding: 14px 20px 12px 45px;
  border: none;
  border-bottom: 1px solid #ddd;
}

#myInput:focus {outline: 3px solid #ddd;}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f6f6f6;
  min-width: 230px;
  overflow: auto;
  border: 1px solid #ddd;
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}

a {
  text-decoration: none;
  display: inline-block;
  padding: 8px 16px;
}

a:hover {
  background-color: #ddd;
  color: black;
}

.previous {
  background-color: #f1f1f1;
  color: black;
}

.next {
  background-color: #4CAF50;
  color: white;

}

.round {
  border-radius: 50%;
}
</style>
</head>
<body>

<p>Click on the button to open the dropdown menu, and use the input field to search for a specific dropdown link.</p>

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Categories</button>
  <a href="#" class="previous round">&#8249;</a>
<a href="#" class="next round">&#8250</a>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
<a href="#IT Security & Vulnerability">IT Security & Vulnerability</a>
<a href="#Information Security - Data Only">Information Security - Data Only</a>
<a href="#Technical Debt or Decommission">Technical Debt or Decommission</a>
<a href="#Availability (SLA)">Availability (SLA)</a>
<a href="#Service Continuity & Resilience">Service Continuity & Resilience</a>
<a href="#Capacity & Performance">Capacity & Performance</a>
<a href="#Licencing & Asset Management, Contracts, Maintenance,">Licencing & Asset Management, Contracts, Maintenance,</a>
<a href="#Legal & Regulatory Compliance">Legal & Regulatory Compliance</a>
<a href="#Supplier & Vendor Management">Supplier & Vendor Management</a>
<a href="#Shadow IT">Shadow IT</a>
<a href="#Resource">Resource</a>
<a href="#Knowledge / Skills / Tooling)">Knowledge / Skills / Tooling)</a>
<a href="#Procedure & Process">Procedure & Process</a>
<a href="#Service Ownership">Service Ownership</a>
<a href="#Project">Project</a>
<a href="#Environmental (includes facilities)">Environmental (includes facilities)</a>
<a href="#Architecture and Strategic">Architecture and Strategic</a>
  </div>
</div>

<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  div = document.getElementById("myDropdown");
  a = div.getElementsByTagName("a");
  for (i = 0; i < a.length; i++) {
    txtValue = a[i].textContent || a[i].innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      a[i].style.display = "";
    } else {
      a[i].style.display = "none";
    }
  }
}
</script>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

我想我理解您的问题,但是如果我弄错了,请告诉我。您的问题有点广泛,并且您想做什么有很多解决方案。我可以通过以下几种方法来解决这个问题。

方法1:一次全部使用;根据需要显示和隐藏

As shown here,使用辅助函数include分解HTML文件进行开发,然后在运行应用程序时将它们拉在一起。

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

内容片段位于其自己的.html文件中,并为最外面的HTML元素提供一个不同的ID。

<section id="IT_Security_And_Vulnerability" class="hidden">
  <h1>IT Security & Vulnerability</h1>
  <p> ...
  </p>
</section>

将所有这些html文件打印到返回给用户的主html文件中。 主要的html文件片段:

<style>
.hidden {
  display: none;
}
</style>
<?= include("#IT_Security_And_Vulnerability") ?>
<?= include("#Information_Security_Data_Only") ?>
<!-- etc -->

<script>
const body = document.querySelector("body"); // this just grabs the sidebar body
body.addEventListener("click", handleClick);
function handleClick(clickEvent) {
  if (clickEvent.target.tagname != "A") { // this listener only for clicking links
    return;
  }
  clickEvent.target.preventDefault(); // you may or may not want/need this to prevent browser from trying to actually navigate with your <a> tag
  const id = clickEvent.target.getAttribute("href");
  const sections = document.querySelectorAll("section");
  for (let i = 0, l = sections.length; i < l; ++i) { // hide everyone
    sections[i].classList.add("hidden");
  }
  document.querySelector(id).classList.remove("hidden"); // show the selected one
}
</script>

方法2:根据需要使用google.script.run获取内容

仍然使用服务器上的include功能,可以按需提供内容。

...
<section class="contentContainer">
<!-- populated by js -->
</section>
...
<script>
const body = document.querySelector("body"); // this just grabs the sidebar body
body.addEventListener("click", handleClick);

function refresh(html) {
  const contentContainer = document.querySelector(".contentContainer");
  contentContainer.innerHTML = html;
}

function handleClick(clickEvent) {
  if (clickEvent.target.tagname != "A") {
    return;
  }
  clickEvent.target.preventDefault(); // you may or may not want/need this.
  const reference = clickEvent.target.getAttribute("href");
  google.script.run.withSuccessHandler(refresh).include(reference);
}
</script>