通过Apps脚本将Google表格csv导入BigQuery时出错

时间:2017-04-10 05:11:27

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

我正在尝试使用以下Google Apps脚本将Google表格从表格导入BigQuery。使用正确的架构/位置创建表,但数据不会加载到其中。给出的错误是:

“CSV表引用列位置1,但从位置开始的行:9只包含1列。(错误代码:无效)”

列A-E,第1-8行的单元格包含数据,其余为空。

function loadCsv() {
// Replace this value with the project ID listed in the Google
// Cloud Platform project.
var projectId = 'xxxxxxx';

// Create a dataset in the BigQuery UI (https://bigquery.cloud.google.com)
// and enter its ID below.
var datasetId = 'zzzzzzzz';

// Sample CSV file of Google Trends data conforming to the schema below.
// https://docs.google.com/file/d/0BwzA1Orbvy5WMXFLaTR1Z1p2UDg/edit
var csvFileId = 'yyyyyyyyyyyyyy';

// Create the table.
var tableId = 'new_' + new Date().getTime();

var table = {
tableReference: {
  projectId: projectId,
  datasetId: datasetId,
  tableId: tableId
},
schema: {
  fields: [
    {name: 'channelGrouping', type: 'STRING'},
    {name: 'medium', type: 'STRING'},
    {name: 'date', type: 'STRING'},
    {name: 'sessions', type: 'INTEGER'},
    {name: 'pageviews', 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);
}

如果有人能给我一个暗示为什么会出现这种情况,我会非常感激。 谢谢,

编辑:以下主题中的答案解决了我的问题。基本上,将GSheet作为CSV存在一个问题,而这个解决方案取代了GSheet中的一个范围并使其成为CSV输入。

Google sheets upload to bigquery: Bad character

0 个答案:

没有答案