I have a spreadsheet with multiple sheets each sheet has the same layout with Date in column "A" and Time Elapsed in column "P" I need to calculate days between column A and today on all four sheets. If checkbox in column "N" is set to true then instead calculate column "O" from "A". So far I have been able to code this bit but, need help getting over the hurdle.
function getDateDifference(event) {
var ss = event.source;
var s = ss.getSheetByName('Alpha'); // I need this to run on all four pages
var r = event.source.getActiveRange();
var startDate = sheet.getRange(3,1).getValue();
var endDate = new Date();
var duration = (endDate - startDate)
var r = event.source.getActiveRange();
if (r.getColumn() == 14 && r.getValue() == true) {
var endDate = sheet.getRange(3, 15);
var duration = (endDate - startDate);
sheet.getRange(3, 16).setValue(duration);
}
else {sheet.getRange(3, 16).setValue(duration);}
}
Example sheet for reference https://docs.google.com/spreadsheets/d/1OKFoS17le-Y5SAOecoLE4EJxiKqKVjRLRHtMzwHNwxM/edit?usp=sharing
CodePudding user response:
Calculate Diff in Days for selected sheets
function calcdiffindays() {
const ss = SpreadsheetApp.getActive();
const incl = ['Alpha', 'Sheet2', 'Sheet3'];//Add the desired sheet names
const dt = new Date();
const dtv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate());
ss.getSheets().filter(sh => ~incl.indexOf(sh.getName())).forEach(s => {
s.getRange(3, 1, s.getLastRow() - 2, s.getLastColumn()).getValues().forEach((r, i) => {
if (r[13] == true) {
let d = new Date(r[0]);
let dv = new Date(d.getFullYear(), d.getMonth(), d.getDate());
s.getRange(i 3, 15).setValue(DiffInDays(dtv, dv));
}
})
})
}
function DiffInDays(Day1,Day2) {
if(Day1 && Day2 && (Object.prototype.toString.call(Day1) === '[object Date]') && (Object.prototype.toString.call(Day2) === '[object Date]')) {
var day=86400000;
var t1=new Date(Day1).valueOf();
var t2=new Date(Day2).valueOf();
var d=Math.abs(t2-t1);
var days=Math.floor(d/day);
//Logger.log(days);
return days;
} else {
throw 'Invalid Inputs';
}
}
Test Sheet:
COL1 | COL2 | COL3 | COL4 | COL5 | COL6 | COL7 | COL8 | COL9 | COL10 | COL11 | COL12 | COL13 | COL14 | COL15 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
7/1/2022 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | TRUE | 3 |
7/2/2022 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | TRUE | 4 |
7/3/2022 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | TRUE | 5 |
7/4/2022 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | TRUE | 6 |
7/5/2022 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | TRUE | 7 |
7/6/2022 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | TRUE | 8 |
7/7/2022 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | TRUE | 9 |
7/8/2022 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | TRUE | 10 |
7/9/2022 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | TRUE | 11 |
7/10/2022 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | TRUE | 12 |
7/11/2022 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | TRUE | 13 |
7/12/2022 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | TRUE | 14 |
7/13/2022 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | TRUE | 15 |
7/14/2022 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | TRUE | 16 |
7/15/2022 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | FALSE | |
7/16/2022 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | TRUE | 18 |
7/17/2022 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | FALSE | |
7/18/2022 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | TRUE | 20 |
7/19/2022 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | FALSE | |
7/20/2022 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | TRUE | 22 |
You appeared to be attempting to use a trigger. However, you did not specify a trigger and there were no triggers defined in your spreadsheet. So I went without triggers.
An onEdit Version:
function onEdit(e) {
const sh = e.range.getSheet();
const incl = ['Alpha', 'Sheet2', 'Sheet3'];//Add the desired sheet names
const idx = incl.indexOf(sh.getName())
if (~idx && e.range.columnStart == 14 && e.range.rowStart > 2 && e.value == "TRUE") {
const dt = new Date();
const dtv = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate());
let d = new Date(sh.getRange(e.range.rowStart, 1).getValue());
let dv = new Date(d.getFullYear(), d.getMonth(), d.getDate());
e.range.offset(0, 1).setValue(DiffInDays(dtv, dv));
}
}
CodePudding user response:
To insert timestamps in column O
when the checkboxes in column N
are ticked, use a timestamping script such as yetAnotherTimestamp_ with these parameters:
// [START modifiable parameters]
{
sheetName: /^(Alpha|Beta|Charlie|Delta)$/i,
watchColumn: ['N'],
stampColumn: ['O'],
watchRowStart: 3,
watchRowEnd: Infinity,
timestampFormat: 'yyyy-MM-dd',
overwriteTimestamp: true,
eraseTimestampOnDelete: true,
},
// [END modifiable parameters]
Then put this formula in cell P2
:
=arrayformula(
{
"Days Elapsed";
if( N3:N = "Yes", O3:O - A3:A, iferror(1/0) )
}
)
The formula will give the number of days between the timestamp and the form submission in the rows where a checkbox is ticked.