如何在BigQuery uisng google appscript中导入csv日期格式

时间:2017-08-27 06:09:16

标签: python csv date google-apps-script google-bigquery

我的数据存储在CSV格式为2017-08-07,我的日期列定义为BQ中的字符串字段。当我尝试在BigQuery中导入所有数据时,所有日期数据都已转换为2017年8月7日星期一00:00:00 GMT + 0800(香港时间)。我想导入2017-08-07格式的所有日期范围,但它正在使用上述格式进行转换。下面是我的CSV脚本,用于在BQ上传数据:

function test() {

  var projectId = 'test1';
  var datasetId = 'YoutubeNG';
     var yestDate = new Date();
      yestDate.setDate(yestDate.getDate()-2);
   var formattedDate = Utilities.formatDate(yestDate, "GMT", "yyyy-MM-dd");

  var tableId = 'Youtube_Keystats_NG';
  var fileId = '1y4xTZ4g1c08D8NOU0O-SWsi-5uyi9ukOuKLD5yz03cY';

  // Define our load job.
  var jobSpec = {
    configuration: {
      load: {
        destinationTable: {
          projectId: projectId,
          datasetId: datasetId,
          tableId: tableId
        },
        allowJaggedRows: true,
        writeDisposition: 'WRITE_TRUNCATE',
        schema: {
          fields: [{name:'Date', type: 'STRING',mode:'NULLABLE'},
                   {name:'Account_ID', type: 'INTEGER',mode:'NULLABLE'},
                   {name:'Account', type: 'STRING',mode:'NULLABLE'},
                   {name:'Campaign_ID', type: 'INTEGER',mode:'NULLABLE'},
                   {name:'Campaign_name', type: 'STRING',mode:'NULLABLE'},
                   {name:'Impressions', type: 'INTEGER',mode:'NULLABLE'},
                   {name:'Clicks', type: 'INTEGER',mode:'NULLABLE'},
                   {name:'Cost', type: 'FLOAT',mode:'NULLABLE'},
                   {name:'Conversions', type: 'FLOAT',mode:'NULLABLE'},
                   {name:'Value_per_conversion', type: 'FLOAT',mode:'NULLABLE'},
                   {name:'Total_conversion_value', type: 'FLOAT',mode:'NULLABLE'},
                   {name:'Average_position', type: 'FLOAT',mode:'NULLABLE'},

          ]
        }
      }
    }
  };

  var spreadsheet = SpreadsheetApp.openById(fileId);
  var filename = spreadsheet.getName();

  var MAX_ROWS = 50000;
  var sheet = spreadsheet.getSheetByName(tableId); 
  //var sheet = spreadsheet.getSheets()[0]; //.getSheetByName("Expenses")
  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");

  // Execute the job.
  BigQuery.Jobs.insert(jobSpec, projectId, data);
}

1 个答案:

答案 0 :(得分:0)

据我所知,行var cell = data[row][col].toString();会将单元格的内容转换为字符串。导入后,日期已存储为dateTime类型,并将使用标准格式转换为字符串,因此会产生不需要的输出。 您必须使用所需的格式显式转换dateTime列。 为了更好地理解内部发生的事情,我建议您使用调试器查看单元格的内容。