嗨,我正在使用JDBC连接器将一些数据从Google表格插入到mysql
有人可以帮助我如何忽略其中包含公式的空单元格值吗?
我只是使用简单的='Sheet1'!C1公式
从另一张表中获取导入数据表中的数据我尝试过
var newdata = [i for each (i in data)if (isNaN(i))];
和
var newdata = data.filter(String);
但无法解决问题(可能是过滤后导致问题的join(','))
下面的日志详细信息供您参考-感谢您提供的任何帮助或指导。让我知道是否需要更多详细信息
[19-07-15 09:22:29:522 PDT] INSERT INTO CymaxVendorReachout2(UTID,Process,BatchNo,BatchName,ReceivedDate,SKUCount,Status,VMTTicket,AssignedTo,Comments,querytype,EmailSentDate,FolUpNum,Folupfrom,FolUpDate,NextFolUpDate,Note) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
[19-07-15 09:22:29:525 PDT] [MER-8903-2019-07-29, Full Merch, MER-8903, Darlee - 903, 2019-07-24, 158, Invalid, No, Onkar, , , 2019-07-29, 1, Vendor, 2019-07-31, 2019-08-02, ]
[19-07-15 09:22:29:530 PDT] [MER-8904-2019-07-30, Full Merch, MER-8904, Darlee - 904, 2019-07-25, 159, Invalid, No, Onkar, , , 2019-07-30, 1, Vendor, 2019-08-01, 2019-08-05, ]
[19-07-15 09:22:29:536 PDT] [MER-8905-2019-07-31, Full Merch, MER-8905, Darlee - 905, 2019-07-26, 160, Invalid, No, Onkar, , , 2019-07-31, 1, Vendor, 2019-08-02, 2019-08-05, ]
[19-07-15 09:22:30:038 PDT] [, , , , , , , , , , , , , , , , ]
[19-07-15 09:22:30:044 PDT] [, , , , , , , , , , , , , , , , ]
[19-07-15 09:22:30:050 PDT] [, , , , , , , , , , , , , , , , ]
[19-07-15 09:22:30:055 PDT] [, , , , , , , , , , , , , , , , ]
[19-07-15 09:22:55:536 PDT] Data truncation: Incorrect date value: '' for column 'ReceivedDate' at row 1
我正在运行完整的google app脚本
function writeToTable() {
var ss = SpreadsheetApp.getActive();
var sheetDetails = ss.getSheetByName('Details');
var sheetData = ss.getSheetByName('Data');
var sheetImportData = ss.getSheetByName('Import Data');
var host = sheetDetails.getRange("B1").getValue();
var databaseName = sheetDetails.getRange("B2").getValue();
var userName = sheetDetails.getRange("B3").getValue();
var password = sheetDetails.getRange("B4").getValue();
var port = sheetDetails.getRange("B5").getValue();
var tableName = sheetDetails.getRange("B6").getValue();
var url = 'jdbc:mysql://'+host+':'+port+'/'+databaseName;
Logger.log(url);
var sql = 'SELECT * FROM ' + tableName;
var data = getData()
var headerString = data[1]
data = data[0]
try{
var start = new Date();
var conn = Jdbc.getConnection(url, userName, password);
conn.setAutoCommit(false);
var sql = 'INSERT INTO ' + tableName + headerString;
Logger.log(sql);
var stmt = conn.prepareStatement(sql);
var dateColumns = []; // Add the index to this array for date type column
for (var r in data) {
Logger.log(data[r]);
for (var c in data[r]){
if (dateColumns.indexOf(parseInt(c) + 1) >= 0){
var date = Utilities.formatDate(data[r][c], Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss");
stmt.setObject(parseInt(c) + 1, date)
}else{
stmt.setString(parseInt(c) + 1, data[r][c]);
}
}
stmt.addBatch();
}
var batch = stmt.executeBatch();
conn.commit();
conn.close();
var end = new Date();
Logger.log('Time elapsed: %sms for %s rows.', end - start, batch.length);
SpreadsheetApp.getActive().toast('Time elapsed: %sms for %s rows.', end - start, batch.length);
}catch(err){
SpreadsheetApp.getActive().toast(err.message);
Logger.log(err.message);
}
}
function getData(){
//(UTID, Process, BatchNo, BatchName, ReceivedDate, SKUCount, Status, VMTTicket, AssignedTo, Comments, querytype, EmailSentDate, FolUpNum, Folupfrom, FolUpDate, NextFolUpDate, Note ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
var ss = SpreadsheetApp.getActive();
var sheetImportData = ss.getSheetByName('Import Data');
var data = sheetImportData.getDataRange().getValues();
var header = data[0];
var headerString = header.join(',')
headerString = '(' + headerString + ') values ('
var questions = Array(header.length + 1).join('?').split('')
headerString = headerString + questions.join(',') + ')'
Logger.log(headerString)
Logger.log(data.slice(1))
return [data.slice(1), headerString]
}
答案 0 :(得分:1)
从您的问题和答复中,我可以像上面那样理解。为此,请按如下所示修改WITH tree (id, parentid, level, name, parentIdInt, chiledId) as
(
SELECT id, parentId, 0 as level, name, 1 as parentIdInt, 0 as chiledId
FROM tableA a
WHERE a.parentId IS NULL
UNION ALL
SELECT b.id, b.parentId, tree.level + 1, b.name, tree.parentIdInt,
cast((tree.chiledId + 1 + tree.parentIdInt) as int)
FROM tableA b
INNER JOIN tree ON tree.id = b.parentId
)
SELECT *
FROM tree
的脚本。
请在getData()
函数中的var data = sheetImportData.getDataRange().getValues();
下面添加以下脚本。
getData()
或
var newData = data.filter(function(row) {return row.filter(String).length > 0});
答案 1 :(得分:0)
编辑2: Tanaike的回答更为简洁:
https://stackoverflow.com/a/57048404/8240995
我通过在数组中查找和删除元素来解决这个问题,这可能对不同的项目有用。
Google Apps脚本部分
如果您只想获取单元格的可见值,即打开电子表格时会看到,则可以替换
.getValue()
使用
.getDisplayValue()
对于具有公式结果为空的单元格,.getDisplayValue()将返回一个空字符串。 它还带有电子表格中应用的所有格式,例如日期和时间格式。
编辑-新信息
我将编辑脚本以忽略getData()函数中的空白单元格显示值:
function getData(){
//(UTID, Process, BatchNo, BatchName, ReceivedDate, SKUCount, Status, VMTTicket, AssignedTo, Comments, querytype, EmailSentDate, FolUpNum, Folupfrom, FolUpDate, NextFolUpDate, Note ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
var ss = SpreadsheetApp.getActive();
var sheetImportData = ss.getSheetByName('Import Data');
// replacing this to fetch the display values var data = sheetImportData.getDataRange().getValues();
var data = sheetImportData.getDataRange().getDisplayValues();
var header = data[0];
var headerString = header.join(',')
headerString = '(' + headerString + ') values ('
var questions = Array(header.length + 1).join('?').split('')
headerString = headerString + questions.join(',') + ')'
Logger.log(headerString)
// now the header's sorted, we can trim the data to exclude any blank entries
var newData = data.slice(1);
for (var i = 0; i < newData.length; i++){
if (newData[i] === ''){
newData.splice([i], 1);
i = i - 1;// set the counter back one to catch consecutive blanks
}
}
Logger.log(newData)
return [newData, headerString]
}
从代码来看,似乎所有数据都在一行中。
我尚未检查与标题行的关系。如果您有要使数据对齐的列标题,则我的代码将有问题。 但是,由于您要删除所有空格,因此我假设没有这样的列关联。