How to get cell value based on column value in one step? - google-apps-script

I need to retrieve date from a cell in Column C based on the name in Column A in the same row. The name comes from a name in another sheet. I need to create several fixed-value variables that will be used to calculate values for a report that is in a different sheet.
Here's what I have now for the example with the Date:
var report = SpreadsheetApp.getActiveSpreadsheet();
var areport = SpreadsheetApp.getActiveSheet();
var agent = areport.getRange('B6').getValue(); // The name of the person
var datafile = SpreadsheetApp.openById("....");
var asheet = datafile.getSheetByName("Agent");
var startDate = new Date(asheet.getRange('C16').getValues()); // The date for the person
So, instead of that reference to C16, I need to retrieve different rows from column C based on the name that is in the report. Logically, it is like this:
getRange(Column C if Column A==agent)
I know that I can extract all data from the Agent sheet and then create 1-2 variables and filter down to the right cell. But I want to do that inside getRange, if possible, because it will greatly simplify my script and make it just five lines for the five variables I need to set to the fixed values in columns C,D,E,F,G.

function getDateInColumnCForNameInColumnA(name='Name20') {//default name for testing
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');//sheet name
const shsr=2;//data start row
const names=sh.getRange(shsr,1,sh.getLastRow()-shsr+1,1).getValues().map(function(r){return r[0]});//names arrray flattened
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(sh.getRange(names.indexOf(name)+shsr,3).getA1Notation()), `Date for ${name}`); //display a1 notation of range for testing you can remove this if you wish
return sh.getRange(names.indexOf(name)+shsr,3).getA1Notation();//return a1 notation of range
}
My Test Data:
COL1,COL3,COL2
Name1,14,Wed Jan 01 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name2,15,Thu Jan 02 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name3,8,Fri Jan 03 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name4,6,Sat Jan 04 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name5,12,Sun Jan 05 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name6,8,Mon Jan 06 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name7,21,Tue Jan 07 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name8,13,Wed Jan 08 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name9,17,Thu Jan 09 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name10,13,Fri Jan 10 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name11,16,Sat Jan 11 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name12,4,Sun Jan 12 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name13,0,Mon Jan 13 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name14,2,Tue Jan 14 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name15,9,Wed Jan 15 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name16,19,Thu Jan 16 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name17,12,Fri Jan 17 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name18,18,Sat Jan 18 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name19,23,Sun Jan 19 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name20,22,Mon Jan 20 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name21,15,Tue Jan 21 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name22,2,Wed Jan 22 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name23,9,Thu Jan 23 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name24,4,Fri Jan 24 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Name25,20,Sat Jan 25 2020 00:00:00 GMT-0700 (Mountain Standard Time)
Note: you could write it all in one line but it wouldn't be just one step and it would be difficult to read.

Related

Google Apps Script indexOf date not working?

function Get_month_column_summary()
{
var month = sheetToPlan.getRange(2, 3).getValue();
Logger.log(month);
var lastCol = summarySheet.getLastColumn();
var lookupRangeValues = summarySheet.getRange(2,1,1,lastCol).getValues();
var concat = [].concat.apply([],lookupRangeValues);
Logger.log(concat);
var index = concat.indexOf(month) + 1;
Logger.log(index);
return index;
}
So basically, I'm trying to find the index of a specific date that I got from sheet 1 and trying to find that same date in sheet 2. But when the index gives me -1 (i added 1 so it shows 0 instead of -1 in logs) for some reason, even though i see that it is in the array, the date that im searching for. It is in the exact same format and wording, 100% the same. but the indexOf still gives me -1. Any idea what is going on here?
[20-05-27 18:21:43:667 HKT] **Fri May 01 00:00:00 GMT+08:00 2020**
[20-05-27 18:21:44:002 HKT] [Sun Jan 01 00:00:00 GMT+08:00 2017,
Wed Feb 01 00:00:00 GMT+08:00 2017,
Wed Mar 01 00:00:00 GMT+08:00 2017,
Sat Apr 01 00:00:00 GMT+08:00 2017,
Mon May 01 00:00:00 GMT+08:00 2017,
Thu Jun 01 00:00:00 GMT+08:00 2017,
Sat Jul 01 00:00:00 GMT+08:00 2017,
Tue Aug 01 00:00:00 GMT+08:00 2017,
Fri Sep 01 00:00:00 GMT+08:00 2017,
Sun Oct 01 00:00:00 GMT+08:00 2017,
Wed Nov 01 00:00:00 GMT+08:00 2017,
Fri Dec 01 00:00:00 GMT+08:00 2017,
Mon Jan 01 00:00:00 GMT+08:00 2018,
Thu Feb 01 00:00:00 GMT+08:00 2018,
Thu Mar 01 00:00:00 GMT+08:00 2018,
Sun Apr 01 00:00:00 GMT+08:00 2018,
Tue May 01 00:00:00 GMT+08:00 2018,
Fri Jun 01 00:00:00 GMT+08:00 2018,
Sun Jul 01 00:00:00 GMT+08:00 2018,
Wed Aug 01 00:00:00 GMT+08:00 2018,
Sat Sep 01 00:00:00 GMT+08:00 2018,
Mon Oct 01 00:00:00 GMT+08:00 2018,
Thu Nov 01 00:00:00 GMT+08:00 2018,
Sat Dec 01 00:00:00 GMT+08:00 2018,
Tue Jan 01 00:00:00 GMT+08:00 2019,
Fri Feb 01 00:00:00 GMT+08:00 2019,
Fri Mar 01 00:00:00 GMT+08:00 2019,
Mon Apr 01 00:00:00 GMT+08:00 2019,
Wed May 01 00:00:00 GMT+08:00 2019,
Sat Jun 01 00:00:00 GMT+08:00 2019,
Mon Jul 01 00:00:00 GMT+08:00 2019,
Thu Aug 01 00:00:00 GMT+08:00 2019,
Sun Sep 01 00:00:00 GMT+08:00 2019,
Tue Oct 01 00:00:00 GMT+08:00 2019,
Fri Nov 01 00:00:00 GMT+08:00 2019,
Sun Dec 01 00:00:00 GMT+08:00 2019,
Wed Jan 01 00:00:00 GMT+08:00 2020,
Sat Feb 01 00:00:00 GMT+08:00 2020,
Sun Mar 01 00:00:00 GMT+08:00 2020,
Wed Apr 01 00:00:00 GMT+08:00 2020,
**Fri May 01 00:00:00 GMT+08:00 2020**,
Mon Jun 01 00:00:00 GMT+08:00 2020,
Wed Jul 01 00:00:00 GMT+08:00 2020,
Sat Aug 01 00:00:00 GMT+08:00 2020,
Tue Sep 01 00:00:00 GMT+08:00 2020,
Thu Oct 01 00:00:00 GMT+08:00 2020,
Sun Nov 01 00:00:00 GMT+08:00 2020,
Tue Dec 01 00:00:00 GMT+08:00 2020,
Fri Jan 01 00:00:00 GMT+08:00 2021,
Mon Feb 01 00:00:00 GMT+08:00 2021,
Mon Mar 01 00:00:00 GMT+08:00 2021,
Thu Apr 01 00:00:00 GMT+08:00 2021,
Sat May 01 00:00:00 GMT+08:00 2021,
Tue Jun 01 00:00:00 GMT+08:00 2021,
Thu Jul 01 00:00:00 GMT+08:00 2021,
Sun Aug 01 00:00:00 GMT+08:00 2021,
Wed Sep 01 00:00:00 GMT+08:00 2021,
Fri Oct 01 00:00:00 GMT+08:00 2021,
Mon Nov 01 00:00:00 GMT+08:00 2021,
Wed Dec 01 00:00:00 GMT+08:00 2021,
Sat Jan 01 00:00:00 GMT+08:00 2022,
Tue Feb 01 00:00:00 GMT+08:00 2022]
[20-05-27 18:21:44:010 HKT] 0.0
This solution may work :
var index = concat.indexOf(month.toString()) + 1;
I you cant to convert all concat data with
var concatstr = []
concat.forEach(e => concatstr.push(e.toString()))

