【发布时间】:2016-08-26 07:15:50
【问题描述】:
我编写了一个独立脚本,用于将数据从 csv 文件加载到 bigquery,但我想知道如何将数据从电子表格加载到 bigquery。这是我将数据从 csv 加载到 bigquery 的代码。我想知道在哪里对 google 电子表格而不是 csv 文件进行更改。
function loadCsv() {
// Replace this value with the project ID listed in the Google
// Developers Console project.
var projectId = '775034665452';
var datasetId = 'DEV';
// Sample CSV file of Google Trends data conforming to the schema below.
// https://docs.google.com/file/d/0BwzA1Orbvy5WMXFLaTR1Z1p2UDg/edit
var csvFileId = '0BwzA1Orbvy5WMXFLaTR1Z1p2UDg';
// Create the table.
var tableId = 'pets_' + new Date().getTime();
var table = {
tableReference: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
schema: {
fields: [
{name: 'week', type: 'STRING'},
{name: 'cat', type: 'INTEGER'},
{name: 'dog', type: 'INTEGER'},
{name: 'bird', type: 'INTEGER'}
]
}
};
table = BigQuery.Tables.insert(table, projectId, datasetId);
Logger.log('Table created: %s', table.id);
// Load CSV data from Drive and convert to the correct format for upload.
var file = DriveApp.getFileById(csvFileId);
var data = file.getBlob().setContentType('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);
}
【问题讨论】:
标签: google-apps-script google-sheets google-bigquery