This function will delete every thing older than about 2 hours.
function deleteRowBasedUponTime() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const sr = 2;
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getDisplayValues();
Logger.log(JSON.stringify(vs));
const dtv = Utilities.formatDate(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), new Date().getHours() - 2), ss.getSpreadsheetTimeZone(), "hh:mm:ss a").split(/:| /).reduce((a, v, i, arr) => {
if (i == 0) a.sum += parseInt(v) * 3600;
if (i == 1) a.sum += parseInt(v) * 60;
if (i == 2) a.sum += parseInt(v);
if (i == 3 && v == "PM" && parseInt(arr[0]) != 12) a.sum += 43200;
return a;
}, { sum: 0, total: function () { return this.sum } }).total();
Logger.log(dtv)
let k = [];
vs.forEach((r, i) => {
let item = r[0];
let itmv = r[0].split(/:| /).reduce((a, v, i, arr) => {
if (i == 0) a.sum += parseInt(v) * 3600;
if (i == 1) a.sum += parseInt(v) * 60;
if (i == 2) a.sum += parseInt(v);
if (i == 3 && v == "PM" && parseInt(arr[0]) != 12) a.sum += 43200;
return a;
}, { sum: 0, total: function () { return this.sum } }).total();
if (itmv > dtv) {
k.push(r)
}
});
if (k && k.length > 0) {
sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).clearContent();
sh.getRange(sr, 1, k.length, k[0].length).setValues(k);
}
}
Timebased Trigger
function onehourtrigger() {
//this conditional ensures that you do not create more than one trigger for the same function
if(ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "deleteRowBasedUponTime").length == 0) {
ScriptApp.newTrigger("deleteRowBasedUponTime").timeBased().everyHours(1).create();
}
}
You can goto to Google Apps Script Reference and using the search box find any function that you don't understand.
If it's a pure JavaScript function the go here
Try it this way:
function deleteRowBasedUponTime() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Diesel");
const sr = 2;
const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getDisplayValues();
Logger.log(JSON.stringify(vs));
const dtv = Utilities.formatDate(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), new Date().getHours() - 2), ss.getSpreadsheetTimeZone(), "hh:mm:ss a").split(/:| /).reduce((a, v, i, arr) => {
if (i == 0) a.sum += parseInt(v) * 3600;
if (i == 1) a.sum += parseInt(v) * 60;
if (i == 2) a.sum += parseInt(v);
if (i == 3 && v == "PM" && parseInt(arr[0]) != 12) a.sum += 43200;
return a;
}, { sum: 0, total: function () { return this.sum } }).total();
Logger.log(dtv)
let k = [];
vs.forEach((r, i) => {
let item = r[5];
let itmv = r[5].split(/:| /).reduce((a, v, i, arr) => {
if (i == 0) a.sum += parseInt(v) * 3600;
if (i == 1) a.sum += parseInt(v) * 60;
if (i == 2) a.sum += parseInt(v);
if (i == 3 && v == "PM" && parseInt(arr[0]) != 12) a.sum += 43200;
return a;
}, { sum: 0, total: function () { return this.sum } }).total();
if (itmv > dtv) {
k.push(r)
}
});
if (k && k.length > 0) {
sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).clearContent();
sh.getRange(sr, 1, k.length, k[0].length).setValues(k);
}
}
I also set the format to time and went through each item of the list changing them to a more standardized format.
This is the resulting page after running the code:
| HOURS |
MINUTES |
LATITUDE |
LONGITUDE |
ADDRESS |
POSTED AT |
BRAND |
| 0 |
2 |
28.68061 |
14/9/1899 20:49:13 |
Del Paraso 8716, Nuevo Paraso, 31123 Chihuahua, Chih., Mxico |
5:36:55 PM |
ESSO"] |
| 0 |
1 |
28.68068 |
14/9/1899 20:49:06 |
Del Paraso 8720, Nuevo Paraso, 31123 Chihuahua, Chih., Mxico |
5:57:09 PM |
SHELL"] |
| 1 |
1 |
28.68059 |
-106.13241 |
Del Paraso 8716, Nuevo Paraso, 31123 Chihuahua, Chih., Mxico |
9:23:42 PM |
ESSO"] |
| 1 |
0 |
28.68067 |
-106.13255 |
Del Paraso 8720, Nuevo Paraso, 31123 Chihuahua, Chih., Mxico |
5:59:42 PM |
ESSO"] |
| 1 |
1 |
28.68069 |
-106.13256 |
Del Paraso 8720, Nuevo Paraso, 31123 Chihuahua, Chih., Mxico |
6:07:49 PM |
BP"] |
| 2 |
1 |
28.68071 |
-106.13255 |
Del Paraso 8720, Nuevo Paraso, 31123 Chihuahua, Chih., Mxico |
4:42:28 PM |
ESSO"] |