我有一张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);
}
}
}
我需要更改什么才能让它发挥作用?
答案 0 :(得分:0)
将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);
}