JDBC Google 工作表中的错误 MYSQL 格式编号

Wrong MYSQL format number in Google Sheets via JDBC

我从 google sheet 上的 sql 数据库导入的数字被解释为持续时间而不是数字,更具体地说是货币。

colA colB colC colD
449 1234 29521.00 0.00
449 1234 29521.00 0.00

错误 = 29521.00(当前导入的格式)
正确 = € 29.521,00

我在 colC 和 colD 中做错了什么?

函数 runSql(查询,选项){

console.log('query from runSql :' + query);
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = doc.getSheets()[2];
    var cell = sheet.getRange('a1');
    var activeCellRow = cell.getRow();
    var activeCellCol = cell.getColumn();

var rs = [];
    try {
        var fullConnectionString = 'jdbc:' + DB_TYPE + '://' + HOST + ':' + PORT
        var conn = Jdbc.getConnection(fullConnectionString, USERNAME, PASSWORD);
        console.log('query :', query)
        var stmt = conn.createStatement();
        stmt.execute('USE ' + DATABASE);
        var start = new Date();
        var stmt = conn.createStatement();
        //stmt.setMaxRows(MAXROWS);
        var rs = stmt.executeQuery(query);
    } catch (e) {
        console.log(e, e.lineNumber);
        Browser.msgBox(e);
        return false
    }

    var results = [];
    cols = rs.getMetaData();
    console.log("cols", cols)
    var colNames = [];
    var colTypes = {};
    for (i = 1; i <= cols.getColumnCount(); i++) {
        var colName = cols.getColumnLabel(i)
        colTypes[colName] = { type: cols.getColumnTypeName(i), loc: i }
        colNames.push(colName);
    }
    var rowCount = 1;
    results.push(colNames);
    while (rs.next()) {
        curRow = rs.getMetaData();
        rowData = [];
        for (i = 1; i <= curRow.getColumnCount(); i++) {
            rowData.push(rs.getString(i));
        }
        results.push(rowData);
        rowCount++;
    }
    rs.close();
    stmt.close();
    conn.close();
    console.log('results', results)
    var colCount = results[0].length
    var rowCount = results.length
    var comment = "Updated on: " + (new Date()) + "\n" + "Query:\n" + query
    if (options.omitColumnNames) {
        results = results.slice(1)
        rowCount -= 1
    }
    if (options.clearColumns && sheet.getLastRow() > 0) {
        var startCellRange = sheet.getRange(startCell)
        sheet.getRange(startCellRange.getRow(), startCellRange.getColumn(), sheet.getLastRow(), colCount).clearContent();
    }
    if (options.clearSheet) {
        var startCellRange = sheet.getRange(startCell)
        sheet.clear({ contentsOnly: true });
    }
    //sheet.getRange(activeCellRow, activeCellCol, rowCount, colCount).clearContent();
    sheet.getRange(activeCellRow, activeCellCol, rowCount, colCount).setValues(results);
    var cell = sheet.getRange(activeCellRow, activeCellCol)
    cell.clearNote()
    cell.setNote(comment);
    sheet.setActiveRange(sheet.getRange(activeCellRow + rowCount + 1, activeCellCol))
    console.log('query success!, rows = ', rowCount - 1)
}

function runSqlFromSheet() {
   //var doc = SpreadsheetApp.getActiveSpreadsheet();

    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = doc.getActiveSheet();
    var sheet = doc.getSheets()[2];
    var sheetName = sheet.getName();
    var cell = doc.getActiveSheet().getActiveCell();



    //var sql2 = '{call P_Totali_per_Azienda(?)}';
    //Logger.log('sql2;', sql2)
      var options = {}
    console.log(sql2);
    runSql(sql2, options)
    
}

值自动转换为持续时间的事实表明数据采用的格式使 Google 工作表将它们视为持续时间值。如果您以文本字符串形式读取和写入数据,并且您的 spreadsheet 位于使用逗号作为小数点分隔符(如 € 1,99)和句点作为时间分隔符(如 12.00).如果您以文本字符串形式读取值,则可能需要以数字形式读取它们以避免自动转换。

考虑使用 ResultSet.getFloat() instead of ResultSet.getString()

一旦您可以将值正确写入传播sheet,您就可以通过编程方式设置最终格式,如下所示:

  SpreadsheetApp.getActive()
    .getRange("Sheet1!C2:D")
    .setNumberFormat("[$ €]#,##0.00");

如果您总是将值写入相同的 sheet,并从数据库中获取数据作为数字,您很可能可以手动将列 C:D 格式化为 Format > Number > Currency 只需一次,并让格式在以后的导入中保持不变。