在google apps脚本中,有一种方法可以使用mysql表中的数据填充列表项。我编写了我的函数但是在下拉列表中未定义。你能指点我做错了吗?
function readFormListFromTable() {
var address = 'database_IP_address';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_name';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var stmt = conn.createStatement();
var results = stmt.executeQuery('SELECT [name] FROM employee');
var numCols = results.getMetaData().getColumnCount();
while (results.next()) {
var rowString = '';
for (var col = 0; col < numCols; col++) {
rowString += results.getString(col + 1) + '\t';
}
}
results.close();
stmt.close();
var form = FormApp.openById('42udfksdfksukdnf-DWER');
var tablelistitem = form.addListItem();
tablelistitem.setTitle('Name from mysql table')
.setChoices([tablelistitem.createChoice(rowString)]);
}
答案 0 :(得分:1)
我们将下一个脚本开发为Form并在open上配置tigger,以填充下拉列表及其工作。我希望这段代码可以帮助你:
function onOpen(){
var conexion=conectar_mysql(); //realizo conexion con mysql
var tiposHora=getTiposHora(conexion); //genero una lista con los tipos de hora disponibles
var form = FormApp.openById('form_id');
var item = form.addListItem(); // lista de todos los elementos que pertenecen al formulario para recargar el que me interese
item.setTitle('Tipos de Horas')
item.setChoiceValues(tiposHora);//recarga con los valores que necesito
}
function getTiposHora(conexion){
var stmt = conexion.createStatement();
var results = stmt.executeQuery('SELECT tipo FROM bbdd.tabla;');
var numCols = results.getMetaData().getColumnCount();
var tiposDeHora=[];
while (results.next()) {
var rowString = '';
for (var col = 0; col < numCols; col++) {
rowString += results.getString(col + 1) + '\t';
tiposDeHora.push(rowString);
}
}
return tiposDeHora;
}
function conectar_mysql(){
var address = 'direccion';
var user = 'usuario';
var userPwd = 'password';
var db = 'bbdd';
var instanceUrl = 'jdbc:mysql://' + address;
var dbUrl = instanceUrl + '/' + db;
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
conn.setAutoCommit(false);
return conn;
}