来自csv beore导入的组ans sum

时间:2018-11-07 17:50:01

标签: google-apps-script google-cloud-sql

我有一个脚本,用于从附加到邮件的csv更新Google Cloud sql db。

问题是csv文件很大。我想知道是否有可能在传输数据之前在csv上进行计算。例如: 按Col5,Col8,Col9选择Col5,Col8,Col9,sum(Col11)组 然后使用此数据为导入创建我的SQL语句。

这是我的实际代码:

function writeManyRecords() {
  var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
  conn.setAutoCommit(false);

  var stmt = conn.prepareStatement('TRUNCATE test_orders');
  stmt.execute();
  conn.commit();

  var threads = GmailApp.search('label:stock_fastmag subject:"Analyse_ventes"');
  var message = threads[0].getMessages()[0];
  var attachment = message.getAttachments()[0];
  attachment.setContentType('text/csv');

 // Is the attachment a CSV file
 if (attachment.getContentType() === "text/csv") {

  var data = Utilities.parseCsv(attachment.getDataAsString(), ";");  
  var start = new Date();
  var stmt = conn.prepareStatement('INSERT INTO test_orders '
      + '(Vente, Date, Heure, CodeMag, Total) values (?, ?, ?, ?, ?)');
  for (var i = 0; i < data.length; i++) {
    stmt.setString(1, data[i][0]);
    stmt.setString(2, data[i][1]);
    stmt.setString(3, data[i][2]);
    stmt.setString(4, data[i][3]);
    stmt.setString(5, data[i][4]);
    stmt.addBatch();
  }


  var batch = stmt.executeBatch();
  conn.commit();
  }
  conn.close();

  var end = new Date();
  Logger.log('Time elapsed: %sms for %s rows.', end - start, batch.length);

0 个答案:

没有答案