Time Trigger a function in Google Sheets - function

I posted the following a few days ago but the answer I received was removed. I am trying again...
In a Google Sheet, the values in column C of sheet 'Daily' are automatically copied and updated from cell C4 in sheet 'Current.'
I would like the value corresponding to each day (in sheet 'Daily') to stop updating at the end of the day.
OR:
Could the value in C4 of 'Current' get copied (without future updating) at a certain time each day in the cell corresponding to that day in 'Daily'?
I hope I wrote this clearly enough. Thank you in advance to anyone who can help or direct me to the right place.
Best,
Lambert
I did not try anything yet because I have no idea where to start. I guess I should learn scripting, but time is short right now...

Related

Adding Date+Time without 12AM moving to the next day

I am using the attached to automate a time entry to go along with the date for a calendar import. The entries don't have times, and the staff will not enter them try as I might. I need to automate them to simplify the entry procedure.
The issue I am facing is that the Calendar API needs the data to be in DATE/TIME format. To do this I need to use the =DATE+TIME formula. When I do so and the time reaches 12:00AM, the dates thereafter change to the following day.
Essentially I need to either override the logic that makes it move into the next day after midnight appears, or I need to tell either the function in column B-C that it can never roll to midnight. I am trying to think of perhaps a way that I can tell the function to reset the time if the date in column A changes to a new day, and if it doesn't change to a new day go ahead and use the existing function and add 5 minutes to the time that is shown previously to it.
I am stumped, any help would be greatly appreciated.
Here is a sheet to show you the issue
Here is the formula I tried, which worked to sort out the problem but did not work with the Calendar API requirements to format at DATE/TIME. Even when using the importrange formula to move the data into a new sheet with the cells formatted as DATE/TIME it still recognizes it as TEXT as this is what the formula prescribes.
=IF(A2<>"",(CONCATENATE(TEXT(A2,"MM/DD/YYYY")&" "&TEXT(B2,"HH:MM:SS"))),"")
I need this to work in both the sheet and in the import to Calendar using the Calendar API requirements through APPScript.
If I understood correctly your question, here a suggestion with a custom Apps Script function called like a normal Google Sheet function.
Open Apps Script Editor and paste the function below
Call the function rebuildDateTime(COL1, COL2) inside your spreadsheet
Spreadsheet:
Code:
function rebuildDateTime(arg0, arg1) {
var date = new Date(arg0);
var str = arg1.toString().split(':');
date.setHours(str[0]);
date.setMinutes(str[1]);
return date;
}
Warning :
Your COL2 (which contains only the time), must be forced to TEXT FORMAT
References :
Create a custom function

Automate copying a value from one cell to another on a specific date