How can I count the rows with unique dates within a ForEach Loop?

I am currently trying to loop through the following data and count the number of rows that have a unique date. i.e. the answer to the dataset below should be 8.
Data Table
My attempt is below, however the if statement I use only ever returns a true value - not sure why it won't return false when 'date' is equal to 'dateCheck[i -1]. When I print the values to logs, they are different but the if statement still returns true?
function rowCount() {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var data = sheet.getRange(2,1,lastRow - 1,17).getValues();
var dateCheck = []
var uniqueDates = 0
data.forEach(function(row,i) {
var date = row[0];
dateCheck.push(date);
if (date != dateCheck[i - 1]) {
Logger.log('new date');
uniqueDates += 1
}
else {
Logger.log('same day');
}
Logger.log(uniqueDates);
});
}
When you fetch date values from a Google Spreadsheet, they are supplied as Javascript Date objects. You can't directly compare the values of two objects, as they will never evaluate to equal.
For example, if you create two identical dates, and compare them directly, like so:
var date1 = new Date(2020,01,01);
var date2 = new Date(2020,01,01);
if(date1 == date2){
console.log('dates are equal');
}else{
console.log('dates are not equal');
}
this will output "dates are not equal"
Instead, you need to store a representation of the dates you can compare against. Your best option is to use Date.getTime(), which returns a timestamp in milliseconds that represents the date.
data.forEach(function(row,i) {
var date = row[0];
dateCheck.push(date.getTime());
if (date.getTime() != dateCheck[i - 1]) {
Logger.log('new date');
uniqueDates += 1
}
else {
Logger.log('same day');
}
}
You mention in your question you are looking for unique dates, but in your code you are really identifying changes in dates between rows. I assume you have your data sorted by date, in which case your current code will work. However, if you don't want to sort your data, or want to guarantee unique dates, you can use indexOf like so:
if (dateCheck.indexOf(date.getTime()) > -1) {
Logger.log('new date');
uniqueDates += 1
}
This checks for any instance of the current date anywhere in your dateCheck array, regardless of sorting. Note that we check if it's > -1, because you could have a match at index 0 of the array.
Try this:
function countUniqueDates() {
const sr=4;
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const rg=sh.getRange(sr,1,sh.getLastRow()-sr+1,sh.getLastColumn());
const v=rg.getValues();
var uA=[];
v.forEach(function(r,i){
var ds=Utilities.formatDate(new Date(r[0]),Session.getScriptTimeZone(),"E MMM dd, yyyy");
if(uA.indexOf(ds)==-1) {
uA.push(ds);
}
});
SpreadsheetApp.getUi().alert(`There are ${uA.length} dates in this sheet.`);
}
Here's my data:
1,2,3,4,5,6
0,1,2,3,4,5
HDR1,HDR2,HDR3,HDR4,HDR5,HDR6
Tue Feb 11 2020 06:00:00 GMT-0700 (Mountain Standard Time),1,2,3,4,5
Tue Feb 11 2020 06:20:00 GMT-0700 (Mountain Standard Time),2,3,4,5,6
Tue Feb 11 2020 06:40:00 GMT-0700 (Mountain Standard Time),3,4,5,6,7
Tue Feb 11 2020 07:00:00 GMT-0700 (Mountain Standard Time),4,5,6,7,8
Tue Feb 11 2020 07:20:00 GMT-0700 (Mountain Standard Time),5,6,7,8,9
Tue Feb 11 2020 07:40:00 GMT-0700 (Mountain Standard Time),6,7,8,9,10
Tue Feb 11 2020 08:00:00 GMT-0700 (Mountain Standard Time),7,8,9,10,11
Tue Feb 11 2020 08:20:00 GMT-0700 (Mountain Standard Time),8,9,10,11,12
Tue Feb 11 2020 08:40:00 GMT-0700 (Mountain Standard Time),9,10,11,12,13
Tue Feb 11 2020 09:00:00 GMT-0700 (Mountain Standard Time),10,11,12,13,14
Tue Feb 11 2020 09:20:00 GMT-0700 (Mountain Standard Time),11,12,13,14,15
Tue Feb 11 2020 09:40:00 GMT-0700 (Mountain Standard Time),12,13,14,15,16
Tue Feb 11 2020 10:00:00 GMT-0700 (Mountain Standard Time),13,14,15,16,17
Tue Feb 11 2020 10:20:00 GMT-0700 (Mountain Standard Time),14,15,16,17,18
Tue Feb 11 2020 10:40:00 GMT-0700 (Mountain Standard Time),15,16,17,18,19
Tue Feb 11 2020 11:00:00 GMT-0700 (Mountain Standard Time),16,17,18,19,20
Tue Feb 11 2020 11:20:00 GMT-0700 (Mountain Standard Time),17,18,19,20,21
Tue Feb 11 2020 11:40:00 GMT-0700 (Mountain Standard Time),18,19,20,21,22
Tue Feb 11 2020 12:00:00 GMT-0700 (Mountain Standard Time),19,20,21,22,23
Tue Feb 11 2020 12:20:00 GMT-0700 (Mountain Standard Time),20,21,22,23,24
Tue Feb 11 2020 12:40:00 GMT-0700 (Mountain Standard Time),21,22,23,24,25
Tue Feb 11 2020 13:00:00 GMT-0700 (Mountain Standard Time),22,23,24,25,26
Tue Feb 11 2020 13:20:00 GMT-0700 (Mountain Standard Time),23,24,25,26,27
Tue Feb 11 2020 13:40:00 GMT-0700 (Mountain Standard Time),24,25,26,27,28
Tue Feb 11 2020 14:00:00 GMT-0700 (Mountain Standard Time),25,26,27,28,29
Tue Feb 11 2020 14:20:00 GMT-0700 (Mountain Standard Time),26,27,28,29,30
Tue Feb 11 2020 14:40:00 GMT-0700 (Mountain Standard Time),27,28,29,30,31
Tue Feb 11 2020 15:00:00 GMT-0700 (Mountain Standard Time),28,29,30,31,32
Tue Feb 11 2020 15:20:00 GMT-0700 (Mountain Standard Time),29,30,31,32,33
Tue Feb 11 2020 15:40:00 GMT-0700 (Mountain Standard Time),30,31,32,33,34
Tue Feb 11 2020 16:00:00 GMT-0700 (Mountain Standard Time),31,32,33,34,35
Tue Feb 11 2020 16:20:00 GMT-0700 (Mountain Standard Time),32,33,34,35,36
Tue Feb 11 2020 16:40:00 GMT-0700 (Mountain Standard Time),33,34,35,36,37
Tue Feb 11 2020 17:00:00 GMT-0700 (Mountain Standard Time),34,35,36,37,38
Tue Feb 11 2020 17:20:00 GMT-0700 (Mountain Standard Time),35,36,37,38,39
Tue Feb 11 2020 17:40:00 GMT-0700 (Mountain Standard Time),36,37,38,39,40
Tue Feb 11 2020 18:00:00 GMT-0700 (Mountain Standard Time),37,38,39,40,41
Tue Feb 11 2020 18:20:00 GMT-0700 (Mountain Standard Time),38,39,40,41,42
Tue Feb 11 2020 18:40:00 GMT-0700 (Mountain Standard Time),39,40,41,42,43
Tue Feb 11 2020 19:00:00 GMT-0700 (Mountain Standard Time),40,41,42,43,44
Tue Feb 11 2020 19:20:00 GMT-0700 (Mountain Standard Time),41,42,43,44,45
Tue Feb 11 2020 19:40:00 GMT-0700 (Mountain Standard Time),42,43,44,45,46
Tue Feb 11 2020 20:00:00 GMT-0700 (Mountain Standard Time),43,44,45,46,47
Tue Feb 11 2020 20:20:00 GMT-0700 (Mountain Standard Time),44,45,46,47,48
Tue Feb 11 2020 20:40:00 GMT-0700 (Mountain Standard Time),45,46,47,48,49
Tue Feb 11 2020 21:00:00 GMT-0700 (Mountain Standard Time),46,47,48,49,50
Tue Feb 11 2020 21:20:00 GMT-0700 (Mountain Standard Time),47,48,49,50,51
Tue Feb 11 2020 21:40:00 GMT-0700 (Mountain Standard Time),48,49,50,51,52
Tue Feb 11 2020 22:00:00 GMT-0700 (Mountain Standard Time),49,50,51,52,53
Tue Feb 11 2020 22:20:00 GMT-0700 (Mountain Standard Time),50,51,52,53,54
Tue Feb 11 2020 22:40:00 GMT-0700 (Mountain Standard Time),51,52,53,54,55
Tue Feb 11 2020 23:00:00 GMT-0700 (Mountain Standard Time),52,53,54,55,56
Tue Feb 11 2020 23:20:00 GMT-0700 (Mountain Standard Time),53,54,55,56,57
Tue Feb 11 2020 23:40:00 GMT-0700 (Mountain Standard Time),54,55,56,57,58
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),55,56,57,58,59
Wed Feb 12 2020 00:20:00 GMT-0700 (Mountain Standard Time),56,57,58,59,60
Wed Feb 12 2020 00:40:00 GMT-0700 (Mountain Standard Time),57,58,59,60,61
Wed Feb 12 2020 01:00:00 GMT-0700 (Mountain Standard Time),58,59,60,61,62
Wed Feb 12 2020 01:20:00 GMT-0700 (Mountain Standard Time),59,60,61,62,63
Wed Feb 12 2020 01:40:00 GMT-0700 (Mountain Standard Time),60,61,62,63,64
Wed Feb 12 2020 02:00:00 GMT-0700 (Mountain Standard Time),61,62,63,64,65
Wed Feb 12 2020 02:20:00 GMT-0700 (Mountain Standard Time),62,63,64,65,66
Wed Feb 12 2020 02:40:00 GMT-0700 (Mountain Standard Time),63,64,65,66,67
Wed Feb 12 2020 03:00:00 GMT-0700 (Mountain Standard Time),64,65,66,67,68
Wed Feb 12 2020 03:20:00 GMT-0700 (Mountain Standard Time),65,66,67,68,69
Wed Feb 12 2020 03:40:00 GMT-0700 (Mountain Standard Time),66,67,68,69,70
Wed Feb 12 2020 04:00:00 GMT-0700 (Mountain Standard Time),67,68,69,70,71
Wed Feb 12 2020 04:20:00 GMT-0700 (Mountain Standard Time),68,69,70,71,72
Wed Feb 12 2020 04:40:00 GMT-0700 (Mountain Standard Time),69,70,71,72,73
Wed Feb 12 2020 05:00:00 GMT-0700 (Mountain Standard Time),70,71,72,73,74
Wed Feb 12 2020 05:20:00 GMT-0700 (Mountain Standard Time),71,72,73,74,75
Wed Feb 12 2020 05:40:00 GMT-0700 (Mountain Standard Time),72,73,74,75,76
Wed Feb 12 2020 06:00:00 GMT-0700 (Mountain Standard Time),73,74,75,76,77
Wed Feb 12 2020 06:20:00 GMT-0700 (Mountain Standard Time),74,75,76,77,78
Wed Feb 12 2020 06:40:00 GMT-0700 (Mountain Standard Time),75,76,77,78,79
Wed Feb 12 2020 07:00:00 GMT-0700 (Mountain Standard Time),76,77,78,79,80
Wed Feb 12 2020 07:20:00 GMT-0700 (Mountain Standard Time),77,78,79,80,81
Wed Feb 12 2020 07:40:00 GMT-0700 (Mountain Standard Time),78,79,80,81,82
Wed Feb 12 2020 08:00:00 GMT-0700 (Mountain Standard Time),79,80,81,82,83
Wed Feb 12 2020 08:20:00 GMT-0700 (Mountain Standard Time),80,81,82,83,84
Wed Feb 12 2020 08:40:00 GMT-0700 (Mountain Standard Time),81,82,83,84,85
Wed Feb 12 2020 09:00:00 GMT-0700 (Mountain Standard Time),82,83,84,85,86
Wed Feb 12 2020 09:20:00 GMT-0700 (Mountain Standard Time),83,84,85,86,87
Wed Feb 12 2020 09:40:00 GMT-0700 (Mountain Standard Time),84,85,86,87,88
Wed Feb 12 2020 10:00:00 GMT-0700 (Mountain Standard Time),85,86,87,88,89
Wed Feb 12 2020 10:20:00 GMT-0700 (Mountain Standard Time),86,87,88,89,90
Wed Feb 12 2020 10:40:00 GMT-0700 (Mountain Standard Time),87,88,89,90,91
Wed Feb 12 2020 11:00:00 GMT-0700 (Mountain Standard Time),88,89,90,91,92
Wed Feb 12 2020 11:20:00 GMT-0700 (Mountain Standard Time),89,90,91,92,93
Wed Feb 12 2020 11:40:00 GMT-0700 (Mountain Standard Time),90,91,92,93,94
Wed Feb 12 2020 12:00:00 GMT-0700 (Mountain Standard Time),91,92,93,94,95
Wed Feb 12 2020 12:20:00 GMT-0700 (Mountain Standard Time),92,93,94,95,96
Wed Feb 12 2020 12:40:00 GMT-0700 (Mountain Standard Time),93,94,95,96,97
Wed Feb 12 2020 13:00:00 GMT-0700 (Mountain Standard Time),94,95,96,97,98
Wed Feb 12 2020 13:20:00 GMT-0700 (Mountain Standard Time),95,96,97,98,99
Wed Feb 12 2020 13:40:00 GMT-0700 (Mountain Standard Time),96,97,98,99,100
Wed Feb 12 2020 14:00:00 GMT-0700 (Mountain Standard Time),97,98,99,100,101
Wed Feb 12 2020 14:20:00 GMT-0700 (Mountain Standard Time),98,99,100,101,102
Wed Feb 12 2020 14:40:00 GMT-0700 (Mountain Standard Time),99,100,101,102,103
Wed Feb 12 2020 15:00:00 GMT-0700 (Mountain Standard Time),100,101,102,103,104
Wed Feb 12 2020 15:20:00 GMT-0700 (Mountain Standard Time),101,102,103,104,105
Wed Feb 12 2020 15:40:00 GMT-0700 (Mountain Standard Time),102,103,104,105,106
Wed Feb 12 2020 16:00:00 GMT-0700 (Mountain Standard Time),103,104,105,106,107
Wed Feb 12 2020 16:20:00 GMT-0700 (Mountain Standard Time),104,105,106,107,108
Wed Feb 12 2020 16:40:00 GMT-0700 (Mountain Standard Time),105,106,107,108,109
Wed Feb 12 2020 17:00:00 GMT-0700 (Mountain Standard Time),106,107,108,109,110
Wed Feb 12 2020 17:20:00 GMT-0700 (Mountain Standard Time),107,108,109,110,111
Wed Feb 12 2020 17:40:00 GMT-0700 (Mountain Standard Time),108,109,110,111,112
Wed Feb 12 2020 18:00:00 GMT-0700 (Mountain Standard Time),109,110,111,112,113
Wed Feb 12 2020 18:20:00 GMT-0700 (Mountain Standard Time),110,111,112,113,114
Wed Feb 12 2020 18:40:00 GMT-0700 (Mountain Standard Time),111,112,113,114,115
Wed Feb 12 2020 19:00:00 GMT-0700 (Mountain Standard Time),112,113,114,115,116
Wed Feb 12 2020 19:20:00 GMT-0700 (Mountain Standard Time),113,114,115,116,117
Wed Feb 12 2020 19:40:00 GMT-0700 (Mountain Standard Time),114,115,116,117,118
Wed Feb 12 2020 20:00:00 GMT-0700 (Mountain Standard Time),115,116,117,118,119
Wed Feb 12 2020 20:20:00 GMT-0700 (Mountain Standard Time),116,117,118,119,120
Wed Feb 12 2020 20:40:00 GMT-0700 (Mountain Standard Time),117,118,119,120,121
Wed Feb 12 2020 21:00:00 GMT-0700 (Mountain Standard Time),118,119,120,121,122
Wed Feb 12 2020 21:20:00 GMT-0700 (Mountain Standard Time),119,120,121,122,123
Wed Feb 12 2020 21:40:00 GMT-0700 (Mountain Standard Time),120,121,122,123,124
Wed Feb 12 2020 22:00:00 GMT-0700 (Mountain Standard Time),121,122,123,124,125
Wed Feb 12 2020 22:20:00 GMT-0700 (Mountain Standard Time),122,123,124,125,126
Wed Feb 12 2020 22:40:00 GMT-0700 (Mountain Standard Time),123,124,125,126,127
Wed Feb 12 2020 23:00:00 GMT-0700 (Mountain Standard Time),124,125,126,127,128
Wed Feb 12 2020 23:20:00 GMT-0700 (Mountain Standard Time),125,126,127,128,129
Wed Feb 12 2020 23:40:00 GMT-0700 (Mountain Standard Time),126,127,128,129,130
Thu Feb 13 2020 00:00:00 GMT-0700 (Mountain Standard Time),127,128,129,130,131
Thu Feb 13 2020 00:20:00 GMT-0700 (Mountain Standard Time),128,129,130,131,132
Thu Feb 13 2020 00:40:00 GMT-0700 (Mountain Standard Time),129,130,131,132,133
Thu Feb 13 2020 01:00:00 GMT-0700 (Mountain Standard Time),130,131,132,133,134
Thu Feb 13 2020 01:20:00 GMT-0700 (Mountain Standard Time),131,132,133,134,135
Thu Feb 13 2020 01:40:00 GMT-0700 (Mountain Standard Time),132,133,134,135,136
Thu Feb 13 2020 02:00:00 GMT-0700 (Mountain Standard Time),133,134,135,136,137
Thu Feb 13 2020 02:20:00 GMT-0700 (Mountain Standard Time),134,135,136,137,138
Thu Feb 13 2020 02:40:00 GMT-0700 (Mountain Standard Time),135,136,137,138,139
Thu Feb 13 2020 03:00:00 GMT-0700 (Mountain Standard Time),136,137,138,139,140
Thu Feb 13 2020 03:20:00 GMT-0700 (Mountain Standard Time),137,138,139,140,141
Thu Feb 13 2020 03:40:00 GMT-0700 (Mountain Standard Time),138,139,140,141,142
Thu Feb 13 2020 04:00:00 GMT-0700 (Mountain Standard Time),139,140,141,142,143
Thu Feb 13 2020 04:20:00 GMT-0700 (Mountain Standard Time),140,141,142,143,144
Thu Feb 13 2020 04:40:00 GMT-0700 (Mountain Standard Time),141,142,143,144,145
Thu Feb 13 2020 05:00:00 GMT-0700 (Mountain Standard Time),142,143,144,145,146
Thu Feb 13 2020 05:20:00 GMT-0700 (Mountain Standard Time),143,144,145,146,147
Thu Feb 13 2020 05:40:00 GMT-0700 (Mountain Standard Time),144,145,146,147,148
Thu Feb 13 2020 06:00:00 GMT-0700 (Mountain Standard Time),145,146,147,148,149
Thu Feb 13 2020 06:20:00 GMT-0700 (Mountain Standard Time),146,147,148,149,150
Thu Feb 13 2020 06:40:00 GMT-0700 (Mountain Standard Time),147,148,149,150,151
Thu Feb 13 2020 07:00:00 GMT-0700 (Mountain Standard Time),148,149,150,151,152
Thu Feb 13 2020 07:20:00 GMT-0700 (Mountain Standard Time),149,150,151,152,153
Thu Feb 13 2020 07:40:00 GMT-0700 (Mountain Standard Time),150,151,152,153,154
Thu Feb 13 2020 08:00:00 GMT-0700 (Mountain Standard Time),151,152,153,154,155
Thu Feb 13 2020 08:20:00 GMT-0700 (Mountain Standard Time),152,153,154,155,156
Thu Feb 13 2020 08:40:00 GMT-0700 (Mountain Standard Time),153,154,155,156,157
Thu Feb 13 2020 09:00:00 GMT-0700 (Mountain Standard Time),154,155,156,157,158
Thu Feb 13 2020 09:20:00 GMT-0700 (Mountain Standard Time),155,156,157,158,159
Thu Feb 13 2020 09:40:00 GMT-0700 (Mountain Standard Time),156,157,158,159,160
Thu Feb 13 2020 10:00:00 GMT-0700 (Mountain Standard Time),157,158,159,160,161
Thu Feb 13 2020 10:20:00 GMT-0700 (Mountain Standard Time),158,159,160,161,162
Thu Feb 13 2020 10:40:00 GMT-0700 (Mountain Standard Time),159,160,161,162,163
Thu Feb 13 2020 11:00:00 GMT-0700 (Mountain Standard Time),160,161,162,163,164

