根据您的代码,我了解到您使用的是 Oracle SQL。如果是这种情况,您可以使用 Apps 脚本的 JDBC Connector 连接到您的数据库,执行查询,然后将数据附加到您的工作表中。
一个例子(改编自official documentation)如下所示:
/**
* Replace the variables in this block with real values.
* You can find the "Instance connection name" in the Google Cloud
* Platform Console, on the instance Overview page.
*/
var connectionName = 'Instance_connection_name';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_name';
var dbUrl = 'jdbc:oracle:thin:@//' + connectionName + '/' + db;
function importData() {
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
var ss = SpreadsheetApp.create('Employees dump');
var sheet = ss.getSheets()[0];
// Insert sheet headers
sheet.appendRow(['Name', 'Age', 'Location']);
var stmt = conn.createStatement();
stmt.setMaxRows(1000);
var cursor = stmt.executeQuery('SELECT name, age, location FROM employees');
while (cursor.next()) {
var row = [cursor.getObject('name'),
cursor.getObject('age'),
cursor.getObject('location')];
sheet.appendRow(row);
}
results.close();
stmt.close();
}
在这种情况下,JDBC 用于查询员工数据,同时通过使用 SpreadsheetApp、Spreadsheet 和 Sheet 类将数据插入到 Sheets 文档中。
同样重要的是,如果需要,将服务器上的以下 IP 列入白名单,以允许从脚本(来自 https://developers.google.com/apps-script/guides/jdbc)访问它:
64.18.0.0/20
64.233.160.0/19
66.102.0.0/20
66.249.80.0/20
72.14.192.0/18
74.125.0.0/16
173.194.0.0/16
207.126.144.0/20
209.85.128.0/17
216.239.32.0/19