从BigQuery查询数据并将单个数据返回到Google AppMaker

时间:2019-01-15 03:48:52

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

从上一个问题here开始,我认为这是服务器脚本函数从BigQuery查询数据的问题。以下是我根据请求者的电子邮件地址查询HOD电子邮件的代码。

//To fetch requester's HOD email address
function BigQueryGetEmail(emailRequester){

  // Replace this value with the project ID listed in the Google
  // Cloud Platform project.
  var projectId = 'xxxxxx-xxx-stg';

  query = '#standardSQL \n SELECT SUPV_EMAIL_A FROM `xxxxx-xxx-stg.xxxxx.vw_HOD_email` WHERE EMP_EMAIL_A = "' + emailRequester + '";';

  var request = {
    query: query,
  };
  var queryResults = BigQuery.Jobs.query(request, projectId);
  var jobId = queryResults.jobReference.jobId;

  // Check on status of the Query Job.
  var sleepTimeMs = 500;
  while (!queryResults.jobComplete) {
    Utilities.sleep(sleepTimeMs);
    sleepTimeMs *= 2;
    queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId);
  }

  // Get all the rows of results.
  var rows = queryResults.rows;
  while (queryResults.pageToken) {
    queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId, {
      pageToken: queryResults.pageToken
    });
    rows = rows.concat(queryResults.rows);
  }

  // Append the results.
  var data = new Array(rows.length);
  for (var i = 0; i < rows.length; i++) {
   var cols = rows[i].f;
    data[i] = new Array(cols.length);
    for (var j = 0; j < cols.length; j++) {
      data[i][j] = cols[j].v;
    }
  }
  console.log('HOD email: ' + data);
  return data.toString();
}

奇怪的是,当我console.log数据时,它返回HOD电子邮件地址。但是,当我调用BigQueryGetEmail函数时,该函数不会返回数据结果。以下是调用者函数:

function getHOD(){
  var ownerEmail = app.pages.Main.descendants.TextBox1.value;
  var HODemailfield = app.pages.Main.descendants.TextBox2.value;

  function successHandler(email){
  //assign email value to widget;
    HODemailfield = email;
  }

google.script.run.withSuccessHandler(successHandler).BigQueryGetEmail(ownerEmail);  
}

小部件TextBox2既不反映任何数据,也不反映错误,我相信这是由于BigQueryGetEmail函数不返回任何数据。

有人可以在我调用BigQueryGetEmail函数时以正确的方式指导我如何返回查询结果吗?尝试了几种方法,但仍然卡住了。

0 个答案:

没有答案