我有一个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);
    }
  }
}我需要改变什么才能让它起作用?
发布于 2017-11-03 09:29:22
将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);
}https://stackoverflow.com/questions/47080633
复制相似问题