应用程序脚本/制造商和大查询

时间:2018-06-01 10:32:19

标签: arrays google-apps-script google-bigquery google-app-maker

我有一个谷歌应用程序(应用程序制造商),我在其中编写以下代码:

    function runQuery() {
  var projectId = 'projekte-123425512';
  var request = {
    query: 'SELECT title FROM [bigquery-public-data:samples.wikipedia]  where title contains "olimpic" LIMIT 100'
  };
  var queryResults = BigQuery.Jobs.query(request, projectId);
  var jobId = queryResults.jobReference.jobId;
  var names = queryResults.schema.fields.map(function(field){ return field.name; });
  return queryResults.rows.map(function(row) {
    var obj = {};
    for( var i = 0, len = names.length; i < len; ++i ) {
      obj[names[i]] = row.f[i].v;
    }
    return obj;
  });
}

所以我基本上尝试从Big Query中获取一些数据并将其存储在数组中。后来我想把它作为我的应用程序中的计算模型和数据源:

enter image description here

我已经尝试过并在记录器中获得结果:

enter image description here

有效!但只有在app脚本调试器中,我才想测试整个应用程序,我得到以下错误:

  

函数queryRecords必须返回一个记录数组,但该数组包含一个不是记录的元素。错误:函数queryRecords必须返回一个记录数组,但该数组包含一个不是记录的元素。

修改

我更新了我的代码

    function runQuery() {
  var projectId = 'nifty-stage-155512';
  var request = {
    query: 'SELECT title FROM [bigquery-public-data:samples.wikipedia]  where title contains "olimpic" LIMIT 100'
  };
  var queryResults = BigQuery.Jobs.query(request, projectId);
  var jobId = queryResults.jobReference.jobId;
  var names = queryResults.schema.fields.map(function(field){ return field.name; });


  //var records = [];  
  return queryResults.rows.map(function(row) {
  var record = app.models.Test.newRecord();
  for (var i = 0, len = names.length; i < len; ++i) {
    // Calculated model should contain correspondent fields
    // all non-defined fields will be ignored
    record[names[i]] =  (row.f[i].v);
  }

  return record;
});
}

它现在可以正常工作,但我仍然没有数据进入我的网格:

enter image description here

在网格配置或数据源中是否存在我遗漏的内容?

enter image description here

1 个答案:

答案 0 :(得分:1)

App Maker不允许通过其数据源返回任意对象。所有结果都应该是强类型的:

...

return queryResults.rows.map(function(row) {
  var record = app.models.CalcModelName.newRecord();

  for (var i = 0, len = names.length; i < len; ++i) {
    // Calculated model should contain correspondent fields
    // all non-defined fields will be ignored
    record[names[i]] = row.f[i].v;
  }

  return record;
});

以下是一些供参考的样本 https://developers.google.com/appmaker/samples/calculated-model/ https://developers.google.com/appmaker/samples/jdbc/