我正在制作一张定期写入SQL服务器的Google工作表。这是我的代码到目前为止,少了SQL的东西。它从数据库中的最后一个条目中获取一个变量(" User"在此测试中),然后提交它之后的所有内容。
function connectToMySqlDB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
var lastColumn = sheet.getLastColumn();
Logger.log('Starting query process... ');
// Connect to DB and get last entry
var lastline = "User"
Logger.log('Found serial number of last enter in SQL Database: ' + lastline);
// Find row of entry in goolge sheet
for (var i = 0; i < values.length; i++) {
var row = "";
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == lastline) {
row = [i+1]
Logger.log('Found serial number in row: ' + row);
}
}
}
// Get first unfilled row
var crange = sheet.getRange("C1:C").getValues();
var Clast = crange.filter(String).length;
Logger.log('Found last row in C: ' + Clast);
var cfin = [Clast - row]
Logger.log('Slection will be: ' + row + ' rows.');
// select rows from entry to last filled row
var selection = sheet.getRange(row,0,cfin,lastColumn)
// Submit rows line by line to DB (from last entry to first unfilled)
// Write one row of data to a table.
}
我从谷歌错误中得到的错误是:
The coordinates or dimensions of the range are invalid. (line 33, file "Code")
使用调试工具,变量&#34; row&#34;没有价值。但是,日志文件显示它确实有效
[16-09-15 11:39:41:063 PDT] Starting query process...
[16-09-15 11:39:41:064 PDT] Found serial number of last enter in SQL Database: User
[16-09-15 11:39:41:067 PDT] Found serial number in row: 26
[16-09-15 11:39:41:157 PDT] Found last row in C: 37
[16-09-15 11:39:41:158 PDT] Selection will be: rows.
我的猜测是某种方式&#34; var cfin = [Clast - row]&#34;正在清除&#34;行&#34;变量。我无法弄清楚如何解决这个问题。
答案 0 :(得分:3)
您似乎希望row
成为一个数字,但是您将其计算包装在方括号中,为您提供一个长度为一个元素的数组。取下方括号。
row = i+1;
(顺便说一下,你将它宣布为一个字符串,不会有所作为,但最好不要这样做!)
之后,您使用cfin
犯了同样的错误。如果您只想要一个数字
var cfin = Clast - row;
如果找到row
设置,您可能也会遇到问题,但外部循环将重置该变量。你可以通过打破两个循环来解决这个问题:
var row = -1;
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == lastline) {
row = i+1
Logger.log('Found serial number in row: ' + row);
break;
}
}
if(row > -1) break;
}