I'm looking for more of a pointer to some documentation of a method here rather than an exact solution, I'm happy with JavaScript, haven't done much Apps Script and I'm moderate to OK familiar with Google Sheets functions but far from expert.
I have part of a Google Sheet with some date specific data on it like this:
Date
Some-Value
1 Jan 2023
123
15 Jan 2023
456
... etc
In another part of a sheet I have a cell with the current value of Some-Value. This cell in fact contains a formula that totals a column on another sheet for the values. In case it's not blindingly obvious, these dates are in fact sprint end dates and the Some-Value is a count of story points extracted from JIRA. So sort of like this:
Current Value of Some-Value
345
On exactly the date in the Date column I want to copy the value from the "Current Value of Some Value" cell into the cell in the "Some Value" column adjacent to that date. I don't care if that copy happens only once on that day, or several times on that day, or every hour on that day, or whatever. I just want it to happen at least once, automatically.
Should I be looking at an Apps Script function to do this, and roughly how should I do that? Or is there a simpler way of using some Google Sheets function to copy that cell?
You could potentially do this with formulas if you are willing to enable iterative calculation for your sheet. In that case, you could then write something like =if(A2=today(),currentValueCell,B2) (I'm assuming your Date/Some-value table is in A1:Bx of a sheet and you are placing the above in B2). This will return the current value only if the date matches, and then when the dates no longer match will just maintain whatever value is already present in the cell.
EDIT
Ah yes, I forgot that the initial state of a self-referencing IF is zero (rather than null) until a TRUE occurs. Try =if(A2=today(),currentValueCell,if(B2<>0,B2,)) to hide the initial zero generated when the date in A2 is not equal to TODAY().
If you need to copy a value than then it's going to be changed or erased, yes, you'll need an AppScript. If you already know something, you'll more than able to set a simple function to do it.
You can look into Installable Triggers that will help you to set when and how frequently you want your script to be fired; and it will notify you if there are errors in any of that executions.
Sometimes, matching dates can be tricky, more if you have them previously written. If you have to do a timestamp, then you'll probably succeed at first. Just for have it handy: https://developers.google.com/google-ads/scripts/docs/features/dates
Remember to always use Logger.log() to track the progress of your code and what it's returning. Good luck and here you can always ask for specific struggles you may find!
(I'm no expert at programming by far, since it's copying only a value, you may find useful to record macros and watch them to learn how to copy in the formats you need - only values, paste format, etc. - if you don't copy, you can also research in .getValue() and .setValue() )
In case anyone else comes across this in future, here is the somewhat creaky Apps Script function I developed to do this. It's my first Google Apps Script function so feel free to tear it to shreds.
I then added a trigger to run this function weekly, which was easier than I had thought -- a bit of google searching for Apps Script time based triggers found me the answer.
Nonetheless thanks to the people who answered earlier because the clues you gave me helped me find the answer in the end.
function updateUnderTest() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = activeSheet.getSheetByName('Supporting Data');
// I12 is the cell that gets updated daily with the story point count
var value = sourceSheet.getRange('I12').getValue();
// D3:E27 is the range containing date : value pairs
var data = sourceSheet.getRange("D3:E27").getValues();
// epochNow will be the current epoch time, in milliseconds
var epochNow = new Date().valueOf();
// Look through the data range to find a matching date
for (nn=0;nn<data.length;++nn) {
// If a match is found to the nearest day, break the loop
if ((data[nn][0].valueOf() < epochNow) && ((epochNow - data[nn][0].valueOf()) < (24*3600*1000) )) {break};
}
// only update if we found a match
if (nn < 24) {
sourceSheet.getRange(nn+3, 5).setValue(value);
}
}

How to stop formulas from updating at a certain time each day in Google Sheets?

I want Google Sheets to stop updating a formula in a cell corresponding to the current date at a certain time each day, but I am not well versed in scripting so I need some help. I have some examples of my data below.
I have a sheet "Time Spent in Statuses" that automatically updates every hour (with a google sheets extension). New 'Keys' are added to the sheet when they are added in the extension, i.e Key '5' doesn't exist now, but when someone creates it in the data source linked with the extension, Key '5' and its corresponding values will be automatically added, so it updates frequently with more data.
In a separate sheet, I track the daily averages of numbers from the auto update in a sheet "Daily Averages" .
Currently in the "Daily Averages" sheet, I use a formula to calculate the average of each column in the "Time Spent in Statuses" sheet. This formula is pre-filled for days upcoming. At the end of each day, I have to go to this sheet and Copy and Paste > Values Only to record the averages for that day and stop the formula from continuing to update the next day. I use the 'Date' column and the Avg columns to show change in the daily averages over time in a dashboard.
What I'm looking for is some kind of logic that will look at the 'Date' column in the "Daily Averages" sheet and if it equals the current date and the time is 11:59 pm (or some other set time), then automatically Copy and Paste > Values Only. Or something similar so that I don't have to go in and manually paste values only every day. Does anyone have any experience doing something like this?
What you're looking for is a time-driven trigger in Apps Script. You can write a script and set it to run daily at a specific time.
There are multiple approaches that you can take when writing the script, but based on the sample that you provided, you can try the following code:
function setDailyAverage() {
let ss = SpreadsheetApp.getActiveSpreadsheet()
let avgsheet = ss.getSheetByName("Daily Averages")
//this is a string with today's date in format mm/dd/yyyy, like in your sample
let today = new Date().toLocaleDateString("en-US")
//this will do a search of today's date and return the row number
let row = avgsheet.createTextFinder(today).findNext().getRow()
let avgcell = avgsheet.getRange(row,2) //gets column 2 in the row corresponding to today
let giavgcell = avgsheet.getRange(row,3) //gets column 3 in the row corresponding to today
//This reads the values in each cell and overwrites the formula with the actual value
avgcell.setValue(avgcell.getValue())
giavgcell.setValue(giavgcell.getValue())
}
To add the script go to Extensions > Apps Script and paste it there, then on the left side go to Triggers > Add Trigger, then the trigger settings should look like this:
This would run the script every day between 11pm and midnight, and pretty much just automate the same "Copy and Paste > Values Only" process that you're following. You still would need to have the pre-filled rows for each date so keep that in mind. With the trigger you could also automate the creation of each row daily but I don't know if this would interfere with your workflow.

Get the previous date from employee record with reference for the current date using 2 sheets [Google Sheets]

I have an excel file and need to update them to Google sheets. The problem is I cannot convert the functions in my excel to Google sheets.
Can anyone help me get the correct function for previous date from employee record with reference for the current date using 2 sheets [Google Sheets]
Excel function:
=AGGREGATE(14,6,Sheet2!K:K/(Sheet2!K:K<X2)/(Sheet2!A:A=A2),1)
I have tried QUERY function but I haven't found any examples online that could get the correct result.
From the 1st sheet 'Main Data to LOAD' I need to adjust column 'G' and 'I' it to get result.
Sheet 2 Employee record history used for checking the correct date for the first sheet.
Here is a mock sheet so anyone can try: (Kindly make a copy first for testing, I have an extra copy but just to be safe. Thanks)
https://docs.google.com/spreadsheets/d/16pZgf7igkt27Ep8a94-5t1M8KEW5wxP5uzSdHCNx_k4/edit?usp=sharing
Let me know if more information is needed.
Kind regards,

Questions about data formatting using UiApp and google spreadsheet

Thank you in advance for any help you can provide. As background, I built a simple UiApp using GAS that I use to populate a google spreadsheet and a calendar with entries about events including time, date, location, etc. I've had it working for awhile but I want to keep improving it and I have a few questions about format and functionality.
1.) I now want the script to copy the information to a second spreadsheet, I've established how to do this, but the second spreadsheet already has some columns in use that I don't want to override and I don't want to just place the info from this Ui into the first 'X' number of columns, is there any easy way to essentially "copy these 5 columns to the first then skip and column and bring in the rest". Here is the code I have for the copy action right now:
var ss = SpreadsheetApp.openById(ssID);
var sheet = ss.getSheets()[0];
sheet.getRange(sheet.getLastRow()+1, 1, 1, 20).setValues([[new Date(), eventTitle, eventDateFrom, eventStartTimeb, eventEndTimeb, eventLocationName, eventLocationCity,eventActivity, eventLeadContact, eventNSLContact, eventContactAttending, eventDepartment, eventStaff, eventMaterials, eventCost, eventIncentive, eventMSTarget, eventSolar, eventNotes,eventRegion]]);
Also, in the same vein as this question, I've been wondering if it is possible to write something that will choose when an entry is copied to the second spreadsheet based on the value of one of the elements. For example, if eventStaff=0 or is blank, the script will copy the designated information to first spreadsheet but not the second.
2.) Date format: I added two listboxes for to capture event time start and event time end and I would like them to show up in the spreadsheet formatted as 00:00 AM/PM, but have only accomplished to get 00:00:00 or whole number so far.
3.) Using multiple elements to fill the location and events portion of calendar entries. This script works to create a basic event with start/end time and a title, but I'd like to use some of the information to fill in the location and description of an event. Is there a way for me to do this or do I need to concatenate those fields into one in able to enter them in the event creation. Current event creation code:
cal.createEvent(eventTitle,eventDateFrom,eventDateTo);
Sorry for the wall of text, if any clarification/additional code sample is needed just ask. Thank you in advance for any help/insight you might be able to provide.
Please don't be offended but I'm afraid your questions are more general programming question than GAS question, by formulating the question you almost answer it by yourself (question 1).
As for question 2, have a look to Utilities.formatDate and you'll get what you want, see also this.
Question 3: see CalendarApp documentation, createEvent, there is a set of optional arguments that suits your needs. - best regards,