How to replace a row in GScript (Google Sheet) if a column value matches another sheet column value?

I am trying to update or replace the contents in a row in PasteSheet if Column B contains a value that matches another value in CopySheet Column B. The matching row in Copysheet is copied to update the row in PasteSheet. Column Values are numbers i.e 4.0
Here's the code I've been working on, but nothing is happening.
function timestamp(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("IMPORT");
var pasteSheet = ss.getSheetByName("Dashboard");
var lastRow = copySheet.getLastRow()
var lastRow2 = pasteSheet.getLastRow()
for (var i = 1; i <= lastRow2; i++){
// pastesheet values
var pasteRange = pasteSheet.getRange([i], 2);
var pasteValues = pasteRange.getValues();
var newRange = pasteSheet.getRange([i], 1,1,41);
var newValues = newRange.getValues();
for (var u = 1; u <= lastRow; u++){
// copysheet values
var copyRange = copySheet.getRange([u], 2);
var copyValues = copyRange.getValues();
var upRange = copySheet.getRange([u], 1,1,41);
var upValues = upRange.getValues();
if (copyValues == pasteValues){ //
newRange.setValue(upValues);
}
}
}
}
Any help to fix this will be appreciated.
Updating Dashboard rows from Import Sheet that match with Dates in column B
//V8
function updateMatching() {
const ss=SpreadsheetApp.getActive();
const srcsr=4;
const dessr=4;
const srcsh=ss.getSheetByName('IMPORT');
const srcrg=srcsh.getRange(srcsr,1,srcsh.getLastRow()-srcsr+1,srcsh.getLastColumn());
const srcvA=srcrg.getValues();
const dessh=ss.getSheetByName('Dashboard');
const desrg=dessh.getRange(dessr,1,dessh.getLastRow()-dessr+1,dessh.getLastColumn());
const desvA=desrg.getValues();
var oA=desvA;
srcvA.forEach(function(sr,i){
desvA.forEach(function(dr,j){
if(sr[1]==dr[1]) {
oA[j]=srcvA[i];
dessh.getRange(dessr + j,1,1,dessh.getLastColumn()).setBackground("#ffff00");//Changed the background just highlight the updates. You can remove this.
}
});
});
desrg.setValues(oA);
}
//Pre V8 - not much change. I'm still learning es6 so if you want to offer suggestions to that end I'm open to them.
function updateMatching1() {
var ss=SpreadsheetApp.getActive();
var srcsr=4;
var dessr=4;
var srcsh=ss.getSheetByName('IMPORT');
var srcrg=srcsh.getRange(srcsr,1,srcsh.getLastRow()-srcsr+1,srcsh.getLastColumn());
var srcvA=srcrg.getValues();
var dessh=ss.getSheetByName('Dashboard');
var desrg=dessh.getRange(dessr,1,dessh.getLastRow()-dessr+1,dessh.getLastColumn());
var desvA=desrg.getValues();
srcvA.forEach(function(sr,i){
desvA.forEach(function(dr,j){
if(sr[1]==dr[1]) {
desvA[j]=srcvA[i];
dessh.getRange(dessr + j,1,1,dessh.getLastColumn()).setBackground("#ffff00")
}
});
});
desrg.setValues(desvA);
}
IMPORT Sheet Start Data(csv):
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
HDR1,HDR2,HDR3,HDR4,HDR5,HDR6,HDR7,HDR8,HDR9,HDR10,HDR11,HDR12,HDR13,HDR14,HDR15,HDR16,HDR17,HDR18,HDR19,HDR20,HDR21,HDR22,HDR23,HDR24,HDR25,HDR26,HDR27,HDR28,HDR29,HDR30,HDR31,HDR32,HDR33,HDR34,HDR35,HDR36,HDR37,HDR38,HDR39,HDR40,HDR41
Fri Jan 31 2020 00:00:00 GMT-0700 (Mountain Standard Time),1,1,2,1,2,0,2,5,5,4,10,11,11,12,9,5,8,17,18,19,1,1,12,11,9,16,6,26,14,19,15,25,16,0,29,31,20,4,23,0
Sat Feb 01 2020 00:00:00 GMT-0700 (Mountain Standard Time),3,0,2,4,4,4,6,0,7,4,2,12,9,6,7,16,10,16,17,19,3,3,17,23,23,18,12,17,3,28,27,0,2,18,16,0,11,14,32,6
Sun Feb 02 2020 00:00:00 GMT-0700 (Mountain Standard Time),5,2,3,5,1,0,5,6,10,7,3,9,7,6,8,4,9,2,18,4,22,14,5,25,0,12,25,7,12,29,4,3,10,8,31,17,38,28,21,39
Mon Feb 03 2020 00:00:00 GMT-0700 (Mountain Standard Time),7,1,4,4,7,1,0,4,0,9,9,14,3,5,16,7,7,9,3,8,13,13,0,22,24,14,20,6,20,16,5,19,11,31,3,14,19,34,26,36
Tue Feb 04 2020 00:00:00 GMT-0700 (Mountain Standard Time),9,4,2,4,6,0,3,10,9,1,14,11,6,10,9,3,4,12,11,23,21,14,21,27,6,10,10,28,13,7,14,16,29,10,14,33,32,37,10,25
Wed Feb 05 2020 00:00:00 GMT-0700 (Mountain Standard Time),11,6,1,3,9,8,9,8,1,1,6,5,2,11,15,5,1,4,22,24,11,24,0,28,7,5,18,20,13,7,33,25,21,34,4,12,40,13,24,14
Thu Feb 06 2020 00:00:00 GMT-0700 (Mountain Standard Time),13,2,8,5,4,4,11,13,3,5,10,8,3,18,19,21,0,23,6,20,2,24,19,4,26,21,6,1,17,0,17,18,32,16,21,38,3,24,37,29
Fri Feb 07 2020 00:00:00 GMT-0700 (Mountain Standard Time),15,4,1,3,8,0,11,11,5,14,2,11,17,2,14,3,16,4,19,23,2,2,18,20,11,9,20,29,13,7,5,26,39,3,5,1,15,6,23,4
Sat Feb 08 2020 00:00:00 GMT-0700 (Mountain Standard Time),17,2,9,2,8,3,9,11,16,0,4,9,6,19,16,6,7,3,3,18,2,4,26,13,29,31,8,23,22,8,38,0,2,10,3,34,12,4,41,34
Sun Feb 09 2020 00:00:00 GMT-0700 (Mountain Standard Time),19,0,1,0,3,2,8,3,15,8,18,17,13,20,6,11,4,7,16,2,12,13,4,30,30,8,14,36,32,27,0,6,5,26,35,39,0,39,42,28
Mon Feb 10 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,9,8,2,14,14,1,11,4,0,2,12,13,13,2,12,23,8,28,14,1,23,18,33,33,7,17,12,16,21,24,5,20,12,29,39,3,28,28,26
Tue Feb 11 2020 00:00:00 GMT-0700 (Mountain Standard Time),23,5,11,1,2,7,7,12,3,14,2,9,2,5,21,17,5,6,6,14,31,20,28,9,33,29,1,24,7,6,6,3,32,39,45,45,33,12,18,47
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,7,14,12,16,17,10,17,20,9,21,3,5,25,23,5,8,25,9,21,6,25,8,15,24,22,12,33,25,35,4,27,21,13,24,5,37,37,14,4
Thu Feb 13 2020 00:00:00 GMT-0700 (Mountain Standard Time),27,1,0,9,7,3,13,9,5,6,15,2,10,8,14,14,20,12,31,15,14,34,22,9,5,12,4,5,30,2,2,28,44,9,19,17,15,46,10,8
Fri Feb 14 2020 00:00:00 GMT-0700 (Mountain Standard Time),29,4,5,4,7,6,16,10,15,21,19,10,8,7,11,29,28,6,14,11,23,1,3,8,31,3,7,37,19,4,18,32,9,5,44,32,28,8,26,26
Sat Feb 15 2020 00:00:00 GMT-0700 (Mountain Standard Time),31,14,3,6,2,13,18,12,1,13,23,12,3,2,10,2,2,25,1,20,11,5,0,21,10,12,22,23,15,0,4,19,37,1,23,44,1,17,52,4
Sun Feb 16 2020 00:00:00 GMT-0700 (Mountain Standard Time),33,15,12,12,6,6,11,12,3,17,18,12,3,26,4,16,24,3,3,2,29,8,23,37,36,15,26,36,8,34,46,43,44,40,1,34,8,5,0,14
Mon Feb 17 2020 00:00:00 GMT-0700 (Mountain Standard Time),35,12,2,11,21,13,23,19,25,8,17,28,5,4,7,6,17,2,4,16,7,18,28,18,12,9,6,32,38,1,44,35,12,31,35,5,7,33,22,41
Tue Feb 18 2020 00:00:00 GMT-0700 (Mountain Standard Time),37,3,7,9,14,4,24,17,15,1,16,0,25,26,18,3,7,16,1,22,34,39,40,2,23,19,1,6,15,9,33,35,18,8,33,38,8,13,37,37
Wed Feb 19 2020 00:00:00 GMT-0700 (Mountain Standard Time),39,10,7,9,5,2,21,9,5,13,16,27,27,20,7,9,30,7,21,17,11,35,0,31,38,39,6,22,43,5,30,15,38,23,49,43,25,43,5,45
Dashboard Sheet Starting Data(csv):
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
HDR1,HDR2,HDR3,HDR4,HDR5,HDR6,HDR7,HDR8,HDR9,HDR10,HDR11,HDR12,HDR13,HDR14,HDR15,HDR16,HDR17,HDR18,HDR19,HDR20,HDR21,HDR22,HDR23,HDR24,HDR25,HDR26,HDR27,HDR28,HDR29,HDR30,HDR31,HDR32,HDR33,HDR34,HDR35,HDR36,HDR37,HDR38,HDR39,HDR40,HDR41
Fri Jan 31 2020 00:00:00 GMT-0700 (Mountain Standard Time),2,0,0,0,4,3,0,5,1,9,7,9,0,8,10,12,0,8,9,7,0,10,3,5,1,24,25,0,20,6,27,26,8,0,31,34,26,3,12,29
Sat Feb 01 2020 00:00:00 GMT-0700 (Mountain Standard Time),4,2,3,3,0,5,0,5,6,0,7,6,12,0,3,14,5,3,17,11,2,15,2,23,7,9,13,3,2,20,4,1,24,2,31,5,33,27,29,30
Sun Feb 02 2020 00:00:00 GMT-0700 (Mountain Standard Time),6,1,1,3,0,0,4,4,0,6,9,6,12,14,12,4,4,3,5,18,7,20,8,6,13,27,22,21,4,12,3,8,8,7,19,18,15,8,29,3
Mon Feb 03 2020 00:00:00 GMT-0700 (Mountain Standard Time),8,2,1,3,3,8,3,6,2,7,5,8,11,7,15,14,11,19,20,3,19,13,20,1,16,21,10,10,5,15,22,14,31,11,25,24,15,23,11,17
Tue Feb 04 2020 00:00:00 GMT-0700 (Mountain Standard Time),10,2,5,5,4,6,3,11,6,2,0,13,13,10,13,4,5,1,8,5,13,3,25,13,9,17,30,21,27,10,6,15,3,2,29,19,14,16,13,38
Wed Feb 05 2020 00:00:00 GMT-0700 (Mountain Standard Time),12,1,4,2,8,4,5,3,6,7,8,14,17,18,9,13,21,8,10,5,17,2,7,12,14,15,29,5,17,10,16,8,8,6,22,20,23,8,41,35
Thu Feb 06 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,2,8,9,5,4,3,6,7,12,9,9,1,7,8,15,14,21,2,9,9,11,23,19,3,0,27,27,27,4,36,13,9,23,11,38,23,17,5,40
Fri Feb 07 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,3,2,0,1,10,0,2,15,4,17,5,11,6,18,3,4,9,20,12,2,11,25,19,16,12,26,0,21,33,30,17,10,16,1,39,15,6,13,5
Sat Feb 08 2020 00:00:00 GMT-0700 (Mountain Standard Time),18,6,7,6,8,1,5,11,13,2,13,3,1,5,21,9,24,20,14,19,5,4,0,23,27,5,13,7,17,10,9,28,24,1,29,28,42,18,28,14
Sun Feb 09 2020 00:00:00 GMT-0700 (Mountain Standard Time),20,8,2,0,2,7,2,7,2,14,17,2,2,2,7,2,18,1,14,7,4,6,25,4,10,17,34,36,2,19,13,27,22,23,36,3,16,42,7,16
Mon Feb 10 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,4,1,8,14,0,7,15,5,9,18,8,1,5,8,3,15,10,2,10,16,18,17,11,0,7,19,1,33,12,38,20,9,9,33,5,4,43,3,27
Tue Feb 11 2020 00:00:00 GMT-0700 (Mountain Standard Time),24,7,6,12,7,14,5,1,14,1,14,8,2,15,0,25,2,0,2,25,6,24,18,26,8,10,25,21,33,10,27,15,33,30,36,39,38,15,46,9
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,9,0,5,1,2,17,5,18,16,18,17,8,17,0,12,11,4,5,27,5,9,24,21,22,19,3,32,24,12,9,33,19,5,5,1,6,48,43,50
Thu Feb 13 2020 00:00:00 GMT-0700 (Mountain Standard Time),28,5,13,15,13,4,5,1,14,0,17,6,18,17,19,4,6,22,21,30,15,6,11,34,10,32,19,20,29,16,32,42,38,26,37,19,20,4,38,25
Fri Feb 14 2020 00:00:00 GMT-0700 (Mountain Standard Time),30,5,12,13,1,1,7,13,19,13,3,17,4,16,15,14,6,11,12,11,0,11,33,28,4,13,1,2,22,4,40,37,24,27,41,19,22,19,8,24
Sat Feb 15 2020 00:00:00 GMT-0700 (Mountain Standard Time),32,15,12,10,5,15,8,21,6,2,0,9,8,0,28,21,26,13,9,30,3,17,6,8,14,28,36,28,30,41,26,46,36,22,13,39,5,10,21,12
Sun Feb 16 2020 00:00:00 GMT-0700 (Mountain Standard Time),34,10,6,3,1,17,18,11,19,8,9,7,20,20,18,27,21,30,25,12,10,25,14,37,20,32,15,10,23,5,3,0,48,13,26,14,6,39,34,27
Mon Feb 17 2020 00:00:00 GMT-0700 (Mountain Standard Time),36,15,7,19,5,15,17,20,4,19,22,15,24,19,29,3,21,15,12,23,14,11,32,14,10,39,31,24,45,37,29,3,15,22,44,52,52,38,7,47
Tue Feb 18 2020 00:00:00 GMT-0700 (Mountain Standard Time),38,3,0,21,18,19,21,2,1,10,6,15,5,7,29,12,16,28,31,8,5,30,36,1,35,28,8,21,1,9,33,32,24,22,34,51,25,12,36,9
Wed Feb 19 2020 00:00:00 GMT-0700 (Mountain Standard Time),40,6,13,3,19,0,18,8,10,25,6,19,27,8,7,15,35,32,2,25,21,3,25,38,20,12,31,20,6,4,5,49,29,37,50,22,51,21,1,13
Dashboard Data after running script(csv):
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
HDR1,HDR2,HDR3,HDR4,HDR5,HDR6,HDR7,HDR8,HDR9,HDR10,HDR11,HDR12,HDR13,HDR14,HDR15,HDR16,HDR17,HDR18,HDR19,HDR20,HDR21,HDR22,HDR23,HDR24,HDR25,HDR26,HDR27,HDR28,HDR29,HDR30,HDR31,HDR32,HDR33,HDR34,HDR35,HDR36,HDR37,HDR38,HDR39,HDR40,HDR41
Fri Jan 31 2020 00:00:00 GMT-0700 (Mountain Standard Time),2,0,0,0,4,3,0,5,1,9,7,9,0,8,10,12,0,8,9,7,0,10,3,5,1,24,25,0,20,6,27,26,8,0,31,34,26,3,12,29
Sat Feb 01 2020 00:00:00 GMT-0700 (Mountain Standard Time),4,2,3,3,0,5,0,5,6,0,7,6,12,0,3,14,5,3,17,11,2,15,2,23,7,9,13,3,2,20,4,1,24,2,31,5,33,27,29,30
Sun Feb 02 2020 00:00:00 GMT-0700 (Mountain Standard Time),6,1,1,3,0,0,4,4,0,6,9,6,12,14,12,4,4,3,5,18,7,20,8,6,13,27,22,21,4,12,3,8,8,7,19,18,15,8,29,3
Mon Feb 03 2020 00:00:00 GMT-0700 (Mountain Standard Time),8,2,1,3,3,8,3,6,2,7,5,8,11,7,15,14,11,19,20,3,19,13,20,1,16,21,10,10,5,15,22,14,31,11,25,24,15,23,11,17
Tue Feb 04 2020 00:00:00 GMT-0700 (Mountain Standard Time),10,2,5,5,4,6,3,11,6,2,0,13,13,10,13,4,5,1,8,5,13,3,25,13,9,17,30,21,27,10,6,15,3,2,29,19,14,16,13,38
Wed Feb 05 2020 00:00:00 GMT-0700 (Mountain Standard Time),12,1,4,2,8,4,5,3,6,7,8,14,17,18,9,13,21,8,10,5,17,2,7,12,14,15,29,5,17,10,16,8,8,6,22,20,23,8,41,35
Mon Feb 10 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,9,8,2,14,14,1,11,4,0,2,12,13,13,2,12,23,8,28,14,1,23,18,33,33,7,17,12,16,21,24,5,20,12,29,39,3,28,28,26
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,7,14,12,16,17,10,17,20,9,21,3,5,25,23,5,8,25,9,21,6,25,8,15,24,22,12,33,25,35,4,27,21,13,24,5,37,37,14,4
Sat Feb 08 2020 00:00:00 GMT-0700 (Mountain Standard Time),18,6,7,6,8,1,5,11,13,2,13,3,1,5,21,9,24,20,14,19,5,4,0,23,27,5,13,7,17,10,9,28,24,1,29,28,42,18,28,14
Sun Feb 09 2020 00:00:00 GMT-0700 (Mountain Standard Time),20,8,2,0,2,7,2,7,2,14,17,2,2,2,7,2,18,1,14,7,4,6,25,4,10,17,34,36,2,19,13,27,22,23,36,3,16,42,7,16
Mon Feb 10 2020 00:00:00 GMT-0700 (Mountain Standard Time),14,9,8,2,14,14,1,11,4,0,2,12,13,13,2,12,23,8,28,14,1,23,18,33,33,7,17,12,16,21,24,5,20,12,29,39,3,28,28,26
Tue Feb 11 2020 00:00:00 GMT-0700 (Mountain Standard Time),24,7,6,12,7,14,5,1,14,1,14,8,2,15,0,25,2,0,2,25,6,24,18,26,8,10,25,21,33,10,27,15,33,30,36,39,38,15,46,9
Wed Feb 12 2020 00:00:00 GMT-0700 (Mountain Standard Time),16,7,14,12,16,17,10,17,20,9,21,3,5,25,23,5,8,25,9,21,6,25,8,15,24,22,12,33,25,35,4,27,21,13,24,5,37,37,14,4
Thu Feb 13 2020 00:00:00 GMT-0700 (Mountain Standard Time),28,5,13,15,13,4,5,1,14,0,17,6,18,17,19,4,6,22,21,30,15,6,11,34,10,32,19,20,29,16,32,42,38,26,37,19,20,4,38,25
Fri Feb 14 2020 00:00:00 GMT-0700 (Mountain Standard Time),30,5,12,13,1,1,7,13,19,13,3,17,4,16,15,14,6,11,12,11,0,11,33,28,4,13,1,2,22,4,40,37,24,27,41,19,22,19,8,24
Sat Feb 15 2020 00:00:00 GMT-0700 (Mountain Standard Time),32,15,12,10,5,15,8,21,6,2,0,9,8,0,28,21,26,13,9,30,3,17,6,8,14,28,36,28,30,41,26,46,36,22,13,39,5,10,21,12
Sun Feb 16 2020 00:00:00 GMT-0700 (Mountain Standard Time),34,10,6,3,1,17,18,11,19,8,9,7,20,20,18,27,21,30,25,12,10,25,14,37,20,32,15,10,23,5,3,0,48,13,26,14,6,39,34,27
Mon Feb 17 2020 00:00:00 GMT-0700 (Mountain Standard Time),36,15,7,19,5,15,17,20,4,19,22,15,24,19,29,3,21,15,12,23,14,11,32,14,10,39,31,24,45,37,29,3,15,22,44,52,52,38,7,47
Tue Feb 18 2020 00:00:00 GMT-0700 (Mountain Standard Time),38,3,0,21,18,19,21,2,1,10,6,15,5,7,29,12,16,28,31,8,5,30,36,1,35,28,8,21,1,9,33,32,24,22,34,51,25,12,36,9
Wed Feb 19 2020 00:00:00 GMT-0700 (Mountain Standard Time),40,6,13,3,19,0,18,8,10,25,6,19,27,8,7,15,35,32,2,25,21,3,25,38,20,12,31,20,6,4,5,49,29,37,50,22,51,21,1,13
Image of Final Dashboard:

