【发布时间】:2021-10-21 15:37:57
【问题描述】:
我有一个谷歌表格,底部有许多不同的“表格标签”。其中一些与 Google 表单相关联,因此当填充该表单时,条目会显示在该“选项卡”上。
我也在尝试将这些条目注入 MySQL 数据库。我已经使用与工作表绑定的 Google Apps 脚本使用单个 Google 工作表/表单配对成功地工作,该工作表监听 onFormSubmit。该代码如下:
// These are the Database connections values
var user = 'dbUsername';
var userPwd = 'dbPassword';
var db = 'databaseName';
var dbUrl = 'jdbc:mysql://192.168.0.0:3306/' + db;
/**
* @param {Object} e The event parameter for form submission to a spreadsheet;
* see https://developers.google.com/apps-script/understanding_events
*/
function onFormSubmit(e) {
console.log(e);
var color = e.namedValues['Color1'][0]; // these names ('Color', 'Number') come from the Form's Question
var number = e.namedValues['Number1'][0];
writeColor(color, number);
}
/**
* Write one row of data to a table.
*/
function writeColor(color, number) {
var conn = Jdbc.getConnection(dbUrl, user, userPwd); // this connects to the database
var stmt = conn.prepareStatement('INSERT INTO test_google_forms '
+ '(`color`, `number`) values (?, ?)');
stmt.setString(1, color);
stmt.setString(2, number);
stmt.execute(); // this performs the SQL that was "prepared" in the previous 3 lines
}
但是,只要提交任何附加的表单,Google 就会触发该功能。因此,如果我有两个表单提供同一张工作表的不同选项卡,则无论提交哪个表单(或写入哪个选项卡),它都会执行此代码。
此外,我在传递的事件对象中看不到任何关于它来自哪个表单(或它正在写入的选项卡)的信息,所以我不知道如何对这些响应进行分类以转到正确的 MySQL表格。
有什么方法可以过滤这些调用,还是我需要将我的工作表分成 1-tab-per-Sheet 以避免这种冲突?
【问题讨论】:
标签: google-apps-script google-sheets google-forms