我在一张纸上有一些表格,我需要制作一个脚本来每天发送一封电子邮件。它今天搜索的脚本日期,将其匹配到我的表中并返回列号,然后我在第一列 (A) 上查找总计,一旦找到它,返回行号。获得行号和列号后,返回当天的总值。我在 JavaScript 方面并不先进,而且我在数组方面很吃力(仍在学习)。到目前为止,只要我在该工作表上只有一个表格,我的脚本就可以很好地工作,但在工作表上将超过 50 个表格,每个表格最后都有一个总计。我的公式会找到总计,但会将所有总计(行号)作为字符串返回。我需要的是只获得第一个总计(行号)。我希望这一切都有意义。
我附上了一张图片来制作一个想法和我目前的脚本:
function getTodaysTotal() {
function toDateFormat(date) {
try {return date.setHours(0,0,0,0);}
catch(e) {return;}
}
var values = SpreadsheetApp
.openById("ID")
.getSheetByName("Q3 - W27 - 39")
.getDataRange()
.getValues();
for (i in values){
if (values[i][0]=='Total'){
var nr = i;
Logger.log(nr); // will return two values (41 - first total and
104 second total ... if you add more Total it will return all rows
numbers that contain word Total
}
}
var today = toDateFormat(new Date());
var todaysColumn =
values[5].map(toDateFormat).map(Number).indexOf(+today);
var output = values[nr][todaysColumn];
// Logger.log(output);
var emailDate = Utilities.formatDate(new Date(today),"GMT+1",
"dd/MM/yyyy");
这只是第一个表,但在这个表下会有更多,每个表都有一个总计。
谢谢!
亲切的问候!
最佳答案
您应该在循环之外声明 nr,因为您将使用该值。
var nr = 0;
既然你正在读取一个数组,你应该使用你循环的数组长度
for (var i=0; i<values.length; i++){
if (values[i][0]=='Total'){
nr = i;
Logger.log(nr);
break; // this will stop at the first match
}
}
获得字符串形式的总数后,您可以通过调用以下函数将其转换为数字。
var string = values[a][b];
var num = Number(string);
关于javascript - 返回行号匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45446943/