查询作为小部件数据源

Query as Widget Datasource

我正在尝试将小部件的数据源定义为查询结果,但不确定是否可行。

我正在使用 SQL 视图和 Table,我想显示来自视图的 table 上的 ID 值.

function queryValue(source, model, key){
  console.log("source " + source);
  app.datasources[model].query.filters.id._equals = source;
  app.datasources[model].load(function () {
    console.log(app.datasources.users.items[0][key]);
    return app.datasources.users.items[0][key];
  });
  app.datasources[model].query.clearFilters();
}

这样称呼它:

queryValue(@datasource.item.[the_id], "[the_SQLView_Datasouce]", "[the_field_i_want]");

在这种情况下,小部件是 table,因此函数会重复表格中的项目数量

问题是要么我多次得到相同的结果,要么第一个不起作用!

第二个问题是结果没有覆盖要显示的小部件文本。

这是一个非常简单的功能,我确实找到了一些解决方法,但没有找到数据源功能,而且它们工作得太慢,有什么建议吗?是否可以用数据源做这样的事情?

如果我理解正确的话,你可能想在服务器端进行查询。发布的示例代码的问题在于,它会在任何加载 return 之前多次触发单个数据源上的加载。完成此操作后,数据源仅加载其中一次加载的结果,我相信是最后一次。因此,您可能看到的是您对所有回调所做的最后一次查询的结果。

所以您的代码应该是服务器端脚本,并且应该类似于:

function queryValue(source, model, key){
  console.log("source " + source);
  var query = app.models.newQuery();
  query.filters.id._equals = source;
  var results = query.run;
  return results[0].key;
}

(凭记忆写的,如有错误请见谅。)

按照 Devin 的建议:

Front-end

/*****************************************************************************
Front-end function that calls the querying function @queryValue(source, model, key) in controller_TransformId
@source => the field ID to transform to label
@model => the model name to be queried
@key => the label to be acquired with the query
@wwidget => the widget making the request
This function works as a model to manage the transactions between the 
controller at the backend and the view.  
******************************************************************************/
function buildTransformID(source, model, key, widget){ 
  google.script.run.withSuccessHandler(
    function successHandler(expectedValue){
      widget.text = expectedValue;})
  .withFailureHandler(
    function failureHandler(){
      widget.text = "undefined";})
  .queryValue(source, model, key);
}

Back-end

/*****************************************************************************
Back-end function that queries the database
@source => the field ID to transform to label
@model => the model name to be queried
@key => the label to be acquired with the query    
This function works works as a controller to query the database from the backend    ******************************************************************************/
function queryValue(source, model, key){ 
  var query = app.models[model].newQuery();
  query.filters.id._equals = source;
  var results = query.run();
  console.log("CONTROLLER return :" + results[0][key]);
  return results[0][key];
}

是否必须通过widget.text值? successHandler 回调是异步的,所以常规 returns 只会给我 nulls