MySQL String to DATE / TIME or TIMESTAMP

In my data set, the start and end time for a task is given as a string. The string contains:
'Day, Date Month YYYY HH:MM:SS GMT'
'Wed, 18 Oct 2017 10:11:03 GMT'
The previous questions on Stack Overflow do not have data in this format and I have been struggling how to convert it into DATE/TIME or TIMESTAMP. Any advice on this would be greatly appreciated!
This post was quite relevant but still does not meet my needs, as the format of the string is different in both cases:
Converting date/time string to unix timestamp in MySQL
Overall, I want to achieve a variable 'time_on_task' which takes the difference per person between their start_time and end_time. Thus, for the following data:
Person TaskID Start_time End_time
Alpha 1 'Wed, 18 Oct 2017 10:10:03 GMT' 'Wed. 18 Oct 2017 10:10:36 GMT'
Alpha 2 'Wed, 18 Oct 2017 10:11:16 GMT' 'Wed, 18 Oct 2017 10:11:28 GMT'
Beta 1 'Wed, 18 Oct 2017 10:12:03 GMT' 'Wed, 18 Oct 2017 10:12:49 GMT'
Alpha 3 'Wed, 18 Oct 2017 10:12:03 GMT' 'Wed, 18 Oct 2017 10:13:13 GMT'
Gamma 1 'Fri, 27 Oct 2017 22:57:12 GMT' 'Sat, 28 Oct 2017 02:00:54 GMT'
Beta 2 'Wed, 18 Oct 2017 10:13:40 GMT' 'Wed, 18 Oct 2017 10:14:03 GMT'
The required output would be something like this:
Person TaskID Time_on_task
Alpha 1 0:00:33 #['Wed, 18 Oct 2017 10:10:36 GMT' - 'Wed, 18 Oct 2017 10:10:03 GMT']
Alpha 2 0:00:12 #['Wed, 18 Oct 2017 10:11:28 GMT' - 'Wed, 18 Oct 2017 10:11:16 GMT']
Beta 1 0:00:46 #['Wed, 18 Oct 2017 10:12:49 GMT' - 'Wed, 18 Oct 2017 10:12:03 GMT']
Alpha 3 0:01:10 #['Sat, 18 Nov 2017 10:13:13 GMT' - 'Sat, 18 Nov 2017 10:12:03 GMT']
Gamma 1 3:03:42 #['Sat, 28 Oct 2017 02:00:54 GMT' - 'Fri, 27 Oct 2017 22:57:12 GMT']
Beta 2 0:00:23 #['Wed, 18 Oct 2017 10:14:03 GMT' - 'Wed, 18 Oct 2017 10:13:40 GMT']
You need STR_TO_DATE() to convert the string to a date. Consider:
select str_to_date(
'Wed, 18 Oct 2017 10:11:03 GMT',
'%a, %d %b %Y %T GMT'
)
Yields:
2017-10-18 10:11:03
Once you strings are converted to dates, you can use timestampdiff() to compute the difference between them, and turn the result back to a time using sec_to_time():
select
person,
taskID,
sec_to_time(
timestampdiff(
second,
str_to_date(Start_time, '%a, %d %b %Y %T GMT'),
str_to_date(End_time, '%a, %d %b %Y %T GMT')
)
) time_on_task
from mytable
Demo on DB Fiddlde:
| person | taskID | time_on_task |
| ------ | ------ | ------------ |
| Alpha | 1 | 00:00:33 |
| Alpha | 2 | 00:00:12 |
| Beta | 1 | 00:00:46 |
| Alpha | 3 | 00:01:10 |
| Gamma | 1 | 03:03:42 |
| Beta | 2 | 00:00:23 |

