'App Script Time Trigger with Condition (i was told impossible)
I need to create a time trigger that requires 3 things.
- Runs every 60 minutes (originally set for 800, but needs to run more often)
- Only runs on rows that have been timestamped over 1 hour
- Only runs on a row if Col13 is TRUE
I have created a simple version that runs perfect, but i dont understand how to integrate with time conditions i need. (Row 2 is an example row, does not need to move)
function TimeTrigger(){
ScriptApp.newTrigger('MoveChecked')
.timeBase()
.atHour(8)
.everyDays(1)
.create();
}
function MoveChecked(){
const sh = SpreadsheetApp.getActive();
const ss = sh.getSheetByName("Shipped_Log");
const outSheet = sh.getSheetByName("Master_Adjustment_Log");
let data = ss.getRange(2,1,ss.getLastRow()-1,23).getValues();
let out = [];
for (let i = 0; i<data.length; i++){
if (data[i][11]== true){
out.push(data[i]);
ss.deleteRow(i+2);
data.splice(i,1);
i--;
}
}
outSheet.getRange(outSheet.getLastRow()+1,1,out.length,23).setValues(out);
}
Solution 1:[1]
Movechecked on a one hour trigger
I assumed timestamp is column 1 but it can be changed easily
function TimeTrigger() {
if (ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "MoveChecked").length == 0) {
ScriptApp.newTrigger('MoveChecked').timeBased().everyHours(1).create()
}
}
function MoveChecked() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Shipped_Log");
const osh = ss.getSheetByName("Master_Adjustment_Log");
let vs = sh.getRange(2, 1, ss.getLastRow() - 1, sh.getLastColumn()).getValues();
let dt = new Date();
let thv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours() - 1).valueOf()
let out = [];
let d = 0;
vs.forEach((r, i) => {
let ctv = new Date(r[0]).valueOf()
if (r[12] == "TRUE" && ctv > thv) {
out.push(r);
sh.deleteRow(i + 2 - d++);
}
})
osh.getRange(osh.getLastRow() + 1, 1, out.length, out[0].length).setValues(out);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cooper |
