在googlecloudsql中从google表中指定范围的数据创建一个表

mum43rcc  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(255)

我在google电子表格中有多个命名范围。我需要在googlecloudsql中为每个命名范围创建表,其中包含来自各个命名范围的数据
我为此开发了以下googleapps脚本。但我很好奇,是否有更好的方法来实现这一点与谷歌应用程序脚本或其他东西。我乐于接受各种想法。

var conn = Jdbc.getConnection(dbUrl, user, userPwd);

  var colNames = rangeValues[0];
  var sampleRow = rangeValues[2]

  var CREATE_TABLE_STATEMENT = " CREATE TABLE " + tableName + "(";

  var COLUMNS_TYPES= Array();
  var UntitledColumnName = 1;
  for (var colNames_i = 0; colNames_i < colNames.length; colNames_i++) {
    var colName = colNames[colNames_i];
    if (typeof sampleRow[colNames_i] == "number") {
      COLUMNS_TYPES[colNames_i] = "number";
      if (colName != "") {
        CREATE_TABLE_STATEMENT += " `" + colName + "` INT,";
      }
      else {
        CREATE_TABLE_STATEMENT += " " + "Column" + UntitledColumnName + " INT,";
        UntitledColumnName++;
      }
    }
    else {
      COLUMNS_TYPES[colNames_i] = 'string';
      if (colName != "") {
        CREATE_TABLE_STATEMENT += " `" + colName + "` VARCHAR(255),";
      }
      else {
        CREATE_TABLE_STATEMENT += " " + "Column" + UntitledColumnName +   " VARCHAR(255),";
        UntitledColumnName++;
      }
    }
  }

  CREATE_TABLE_STATEMENT = CREATE_TABLE_STATEMENT.slice(0, -1);
  CREATE_TABLE_STATEMENT += ")"
  Logger.log(CREATE_TABLE_STATEMENT);

  var stmt = conn.prepareStatement(CREATE_TABLE_STATEMENT); 
  stmt.execute();
  //   conn.setAutoCommit(false);

  for (var row_i = 1; row_i < rangeValues.length; row_i++) {
    var INSERT_STATEMENT = "INSERT INTO " + tableName + " values (";
    for (var colNames_i = 0; colNames_i < colNames.length; colNames_i++){
      if (COLUMNS_TYPES[colNames_i] == "number") {
        INSERT_STATEMENT +=  rangeValues[row_i][colNames_i] + ",";
      }
      else {
        INSERT_STATEMENT +=  "'" + rangeValues[row_i][colNames_i] + "',";
      }
    }

    INSERT_STATEMENT = INSERT_STATEMENT.slice(0, -1);
    INSERT_STATEMENT += ")"

    Logger.log(INSERT_STATEMENT);
    var stmt = conn.prepareStatement(INSERT_STATEMENT);
    try {
      stmt.execute();
    }
    catch(e){}
  }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题