【问题标题】:Appending rows from a Google Sheet to an existing table in Big Query using Google Apps Script使用 Google Apps 脚本将行从 Google 表格附加到 Big Query 中的现有表
【发布时间】:2018-04-15 07:00:36
【问题描述】:

我有一个 Google 表格,我想从中选取一些单元格并将它们作为行附加到 Big Query 中已经存在的表格中。我编写了以下代码,运行时没有任何错误,但是当我检查 BQ 中的表时,它没有更新。

function myFunction() {

  var projectId = 'projectId'; 
  var datasetId = 'datasetId';
  var tableId = 'tableId';

  var fileId = 'fileId';
  var ss = SpreadsheetApp.openById(fileId);
  var sheet = ss.getSheets()[0];
  var range = sheet.getRange(21,2,sheet.getLastRow()-21,7);
  var values = range.getValues();
  var rowsCSV = values.join("\n");
  Logger.log(rowsCSV);

  function convertValuesToRows(rowsCSV) {
    var rows = [];  
    var headers = rowsCSV[0]; 


    for (var i = 1, numColumns = rowsCSV.length; i < numColumns; i++) {
      var row = BigQuery.newTableDataInsertAllRequestRows();
      row.json = rowsCSV[i].reduce(function(obj, value, index) {
        obj[headers[index]] = value;
        return obj
      }, {});
      rows.push(row);
    }; 
    return rows;
    }

  function bigqueryInsertData(rowsCSV, tableId) {
    var insertAllRequest = BigQuery.newTableDataInsertAllRequest();
    insertAllRequest.rows = convertValuesToRows(rowsCSV);     
    var response = BigQuery.Tabledata.insertAll(insertAllRequest, projectId, datasetId, tableId);
    if (response.insertErrors) {
      Logger.log(response.insertErrors);
    }
  }

}

我需要进行哪些更改才能使其正常工作?

【问题讨论】:

  • 你现在从不打电话给bigqueryInsertData

标签: google-apps-script google-sheets google-bigquery


【解决方案1】:

将 rowsCSV 转换为 blob 允许我在 bigqueryInsertData 函数中使用 getDataAsString() 方法。我还手动命名了标题以匹配 BigQuery 中表的列名。最终代码如下所示,运行无误,BQ表更新成功。

function myFunction() {

      var projectId = 'production-1077'; 
      var datasetId = 'alex_test';
      var tableId = 'Total_Jobs_Reporting_Table';

      var fileId = 'fileId';
      var ss = SpreadsheetApp.openById(fileId);
      var sheet = ss.getSheets()[0];
      var range = sheet.getRange(21,2,sheet.getLastRow()-21,7);
      var values = range.getValues();
      var rowsCSV = values.join("\n");
      var data = Utilities.newBlob(rowsCSV, 'application/octet-stream'); 

      function convertValuesToRows(data) {
        var rows = [];  
        var headers = ["name1","name2","name3","name4","name5","name6","name7"] ; 


        for (var i = 1, numColumns = data.length; i < numColumns; i++) {
          var row = BigQuery.newTableDataInsertAllRequestRows();
          row.json = data[i].reduce(function(obj, value, index) {
            obj[headers[index]] = value;
            return obj
          }, {});
          rows.push(row);
        }; 
        return rows;
     }

     function bigqueryInsertData(data, tableId) {
       var insertAllRequest = BigQuery.newTableDataInsertAllRequest();
       insertAllRequest.rows = convertValuesToRows(data);     
       var response = BigQuery.Tabledata.insertAll(insertAllRequest, projectId, datasetId, tableId);
       if (response.insertErrors) {
         Logger.log(response.insertErrors);
       }
    }

    bigqueryInsertData(Utilities.parseCsv(data.getDataAsString()), tableId);
}

【讨论】:

    猜你喜欢
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2015-06-25
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    相关资源
    最近更新 更多