背景: 我尝试从Google表格上传单个数据行并将其附加到BigQuery表格。
方法:我一直在使用https://developers.google.com/apps-script/advanced/bigquery来执行此操作,但不是像示例那样使用数据文件,而是使用自己的工作表来处理特定行的数据:
var file = SpreadsheetApp.getActiveSpreadsheet();
var currentSheet = file.getSheetByName(name);
var lastRow = currentSheet.getLastRow()
var lastC = currentSheet.getLastColumn()
var rows = currentSheet.getRange(2,1,1,lastC).getValues();
"行"是要导入BQ的数据行。我已经尝试了很多东西,根据另一个StackOverflow question," rowsCSV"使2D数组值为CSV。
var rowsCSV = rows.join("\n");
var data = rowsCSV.getBlob().setContentType('application/octet-stream');
问题:每次运行该函数时,都会收到错误"无法在对象Blob中找到函数getBlob。 "或者,"无法将数组转换为(类)[] []"或"无法在对象中找到函数getBlob Tue May 16 2017 00:00:00 GMT + 0200(CEST),58072.4 ,,,,,,,,,,, test",其中最后一位(" Tue May ..")是该行的实际数据。
我在这里做错了什么?
答案 0 :(得分:3)
数组没有getBlob
方法。您必须使用Utilities.newBlob()
从字符串中获取blob。您可以在同一here
var rowsCSV = rows.join("\n");
var blob = Utilities.newBlob(rowsCSV, "text/csv")
Logger.log(blob.getDataAsString())
var data = blob.setContentType('application/octet-stream');
等效地你可以这样做
var rowsCSV = rows.join("\n");
var data = Utilities.newBlob(rowsCSV, 'application/octet-stream')
答案 1 :(得分:0)
基于@JackBrown给出的更正,我已经编辑了代码,但是无法将数据推送到Big Query。下面的代码创建表,但不要推送值。
/**
* Loads a CSV into BigQuery
*/
function loadCsv() {
// Replace this value with the project ID listed in the Google
// Cloud Platform project.
var projectId = 'master-ad-data';
// Create a dataset in the BigQuery UI (https://bigquery.cloud.google.com)
// and enter its ID below.
var datasetId = 'DataImportFromSpreadsheet';
// Sample CSV file of Google Trends data conforming to the schema below.
// https://docs.google.com/file/d/0BwzA1Orbvy5WMXFLaTR1Z1p2UDg/edit
var csvFileId = '17kYH6hP2RlsFeUmwM1v6WOgm2FKrwLTXWDhA2ZLISN8';
var name = 'Sheet1';
// Create the table.
var tableId = 'pets_' + new Date().getTime();
var table = {
tableReference: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
schema: {
fields: [
{name: 'CampaignLabels', type: 'STRING'},
{name: 'ImpressionShare', type: 'INTEGER'}
]
}
};
table = BigQuery.Tables.insert(table, projectId, datasetId);
Logger.log('Table created: %s', table.id);
var file = SpreadsheetApp.getActiveSpreadsheet();
var currentSheet = file.getSheetByName(name);
var lastRow = currentSheet.getLastRow()
var lastC = currentSheet.getLastColumn()
var rows = currentSheet.getRange(2,1,1,lastC).getValues();
var rowsCSV = rows.join("\n");
Logger.log("Check This"+" "+rowsCSV);
var data = Utilities.newBlob(rowsCSV, 'application/octet-stream')
// Create the data upload job.
var job = {
configuration: {
load: {
destinationTable: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
skipLeadingRows: 1
}
}
};
job = BigQuery.Jobs.insert(job, projectId, data);
Logger.log('Load job started. Check on the status of it here: ' +
'https://bigquery.cloud.google.com/jobs/%s', projectId);
}
答案 2 :(得分:0)
对于其他人,杰克·布朗的答案是正确的,您只需要更改
var rows = currentSheet.getRange(2,1,1,lastC).getValues();
到
var rows = currentSheet.getRange(2,1,lastRow,lastC).getValues();