javascript 一个应用程序脚本示例,用于从谷歌电子表格中获取大查询数据。更多信息:http://wp.me/pB1lQ-19i

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 一个应用程序脚本示例,用于从谷歌电子表格中获取大查询数据。更多信息:http://wp.me/pB1lQ-19i相关的知识,希望对你有一定的参考价值。

/**
* Fetching data from BigQuery and present it in our sheet
* Author: Ido Green
* Date: 14/12/2013
* 
* See: http://wp.me/pB1lQ-19i
* Misc: https://developers.google.com/bigquery/
*/

//
// Build and run the query: Get the top 30 longest works of Shakespeare
//
function runQuery() {
  // Replace this value with your Google Developer project number (It is really a number. 
  // Don't confuse it with an alphanumeric project id)
  var projectNumber = 'Put your project number here';
  if (projectNumber.length < 1) {
      var errMsg = "You forgot to set a project number - So no BQ for you!";
      Logger.log(errMsg);
      Browser.msgBox(errMsg);
      return;
  }
  
  var sheet = SpreadsheetApp.getActiveSheet();
  var sql = 'select TOP(word, 30), COUNT(*) as word_count from publicdata:samples.shakespeare WHERE LENGTH(word) > 10;';
  var queryResults;

  // Inserts a Query Job
  try {
    var queryRequest = BigQuery.newQueryRequest();
    queryRequest.setQuery(sql).setTimeoutMs(100000);
    queryResults = BigQuery.Jobs.query(queryRequest, projectNumber);
  }
  catch (err) {
    Logger.log(err);
    Browser.msgBox(err);
    return;
  }
  
  // Check on status of the Query Job
  while (queryResults.getJobComplete() == false) {
    try {
      queryResults = BigQuery.Jobs.getQueryResults(projectNumber, queryJob.getJobReference().getJobId());
    }
    catch (err) {
      Logger.log(err);
      Browser.msgBox(err);
      return;
    }
  }
  
  // Update the amount of results
  var resultCount = queryResults.getTotalRows();
  var resultSchema = queryResults.getSchema();
  
  var resultValues = new Array(resultCount);
  var tableRows = queryResults.getRows();
  
  // Iterate through query results
  for (var i = 0; i < tableRows.length; i++) {
    var cols = tableRows[i].getF();
    resultValues[i] = new Array(cols.length);
    // For each column, add values to the result array
    for (var j = 0; j < cols.length; j++) {
      resultValues[i][j] = cols[j].getV();
    }
  } 

  // Update the Spreadsheet with data from the resultValues array, starting from cell A1
  sheet.getRange(1, 1, resultCount, tableRows[0].getF().length).setValues(resultValues);  
  Browser.msgBox("Yo yo! We are done with updating the results");
}

//
// Insert our customize menu item
//
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: 'Run Query', functionName: 'runQuery'} ];
  sheet.addMenu('BigQuery Example', menuEntries);
};

以上是关于javascript 一个应用程序脚本示例,用于从谷歌电子表格中获取大查询数据。更多信息:http://wp.me/pB1lQ-19i的主要内容,如果未能解决你的问题,请参考以下文章

javascript 用于从书签中加载外部脚本的简单代码段。

javascript [False Blocker]用于从数组#vanilla #script中删除所有错误值的脚本

javascript 提供用于从React-Native WebView发送和接收消息的示例实现(使用postMessage / onMessage WebVie)

用于从文本文件中提取数据的 Shell 脚本

Selenium:如何点击javascript按钮

如何在 Ant 构建脚本中为 javascript 和 css 使用 YUI Compressor