Google Sheets JDBC脚本中的西里尔文数据库内容

时间:2018-12-23 10:52:33

标签: mysql jdbc google-apps-script encoding google-sheets

大家好,我在Google Sheets脚本中遇到了一个比较棘手的问题,由于其查询内容为俄语,因此MySQL查询的显示和过滤无法正常工作

我将整个脚本代码放在下面,但是不起作用的主要部分是:

  from samplename_order WHERE status = "Оплачен")');

以及

  select (select data_create) as "Дата"

在第二个示例中,列标题呈现为“ ????”

在第一个示例中,除了列标题名称之外,我什么都没得到。

整个代码:

    function onOpen() {

var spreadsheet = SpreadsheetApp.getActive();

var menuItems = [
    {name: 'Sync', functionName: 'readData'}
];
spreadsheet.addMenu('Sync', menuItems);
}


var address = 'xxx:3306'; //ex. '10.1.1.1:1433'
var user = 'xxx';
var userPwd = 'xxxx';
var db = 'sxxxx';

var dbUrl = 'jdbc:mysql://' + address + '/' + db;


function readData() {
 var conn = Jdbc.getConnection(dbUrl, user, userPwd);
 var stmt = conn.createStatement();
 stmt.setMaxRows(1000);

 var results = stmt.executeQuery('select (select data_create) as "Дата",(select concat(name," ",sername)) as name,mail,(select concat(" ",phone)) as phone,comments,`status` from sample_order WHERE status = "Оплачен"');

  var metaData=results.getMetaData();
 var numCols = results.getMetaData().getColumnCount();






 var sheet = SpreadsheetApp.getActiveSheet();




 sheet.clearContents();

 var arr=[];

 for (var col = 0; col < numCols; col++) {
  arr.push(metaData.getColumnName(col + 1));
 }

 sheet.appendRow(arr);


 while (results.next()) {
  arr=[];

  for (var col = 0; col < numCols; col++) {

   arr.push(results.getString(col + 1));
  }
 sheet.appendRow(arr);

}

results.close();
stmt.close();

sheet.autoResizeColumns(1, numCols+1);


}

2 个答案:

答案 0 :(得分:2)

可能您应该在连接字符串中使用正确的字符编码选项。 在这种情况下,有一个good answer用于使用utf-8。

答案 1 :(得分:2)

将此添加到getConnection()呼叫中:

?useUnicode=yes&characterEncoding=UTF-8

问号和其他常见字符集问题的解释here