如何从HTML模板调用Google Apps脚本封装函数



嗨,我在Google表上使用Google App脚本,我想整理我的代码,因此我尝试封装一些功能。但是,我似乎无法弄清楚如何从HTML模板中调用封装功能。

有人知道是否可以使用html模板使用google.script.run调用封装功能?

示例:

code.gs

function onOpen(e){
  var menu = SpreadsheetApp.getUi().createMenu('Document Routing')
    .addItem('View/Edit My Routes', 'ViewMyRoutes')
    .addToUi();
}
function ViewMyRoutes() {
  var html = HtmlService.createHtmlOutputFromFile('MyRoutes')
      .setTitle('My Routes')
      .setWidth(300);
  
  SpreadsheetApp.getUi()
      .showSidebar(html);
}

user.gs

var User = function(){
  return {
    GetMyRoutes: function (){
      var userProperties = PropertiesService.getUserProperties(); 
      
      var routeData = userProperties.getProperty("SpreadsheetRoutes");
      
      return routeData;
    }
  };
}();

sample.html

<h1>Edit Route</h1>
<select id="EditRoute" onchange="LoadSelectedRoute()"></select>
<div id='divRoutes'></div>
<script>
  function LoadEditDropDown(){
    
    google.script.run
      .withFailureHandler(onFailure)
      .withSuccessHandler(OnGetRoutesSuccess)
      .User.GetMyRoutes();
  
    function OnGetRoutesSuccess(scriptProperties){  
    
      //do stuff with scriptProperties here
    }
  }
  
  LoadEditDropDown();
</script>

这怎么样?在这种情况下,我使用这种方式。但是我不知道这是最好的方法。对不起。我担心这可能是您想要的相反方式。

1。请添加此功能

function UserGetMyRoutes() {
    return User.GetMyRoutes();
}

2。请修改google.script.run如下

来自:

google.script.run
  .withFailureHandler(onFailure)
  .withSuccessHandler(OnGetRoutesSuccess)
  .User.GetMyRoutes();

to:

google.script.run
  .withFailureHandler(onFailure)
  .withSuccessHandler(OnGetRoutesSuccess)
  .UserGetMyRoutes();

最新更新