Querying data from BigQuery and return a single data into Google AppMaker

末鹿安然 提交于 2020-01-06 06:19:44

问题


From my previous question here, I think it is the problem to my server-script function that queries the data from BigQuery. Following are my code to queries HOD email base on requester email address.

//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();
}

The weird thing is that when I console.log data it return the HOD email address. But the function won't return the data result when ever I call the BigQueryGetEmail function. Following is the caller function:

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);  
}

The widget TextBox2 doesn't reflect any data nor error which is I believe it is because of the BigQueryGetEmail function doesn't return any data.

Can anyone guide me on the correct way how to return the query result when I call the BigQueryGetEmail function? had tried several ways but still stuck.

来源:https://stackoverflow.com/questions/54192453/querying-data-from-bigquery-and-return-a-single-data-into-google-appmaker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!