问题
I have some tables in one sheet and I need to make a script to send an email everyday. The script it search today date, match it into my table and return column number, then I looks for Total on the first column (A) and once found it, return row number. Once I have row number and column number, return Total value for that day. I'm not advanced with JavaScript and I'm struggle with arrays (still learning). The script I have so far is working very good as long I have only one table on that sheet, but on the sheet will be over 50 tables, each one will have a Total at the end. The formula I have will find Total, but will return all Totals (row numbers) as Strings. What do I need is to get just the first Total (row number). I hope it all make sense.
I have attached an image to make an idea and my script I have so far:
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");
This is just the first table, but there will be more under this one and each one will have a Total.
Thank you!
Kind regards!
回答1:
You should declare nr outside of your loop since you will use the value.
var nr = 0;
Since you are reading an array, you should use the array length for you loop
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
}
}
Once you get your total as a String, you can convert it to a number by calling the following function.
var string = values[a][b];
var num = Number(string);
来源:https://stackoverflow.com/questions/45446943/return-row-number-is-match-error