SQL sum and show row with non-existent sum values

i have 2 tables : dt_user and dt_invoice.
**dt_members :**
id firstname
3 Salim
5 Sara
8 Julie
**dt_invoice**
user_id amount_ht period month year
3 4950 04 2018 04 2018
3 7200 10 2018 10 2018
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
3 8800 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
3 7550 03 2019 03 2019
I want to create a query joining the two table, but i want to show each user_id for PERIOD that there is in table dt_invoice.
**Expected results :**
user_id amount_ht period month year
3 4950 04 2018 04 2018
5 0 04 2018 04 2018 //non-existent record in dt_invoice
8 0 04 2018 04 2018 //non-existent record in dt_invoice
3 7200 10 2018 10 2018
5 0 10 2018 10 2018 //non-existent record in dt_invoice
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
5 0 11 2018 11 2018 //etc ...
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
5 0 12 2018 12 2018
8 0 12 2018 12 2018
3 8800 01 2019 01 2019
5 0 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
5 0 02 2019 02 2018
8 0 02 2019 02 2018
3 7550 03 2019 03 2019
5 0 03 2019 03 2018
8 0 03 2019 03 2018
Thanks in advance for your help, i'm totally stuck ..
SQL datas available here : https://rextester.com/live/LBSEY76360
also in sqlfiddle : http://sqlfiddle.com/#!9/728af3/1
Use a cross join to generate the rows and left join to bring in the values:
select m.user_id, p.period, p.month, p.year,
coalesce(t.amount_ht, 0) as amount_ht
from dt_members m cross join
(select distinct period, month, year from dt_invoice) p left join
dt_invoice t
on t.user_id = m.id and t.period = p.period;
Maybe this would help.
SELECT user_id, amount_ht, period, month, year
FROM dt_invoice
LEFT JOIN dt_members ON user_id = id