可以使用HTML(侧边栏)调用库脚本吗?

时间:2014-12-16 13:13:30

标签: html google-apps-script google-sheets sidebar

我在Spreadsheets上用HTML创建了一个侧边栏,并且一切都运行得很好,但是当移动到维护所有代码的库时,它停止工作,侧边栏使用的功能现在未定义,我怎么能调用函数库中的“FuncoesOrcamentoV2”直接来自侧边栏?将代码复制到每个电子表格是不切实际的,其中有几十个。

示例:

form.html

<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" /> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>

<label for="autocomplete">Select a programming language: </label>
<input id="autocomplete" disabled="true" value="Carregando..." clss="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content">
<input type="button" value="Close" onclick="escrever()" />
<script>
function autoCompletar( comps ){
$( "#autocomplete" ).removeAttr("disabled").removeAttr("value");
    $( "#autocomplete" ).autocomplete({
    source: comps
    });
}
google.script.run.withSuccessHandler( autoCompletar ).arrayObjetos(); //arrayObjetos cannot be called
</script>

code.gs - &gt;在图书馆FuncoesOrcamentoV2

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');
}

function arrayObjetos(){
var composicoes = FuncoesOrcamentoV2.composicoesObject( ),
     arrayFinal = [];

  for( comp in composicoes )
    arrayFinal.push( comp );

  return arrayFinal;
}

1 个答案:

答案 0 :(得分:0)

不是最优雅的方式,但使用eval()的形式适合我的情况:

每张纸上的code.gs:

function funcoesOrcamento( funcao, args ){
  if(args)
    args = args.split("\!|");
  else
    args = [];

  return FuncoesOrcamentoV2[ funcao ].apply(this, args);
}

并在HTML侧栏上:

google.script.run.withSuccessHandler( salvaNivelColuna ).funcoesOrcamento( "nivelColuna" );
google.script.run.withSuccessHandler( autoCompletar ).funcoesOrcamento( "arrayObjetos" );