【问题标题】:How to call Google Apps Script encapsulated function from html template如何从 html 模板调用 Google Apps 脚本封装的函数
【发布时间】:2017-08-30 14:56:41
【问题描述】:

您好,我在 Google Sheet 上使用 Google App Scripts,我想组织我的代码,所以我尝试封装一些函数。但是,我似乎无法弄清楚如何从 html 模板中调用封装的函数。

有谁知道是否可以使用 google.script.run 从 html 模板调用封装函数?

例子:

代码.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);
}

用户.gs

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

示例.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>

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

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

    1。请将此功能添加到 GAS

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

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

    发件人:

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

    收件人:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 2015-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多