如何获得正确的csv格式以从Google表格中导入Google BigQuery中的数据。

时间:2017-04-27 00:21:52

标签: google-apps-script google-bigquery

我想将Google表格中的一些数据导入Google BigQuery。我一直在成功地遵循官方的第一步指南https://developers.google.com/apps-script/advanced/bigquery,但是当我尝试导入自己的数据时,我无法做到。

我从this问题中了解到问题是因为我正在格式化我的csv文件。

我正在使用以下脚本:

/*
 * script to export data in all sheets in the current spreadsheet as individual csv files
 * files will be named according to the name of the sheet
 * author: Michael Derazon
*/

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var csvMenuEntries = [{name: "export as csv files", functionName: "saveAsCSV"}];
  ss.addMenu("csv", csvMenuEntries);
};

function saveAsCSV() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  // create a folder from the name of the spreadsheet
  var folder = DriveApp.createFolder(ss.getName().toLowerCase().replace(/ /g,'_') + '_csv_' + new Date().getTime());
  for (var i = 0 ; i < sheets.length ; i++) {
    var sheet = sheets[i];
    // append ".csv" extension to the sheet name
    fileName = sheet.getName() + ".csv";
    // convert all available sheet data to csv format
    var csvFile = convertRangeToCsvFile_(fileName, sheet);
    // create a file in the Docs List with the given name and the csv data
    folder.createFile(fileName, csvFile);
  }
  Browser.msgBox('Files are waiting in a folder named ' + folder.getName());
}

function convertRangeToCsvFile_(csvFileName, sheet) {
  // get available data range in the spreadsheet
  var activeRange = sheet.getDataRange();
  try {
    var data = activeRange.getValues();
    var csvFile = undefined;

    // loop through the data in the range and build a string with the csv data
    if (data.length > 1) {
      var csv = "";
      for (var row = 0; row < data.length; row++) {
        for (var col = 0; col < data[row].length; col++) {
          if (data[row][col].toString().indexOf(",") != -1) {
            data[row][col] = "\"" + data[row][col] + "\"";
          }
        }

        // join each row's columns
        // add a carriage return to end of each row, except for the last one
        if (row < data.length-1) {
          csv += data[row].join(",") + "\r\n";
        }
        else {
          csv += data[row];
        }
      }
      csvFile = csv;
    }
    return csvFile;
  }
  catch(err) {
    Logger.log(err);
    Browser.msgBox(err);
  }
}

它似乎工作正常,我可以在我的Google云端硬盘文件夹中看到我的CSV文件。 Bigquery导出脚本也正常运行,但在查看我的表时没有任何内容。

我该怎么做才能完成所有这些工作?

谢谢!

1 个答案:

答案 0 :(得分:2)

最后,我使用this问题答案中的代码段,它完美无缺:

function myFunction() {
  var projectId = 'XXXXXXXXXXXXXXXXXXX';
  var fileId = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

  // Define our load job.
  var jobSpec = {
    configuration: {
      load: {
        destinationTable: {
          projectId: projectId,
          datasetId: 'Experiment',
          tableId: 'disease_rates'
        },
        allowJaggedRows: true,
        writeDisposition: 'WRITE_TRUNCATE',
        schema: {
          fields: [
            { name: 'data1', type: 'STRING' },
            { name: 'data2', type: 'STRING' },
            { name: 'data3', type: 'INTEGER' }
          ]
        }
      }
    }
  };

  // Partially borrowed from: https://developers.google.com/apps-script/articles/docslist_tutorial#section-3-saving-a-selected-range-to-a-csv-file  
  var spreadsheet = SpreadsheetApp.openById(fileId);
  var filename = spreadsheet.getName();

  // TODO: only handles the first sheet and limited to MAX_ROWS.
  // TODO: does not escape embedded quotes.
  var MAX_ROWS = 5000;
  var sheet = spreadsheet.getSheets()[0]; 
  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);
}