如何在Google App脚本中从JdbcResultSet获取数组? [英] How to getArray from JdbcResultSet in google app script?
本文介绍了如何在Google App脚本中从JdbcResultSet获取数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
虽然我能够循环执行并从jdbc结果获取结果到数组,但是我想知道如何使用getArray()做到这一点.
I am able to loop though and get results to an array from jdbc result, but I am wondering how to do this with getArray().
var results = stmt.executeQuery("select col1, col2 from sometable;");
var arr1 = results.getArray("col1");
此外,如何仅转储或检查Google应用程序脚本中的结果对象?
Also, how to just dump or inspect the results object in google app script ?
推荐答案
尝试以下代码.该示例基于官方文档中的示例:
Try the following code. It's based in this example from the official docs:
https://developers.google.com/apps-script/guides/jdbc#read_from_the_database
//Execute query
var results = stmt.executeQuery("select col1, col2 from sometable;");
//Generate a 2d array to insert in spreadsheet
var array2d = [];
//Generate array with titles
var numCols = results.getMetaData().getColumnCount();
var titles = [];
for (var col = 0; col < numCols; col++){
var colName = results.getMetaData().getColumnLabel(col + 1);
titles.push(colName);
}
//Insert titles in 2d array
array2d.push(titles);
//Extract results from query and insert insert into array2d
while (results.next()) {
//This is an array with data from each row
rowData = [];
for (var col = 0; col < numCols; col++) {
rowData.push(results.getString(col + 1));
}
//Insert data from each row in 2d array
array2d.push(rowData);
}
//close
results.close();
Logger.log(array2d);
SpreadsheetApp.getActiveSheet()
.getRange(1, 1, Object.keys(array2d).length, array2d[0].length)
.setValues(array2d)
这篇关于如何在Google App脚本中从JdbcResultSet获取数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文