So basically I am trying to make a script that saves input and puts it in another table. Once it does this I need it to create a timer that counts from 0 to a week based off the date and time that was transferred across. I was wondering if anyone knows how this can be done cause I haven't found anything online.
Input Table Output Table The date and name would transfer across to the output table and what I am trying to achieve is the timer appearing in the column next to it, on that row.
This is the code I have to transfer the data, but I have nothing to create the timer and am trying to add something into this code to do so.
function saveTax() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var taxForm = ss.getSheetByName('Tax Form');
var lastRow = taxForm.getLastRow();
var targetRow = lastRow + 1;
if (taxForm.getRange('C7').isBlank()){
taxForm.getRange('C7').setBackground('#ff0000');
throw new Error('Required Cell is Missing Info. ');
}
else {
var valuesToCopy = taxForm.getRange('B7').getValues();
taxForm.getRange(targetRow, 2, 1, valuesToCopy.length).setValues([valuesToCopy.flat()]);
var valuesToCopy = taxForm.getRange('C7').getValues();
taxForm.getRange(targetRow, 3, 1, valuesToCopy.length).setValues([valuesToCopy.flat()]);
taxForm.getRange('C7').setBackground('#ffffff');
}
}
If I understood correctly you need update a sheet column called timer, with time spent since a specific date which comes from other column, right? On this case, you can create a App Script custom formula to the cell and update constantly, here an example:
function getTimeSince(dateCell) {
// Get the date from the cell
const dateTime = new Date(Date.parse(dateCell));
// Check if the date is valid
if (!dateTime || !(dateTime instanceof Date)) {
return "Invalid Date";
}
// Calculate the time difference in milliseconds
const now = new Date();
const diff = now.getTime() - dateTime.getTime();
// Limit the difference to one week (7 days)
const limitedDiff = Math.min(diff, 7 * 24 * 60 * 60 * 1000);
// Calculate days, hours, minutes, and seconds
const days = Math.floor(limitedDiff / (1000 * 60 * 60 * 24));
const hours = Math.floor((limitedDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((limitedDiff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((limitedDiff % (1000 * 60)) / 1000);
// Format the time string with leading zeros
const formattedDays = days.toString().padStart(2, '0');
const formattedHours = hours.toString().padStart(2, '0');
const formattedMinutes = minutes.toString().padStart(2, '0');
const formattedSeconds = seconds.toString().padStart(2, '0');
// Return the formatted string (x days hours:minutes:seconds)
return `${formattedDays} days ${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
}
function createTriggerEvent() {
// Create a trigger to clear all data from reports at final of month
ScriptApp.newTrigger('getTimeSince')
.timeBased()
.everyMinutes(5) // You can control the interval here
.create();
}
Then, you'll need run once the createTriggerEvent()
on App Script editor to create the trigger, after you can go to the cell and put the new formula =getTimeSince(A1)
, now it will update the column with the time spent until a week and updates every x minutes.
Does this solve your problem?