BigQuery CSV上传中的双引号数据错误

时间:2018-09-26 16:11:40

标签: google-apps-script google-bigquery

将数据从Google表格上传到BigQuery时,“注释”字段包含如下数据

function pushToBQ(projectId, datasetId, tableId) {
  var fileId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

  var jobSpec = { configuration: {
      load: {
        destinationTable: {
          projectId: projectId,
          datasetId: datasetId,
          tableId: tableId
        },
        allowJaggedRows: true,
        writeDisposition: 'WRITE_TRUNCATE',
        allowQuotedNewlines: true,
        schema: {
          fields: [ 
            {name: 'User_id', type: 'STRING'},
            {name: 'email', type: 'STRING'},
            {name: 'Comments', type: 'STRING'},
          ] 
        }
      }
    }
  };

  var spreadsheet = SpreadsheetApp.openById(fileId);
  var MAX_ROWS = 50000;
  var sheet = spreadsheet.getSheetByName("xyz");
  var data = sheet.getDataRange().getValues();
  var csvdata = "";
  for (var row = 1; row < data.length && row < MAX_ROWS + 1; row++) {
    for (var col = 0; col < data[row].length; col++) {
      var cell = data[row][col].toString();
      if (cell.indexOf(",") != -1) {
        csvdata += "\"" + cell + "\"";
      } else {
        csvdata += cell;
      }

      if (col < data[row].length - 1) {
         csvdata += ",";
      }
    }
    csvdata += "\r\n";

  }

  var data = Utilities.newBlob(csvdata, "application/octet-stream");
  BigQuery.Jobs.insert(jobSpec, projectId, data);
}

function daily_upload(){
  pushToBQ("dev", "sampledataset",'sampletable');
}

请提供给我解决方案,我想按原样上传评论,而不用空格替换。

1 个答案:

答案 0 :(得分:1)

除了从工作表中创建CSV,然后将该CSV导入到BigQuery中之外的其他选项:

  1. Read from your sheet in BigQuery by creating an 'external table'(要求数据在工作表的第一个标签中)。然后,您可以从该外部表复制到真正的bigquery表。

  2. 使用BigQuery.Tabledata.insertAll()将数据作为JSON插入表中,而不必格式化CSV。