Google 特定列的 Apps 脚本 onEdit(e) 不起作用
Google Apps Script onEdit(e) for specific column does not work
我知道这个问题已经得到解答,但我尝试了多种方法,无法理解为什么我的脚本不起作用。所以我希望它仅在您编辑特定列(H 列的第 8 号)时具有 运行 功能,但它不起作用,并且在我编辑任何单元格时具有 运行s 功能。
请修改我的代码,如果有任何遗漏请告诉我,因为我尝试了多种方法但它们都不起作用。
提前致谢
My code
function onEdit(e) {
var server = "" , dbName = "" , username = "" , password = "" , port = 3306;
var url = "jdbc:mysql://"+server+":"+port+"/"+dbName;
var conn = Jdbc.getConnection(url, username, password);
var range = e.range;
var col = range.getColumn();
if (col === 8) {
var stmt_del = conn.prepareStatement('Delete from sheets_db');
var exe_del = stmt_del.execute();
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var stmt_inst = conn.prepareStatement('INSERT INTO sheets_db (Full_Name, LOS, Grade, Sub_LoS, E_mail, DS_Floor, DS_Desk) values (?, ?, ?, ?, ?, ?, ?)');
for (var i = 1; i < data.length; i++) {
stmt_inst.setString(1, data[i][0]);
stmt_inst.setString(2, data[i][1]);
stmt_inst.setString(3, data[i][2]);
stmt_inst.setString(4, data[i][3]);
stmt_inst.setString(5, data[i][4]);
stmt_inst.setString(6, data[i][5]);
stmt_inst.setString(7, data[i][6]);
stmt_inst.addBatch();
};
var exe_inst = stmt_inst.executeBatch();
};
conn.close();
};
PD:对于上下文,该函数删除数据库中的所有记录并插入 sheet 中的行,如果您可以另外告诉我一种不删除记录但不插入的方法每次重复条目 运行s 我将不胜感激。
the function runs when i edit any cell and i want to run when i edit
only one column
当用户更改文档中任何单元格的值时,onEdit
将被激活。你无法摆脱它。
- 但是您可以将完整的代码放在
if
条件中,这样只有在您编辑第 8 列时才会执行代码。
请记住,这对每个 sheet 都有效,如果您需要在 if
条件中添加特定的 sheet 名称,请告诉我。
解法:
function onEdit(e) {
var range = e.range;
var col = range.getColumn();
if (col === 8) {
var server = "" , dbName = "" , username = "" , password = "" , port = 3306;
var url = "jdbc:mysql://"+server+":"+port+"/"+dbName;
var conn = Jdbc.getConnection(url, username, password);
var stmt_del = conn.prepareStatement('Delete from sheets_db');
var exe_del = stmt_del.execute();
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var stmt_inst = conn.prepareStatement('INSERT INTO sheets_db (Full_Name, LOS, Grade, Sub_LoS, E_mail, DS_Floor, DS_Desk) values (?, ?, ?, ?, ?, ?, ?)');
for (var i = 1; i < data.length; i++) {
stmt_inst.setString(1, data[i][0]);
stmt_inst.setString(2, data[i][1]);
stmt_inst.setString(3, data[i][2]);
stmt_inst.setString(4, data[i][3]);
stmt_inst.setString(5, data[i][4]);
stmt_inst.setString(6, data[i][5]);
stmt_inst.setString(7, data[i][6]);
stmt_inst.addBatch();
};
var exe_inst = stmt_inst.executeBatch();
conn.close();
};
};
我知道这个问题已经得到解答,但我尝试了多种方法,无法理解为什么我的脚本不起作用。所以我希望它仅在您编辑特定列(H 列的第 8 号)时具有 运行 功能,但它不起作用,并且在我编辑任何单元格时具有 运行s 功能。
请修改我的代码,如果有任何遗漏请告诉我,因为我尝试了多种方法但它们都不起作用。
提前致谢
My code
function onEdit(e) {
var server = "" , dbName = "" , username = "" , password = "" , port = 3306;
var url = "jdbc:mysql://"+server+":"+port+"/"+dbName;
var conn = Jdbc.getConnection(url, username, password);
var range = e.range;
var col = range.getColumn();
if (col === 8) {
var stmt_del = conn.prepareStatement('Delete from sheets_db');
var exe_del = stmt_del.execute();
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var stmt_inst = conn.prepareStatement('INSERT INTO sheets_db (Full_Name, LOS, Grade, Sub_LoS, E_mail, DS_Floor, DS_Desk) values (?, ?, ?, ?, ?, ?, ?)');
for (var i = 1; i < data.length; i++) {
stmt_inst.setString(1, data[i][0]);
stmt_inst.setString(2, data[i][1]);
stmt_inst.setString(3, data[i][2]);
stmt_inst.setString(4, data[i][3]);
stmt_inst.setString(5, data[i][4]);
stmt_inst.setString(6, data[i][5]);
stmt_inst.setString(7, data[i][6]);
stmt_inst.addBatch();
};
var exe_inst = stmt_inst.executeBatch();
};
conn.close();
};
PD:对于上下文,该函数删除数据库中的所有记录并插入 sheet 中的行,如果您可以另外告诉我一种不删除记录但不插入的方法每次重复条目 运行s 我将不胜感激。
当用户更改文档中任何单元格的值时,the function runs when i edit any cell and i want to run when i edit only one column
onEdit
将被激活。你无法摆脱它。
- 但是您可以将完整的代码放在
if
条件中,这样只有在您编辑第 8 列时才会执行代码。
请记住,这对每个 sheet 都有效,如果您需要在 if
条件中添加特定的 sheet 名称,请告诉我。
解法:
function onEdit(e) {
var range = e.range;
var col = range.getColumn();
if (col === 8) {
var server = "" , dbName = "" , username = "" , password = "" , port = 3306;
var url = "jdbc:mysql://"+server+":"+port+"/"+dbName;
var conn = Jdbc.getConnection(url, username, password);
var stmt_del = conn.prepareStatement('Delete from sheets_db');
var exe_del = stmt_del.execute();
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var stmt_inst = conn.prepareStatement('INSERT INTO sheets_db (Full_Name, LOS, Grade, Sub_LoS, E_mail, DS_Floor, DS_Desk) values (?, ?, ?, ?, ?, ?, ?)');
for (var i = 1; i < data.length; i++) {
stmt_inst.setString(1, data[i][0]);
stmt_inst.setString(2, data[i][1]);
stmt_inst.setString(3, data[i][2]);
stmt_inst.setString(4, data[i][3]);
stmt_inst.setString(5, data[i][4]);
stmt_inst.setString(6, data[i][5]);
stmt_inst.setString(7, data[i][6]);
stmt_inst.addBatch();
};
var exe_inst = stmt_inst.executeBatch();
conn.close();
};
};