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);
}
spreadsheet for context
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);
}
Related
I am trying to automate moving a row of data from a sheet called Pending to a sheet called Closed if the bid due date (located in column F) is older than 60 days. I am a beginner and use a lot of outside sources to help me build what I want, but I don't fully understand what I am writing, as long as it works.
This is what I have tried to write and I am looking for some fine tuning
function approveRequests() {
// Initialising
var ss = SpreadsheetApp.getActiveSpreadsheet();
var scheduleSheet = ss.getSheetByName("Pending");
var pastSheet = ss.getSheetByName("Closed");
var lastColumn = scheduleSheet.getLastColumn();
for(var i = scheduleSheet.getLastRow(); i > 60; i--){
var dateCell = scheduleSheet.getRange(i, 1).getValue();
if(isValidDate(dateCell)){
var today = new Date();
var test = new Date(dateCell);
if(test < today){
var rangeToMove = scheduleSheet.getRange(i, 1, 1, scheduleSheet.getLastColumn()).getValues();
pastSheet.getRange(pastSheet.getLastRow() + 1, 1, 1, scheduleSheet.getLastColumn()).setValues(rangeToMove);
scheduleSheet.deleteRow(i);
}
}
}
}
function isValidDate(value) {
var dateWrapper = new Date(value);
return !isNaN(dateWrapper.getDate());
}
Move Row is column 1 is older than 60 days:
function approveRequests() {
const ss = SpreadsheetApp.getActive();
const sh1 = ss.getSheetByName("Pending");
const vs1 = sh1.getRange(61,1,sh1.getLastRow() - 60, sh1.getLastColumn()).getValues();
const sh2 = ss.getSheetByName("Closed");
const dt = new Date();
const dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() - 60);
let d = 0;
vs1.forEach((r,i) => {
if (new Date(r[0]).valueOf() < dtv) {
sh2.getRange(sh2.getLastRow() + 1, 1, 1, r.length).setValues([r]);
sh1.deleteRow(i + 61 - d++);
}
});
}
I am currently using this script to move (selected) rows from one google sheet into another using checkbox.
It moves the rows fine but the move keeps duplicating if I do not physically go back and uncheck each row.
1)Am I using the correct script to copy the selected data?
2)and if so what can be done to prevent duplicates?
3)How do i make the checkbox to be a part of the code as well?
Thanks for whatever help I can get.
function moveChecked(){
const sh = SpreadsheetApp.getActive();
const ss = sh.getSheetByName("Original Data Sheet");
const outSheet = sh.getSheetByName("Sheet1");
let data = ss.getRange(2,1,ss.getLastRow()-1,48).getValues();
let out = [];
for (let i = 0; i< data.length; i++){
if (data[i][0] == true){
out.push(data[i]);
}
}
outSheet.getRange(outSheet.getLastRow()+1,1,out.length,48).setValues(out);
}
Move Checked
function moveChecked() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(2, 1, sh.getLastRow() - 1, 48).getValues();
const osh = ss.getSheetByName("Sheet1");
let out = [];
//let d = 0;
vs.forEach((r, i) => {
if (r[0] == true) {
out.push(r);
//sh.deleteRow(i + 2 - d++)
}
});
osh.getRange(osh.getLastRow() + 1, 1, out.length, out[0].length).setValues(out)
}
Looking for a script that will move data from one sheet to another, moving down the columns for each day.
Day 1 copy data in B3:B24 to log sheet column A with date copied in row 1
Day 2 copy data in B3:B24 to log sheet Column B and with date copied in row 1
This is for google sheets
Log Data
function logdata() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Enter your Sheet Name");
const vs = sh.getRange("B3:B24").getDisplayValues();
const lsh = ss.getSheetByName("Enter Log Sheet Name");
const dts = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy")
vs.unshift([dts]);
lsh.getRange(1,getRowWidth(1,lsh,ss) + 1, vs.length, vs[0].length);
}
function dailyTimer() {
if(ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "logdata").length == 0) {
ScriptApp.newTrigger("logdata").timeBased().atHour(1).everyDays(1).create();//You can add more detail if you wish
}
}
function getRowWidth(row, sh, ss) {
var ss = ss || SpreadsheetApp.getActive();
var sh = sh || ss.getActiveSheet();
var row = row || sh.getActiveCell().getRow();
var rcA = [];
if(sh.getLastColumn()){rcA = sh.getRange(row, 1, 1, sh.getLastColumn()).getValues().flat().reverse();}
let s = 0;
for (let i = 0; i < rcA.length; i++) {
if (rcA[i].toString().length == 0) {
s++;
} else {
break;
}
}
return rcA.length - s;
}
I am trying to write a script for sheets that will move all resolved support tickets older than 30 days to the archive sheet. It'll then delete the entries from the current support sheet. If the ticket does not have a completion date it should be skipped. Currently the script is not moving the all the correct cells. Sometimes it moves things that are newer than 30 days and other times it moves things that have null entries. Thanks in advance.
function moveAfterThirty() {
const sh = SpreadsheetApp.getActive();
const ss = sh.getSheetByName("Current Support Requests"); //requests
const dest = sh.getSheetByName("Archived Support Requests"); //destination
let data = ss.getDataRange().getValues(); //all values
let timeRange = getFullColumn('C', 2).getValues(); //date values for the entire Requests Sheet
var length = timeRange.length;
var date = new Date(); //new date
date.setDate(date.getDate() - 30); //setting date to 30 days ago
for (let i = 0; i < timeRange.length; i++){
var d1 = new Date(timeRange[i]); //setting d1 to the date that corresponds with the position in the array
if (d1 < date)
{
ss.getRange(i+1,1,1,14).moveTo(dest.getRange(dest.getLastRow()+1,1,1,14)); //goes to the row and moves all information
ss.deleteRow(i+1); //deletes row
}
else //if no date of completion do nothing
{
}
}
}
function getFullColumn(column, startIndex){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = sheet.getSheetByName("Current Support Requests");
var lastRow = ss.getLastRow();
return sheet.getRange(column+startIndex+':'+column+lastRow)
}
I am expected it to purge all the entries that have been completed more than 30 days ago. So far it is giving me a hodgepodge of some things moved and other things not.
As a guess. Try to change this line:
if (d1 < date)
with:
if (d1.getTime() < date.getTime())
Move after 30 days
function moveAfterThirty() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const dsh = ss.getSheetByName("Sheet1");
const dtv = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 30).valueOf();
let vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
var a = [];
var d = 0;
vs.forEach((r, i) => {
let dv = new Date(r[2]).valueOf();
if (dv < dtv) {
a.push(r);
sh.deleteRow(i + 2 - d++)
}
});
dsh.getRange(dsh.getLastRow() + 1, 1, a.length, a[0].length).setValues(a);
}
function test() {
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
var ss = ss1.getActiveSheet();
var only = ['AutoGR', 'Sheet2', 'Sheet3', 'Sheet4'];
if (only.indexOf(ss.getName()) == -1) return;
var numRows = SpreadsheetApp.getActiveSheet().getLastRow();
var range;
for (var i = 1; i <= numRows; i++) {
range = ss.getRange('B' + i);
if (range.getValues() == "Crate") {
range.offset(0, 4).setValue(3000);
}
}
for (var i = 1; i <= numRows; i++) {
range = ss.getRange('B' + i);
if (range.getValues() == "Pallet") {
range.offset(0, 4).setValue(4200);
}
}
}
What I am trying to achieve is if values in column B = ‘Crate’ then in the same row under column named Qty put value automatically as 3000, if values in column B = ‘Pallet’ then Qty column automatically gets updated as 4200 but when B=‘close operation’ I have to add qty manually.
Setting Column F in multiple sheets using getValues() and setValues()
function test() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const incl = ['AutoGR', 'Sheet2', 'Sheet3', 'Sheet4'];
const shts = ss.getSheets().filter(sh => ~incl.indexOf(sh.getName()));
shts.forEach(sh => {
let bs = sh.getRange(1,2,sh.getLastRow()).getValues();
let fs = sh.getRange(1,6,sh.getLastRow()).getValues();
bs.forEach((r,i) => {
if(r[0] == "Crate") {
fs[i][0] = 3000;
} else if(r[0] == "Pallet") {
fs[i][0] = 4200;
}
})
sh.getRange(1,6,fs.length,1).setValues(fs);
})
}
If this causes problems in other cells in F then you may be forced to write to F one cell at a time but you'll still get better performance on the read side.