在使用菜单项时,如何在Google Apps脚本中的函数之间传递变量?

时间:2017-08-02 19:28:48

标签: google-apps-script google-sheets

我已经能够使用下面的onOpen()代码成功构建自定义菜单。

我希望的结果是使用我的函数setSheet()设置一个用户单击菜单项以选择给定工作表的特定工作表。

问题是setSheet功能实际上并没有做任何事情,因为我无法弄清楚如何传递菜单项' name'到另一个功能。

我不确定在这种情况下我做错了什么?我很感激您提供的任何帮助。谢谢!

这是我到目前为止的工作代码:

function onOpen(e) {
 subMenus = [];
 ss = SpreadsheetApp.getActiveSpreadsheet();
 allsheets = ss.getSheets();
 for ( i = 0; i < allsheets.length; ++i){
   sheet = allsheets[i]
   subMenus.push({name:sheet.getName(), functionName: 'setSheet'})
  }
  SpreadsheetApp
  .getActiveSpreadsheet()
  .addMenu("Pick A Spreadsheet", subMenus)
}

function setSheet() {
  ss = SpreadsheetApp.getActiveSpreadsheet();
  name = ss.getss.getSheetByName()
  ss.setActiveSheet(name)
}

此外,我通过使用下面的代码做了一个解决方法,但我想保存用户直接在提示中输入值的需要,因为他们可能不知道工作表名称的值。

function onOpen(e) {
  subMenus = [{name:"Go To Sheet", functionName: 'setSheet'}];
  SpreadsheetApp
   .getActiveSpreadsheet()
   .addMenu("Pick A Spreadsheet", subMenus)
}

function setSheet() {
  ss = SpreadsheetApp
  active = ss.getActiveSpreadsheet()
  ui = ss.getUi();
  response = ui.prompt("What Sheet Do You Need?")
  active.setActiveSheet(active.getSheetByName(response.getResponseText()))
}

1 个答案:

答案 0 :(得分:3)

从菜单调用的函数不能有参数,除了 - 某种方式 - 没有真正的替代方法 - 硬编码值或至少引用存储在其他地方的变量(例如,在属性服务中)。

但无论如何,我发现使用菜单选择的纸张不是最佳解决方案,至少不是非常用户友好。

这就是为什么我建议您尝试使用带有html界面的侧边栏的这个版本。 您必须在项目中创建3个文件,1个脚本文件和2个html文件。

代码在3个单独的项目下面(很长,我知道......这里是a link到你可以复制使用的只读演示)

下面的

是结果的截屏。

code.gs

function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu("Utilities")
  .addItem('authorize script', 'autorize')
  .addToUi();
  showSidebar();
}

function autorize(){
  showSidebar();
}

function showSidebar() {
  var ui = HtmlService.createTemplateFromFile('Sidebar')
      .evaluate()
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setTitle('Sheet selector');
  SpreadsheetApp.getUi().showSidebar(ui);
}


function createList(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var list = [];
  var sh, sheetName;
  for(var s=0;s<ss.getNumSheets();s++){
    sh = ss.getSheets()[s];
    sheetName = sh.getName();
    list.push(sheetName);
  }
  var message = ''; 
  var color;
  message+= '<table>';
  message+='<tr valign="top" cellpadding=5>'
  for(var n=0;n<list.length;n++){
      color="#000"
    //Logger.log('list[n] = '+list[n]+' is in '+alreadyCreated.indexOf(list[n])+' position');
    message+='<tr><td align="center">&nbsp'+(n+1)+'&nbsp</td><td>'+
      '<input type="button" id="button'+n+'" value="'+list[n]+'" style="color:'+color+';" onclick="selectSheet(\''+n+'\')"/></td></tr>';
  }
  message+='</table>';
  return message;
}

function showSheetGS(sheetNumber){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.getSheets()[sheetNumber].activate();
}

Sidebar.html

<!-- Use a templated HTML printing scriptlet to import common stylesheet. -->
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
<body>
<!-- Below is the HTML code that defines the sidebar element structure. -->
<div class="sidebar branding-below">
<!-- The div-table class is used to make a group of divs behave like a table. -->
<h3> Listof available sheets,<br><span style="color:#0A0"> green color </span> if already selected</h3>
  <div id="sidebarList">
  <br><br><br>...please wait a second ...<br>
  <p>If nothing happens please authorize the script using the menu.</p> 
  </div>

<!-- Enter sidebar bottom-branding below. -->
  <div class="sidebar bottom">
  <img alt="Add-on logo" class="logo" width="25"
      src="http://insas.cluster006.ovh.net/serge/apps-script_2x.png">
  <span class="gray branding-text">sheet selector demo  V1.0<br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp© SG 2017</span>
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
  /**
   * Run initializations on sidebar load.
   */
populateEncodeLocal();

function populateEncodeLocal(){
   //console.log('populateEncode');
   google.script.run.withSuccessHandler(populateList).createList();
}

function selectSheet(sheetNumber){
  console.log(sheetNumber);
  google.script.run.withSuccessHandler(showSheet(sheetNumber)).showSheetGS(sheetNumber);
}
function showSheet(n){
  console.log("showSheet"+n);
  $('#button'+n).css('font-size','12pt').css('color','#0A0');
}

function populateList(data){
   //console.log(data);
   $('#sidebarList').html(data);
}
</script>

Stylesheet.html

<!-- This CSS package applies Google styling; it should always be included. -->
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<style>
html, body {
    max-width: 100%;
    overflow-x: hidden;
}
#sidebarList {
  height:600px;
  overflow:auto;
  }
table{
  display:block;
  background-color:#EEE;
  border:1px solid  #666666;
  border-spacing:2px;
  border-collapse:collapse;
  font-family:arial,sans;
  font-size:9pt;
  }
th {
  border-bottom: 0px solid #acacac;
  font-weight: normal;
  padding: 1px 1px 0;
  text-align: left;
}
td {
  border-bottom: 0px solid #ebebeb;
  padding: 1px 0;
}
input[type=button] {
    color:#08233e;
    font:11pt Helvetica,Arial,sans;
    padding:3px;
    background:#FFE;
    border:1px solid #AAA;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
    border-radius:5px;
    cursor:pointer;
    width:270px;
    white-space: normal;
}
input[type=button]:hover {
    border:1px solid #000;
}
label {
  font-weight: bold;
}
.branding-below {
  bottom: 3px;
  top: 0px;
  padding-left:0px;
}
.branding-text {
  left: 7px;
  position: relative;
  top: 3px;
}
.logo {
  vertical-align: middle;
}
</style>

enter image description here