I currently have a start date in cell C1 and end date in C2 of a Google Sheet spreadsheet. I would like to set it up so that if no date is entered in the end date (C2), this cell will be auto-populated with today's date
I have thus far found the following script
function onFormSubmit(e) {
//edit responses sheet name
var responseSheetName = 'Stats';
//Edit colmn number, column in which the date has to be autopopulated
var column = 3;
//Get target row number
var row = e.range.rowStart;
//If no date, pouplate the cell with current date
if(!e.values[column-1]){
SpreadsheetApp.getActive().getSheetByName(responseSheetName).getRange(row, column).setValue(new Date())
}
}
This doesn't seem to be doing the trick so either I am reading it wrong, it is not what I am looking for!
Is this something that is possible?
Related
I have question. We are making a planning tool for one of our internal products. In this tool we can switch the status of an item (in this case a package) manually (drop down menu)(column E). Based on the status it is ending up in one of the employees planning overview tabs. But, every packages has an offline date (column G). This date is the moment that the package will go offline automatically. Based on the offline date I would like to switch the cell status automatically to 'Offline'. The problem is that this cell could also be changed manually (for other status).
I was thinking, may be there is a trick with conditional formatting to also change the value / text inside a cell. When the present date is the 'offline date' or later than the offline date the conditinal formatting will change the status of the cell to: "Offline".
Does anyone have a trick for this? Thank you in advance!
Link to example sheet: Stackoverflow example date changes cell sheet
This function does change the status in column E to 'Offline' if its accompanying offline date in column G is equal to the date the function is running.
Script:
function changeStatusToOffline() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var lastRow = sheet.getLastRow();
// column E for status
var statusRange = sheet.getRange(2, 5, lastRow - 1, 1);
var statusValues = statusRange.getValues();
// column G for offline dates
var offlineDateRange = sheet.getRange(2, 7, lastRow - 1, 1);
var offlineDateValues = offlineDateRange.getValues();
// set time properties of today to 0 to compare date values only
// do the same with the individual offline dates later
var todayDate = new Date();
todayDate.setHours(0,0,0,0);
var todayTime = todayDate.getTime();
// generate status output during iteration of date values
var statusOutput = offlineDateValues.map((offlineDateValue, index) => {
// the same with today's date
var offlineDate = new Date(offlineDateValue);
offlineDate.setHours(0,0,0,0)
var offlineTime = offlineDate.getTime();
// if trigger date value is same as offline date value, change status to offline
if (todayTime == offlineTime)
return ['Offline'];
// use existing value if date are not the same
return statusValues[index];
});
// set the status range by bulk
statusRange.setValues(statusOutput);
}
Note:
Test the script by running it manually
If it successfully does what you need, then proceed on setting up a daily trigger and trigger the function above daily on a specific time. (e.g. Daily 12AM)
This only updates the status IF trigger date is equal to offline date. (This makes the trigger change the status change to offline just once)
Only if you change the offline date to a later date, then it will be possible to automatically update the status when the script runs on that date again.
Before:
After:
I wondered if anyone could help. I have a script where I am pulling out data from a spreadsheet list, where this is a match for this week (basically an events list, to produce a weekly agenda). I will use a for loop to increment the days to add on, but I am just trying to make it work for one day for now...
The first column is the data in format dd/mm/yyy
I am trying to take today's increment by 1 and then search through the list to find a match. The searching etc, I can make work, but the date part is just not playing. I wondered if anyone could advise.
E.g. Date Column A:
06/07/2021
06/07/2021
01/11/2021
01/11/2021
01/11/2021
01/11/2021
02/09/2021
02/09/2021
var selectedDate = row[0];
selectedDate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var currdate = new Date();
currdate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var daystochange = 1;
var newdate = new Date(currdate.getFullYear, currdate.getMonth, currdate.getDay+daystochange );
Could anyone help?
Thanks
Only use Utilities.formatDate() to output dates, not to work with dates.
The JavaScript date object has all you need to work with dates and compare. When you use the Utilities function it converts it to a string, and so you lose all the functionality of the Date object.
Also bear in mind that if you have dates, that are formatted as dates in your sheet, they will automatically be returned as Date objects.
For example, if your sheet has a date in cell A1
var date = Sheet.getRange("A1").getValue()
date instanceof Date // true
Once you have your date, if you want to add one day to it, you can take an approach similar to what you have already done:
var selectedDate = new Date(2021, 1, 15)
var newdate = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate() + 1);
console.log(newdate) // Tue Feb 02 2021 00:00:00
Note - use getDate to return the day of the month, getDay only returns day of the week.
To check if two dates are the same, you can write a function to compare:
function isSameDate(a, b) {
return a instanceof Date &&
b instanceof Date &&
a.getYear() === b.getYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
}
This function will return true if the dates are the same.
Reference
Date
I have a LibreOffice Base form that allows me to manually add rows to a table. However, the first field of every row is almost always a duplicate of the previous rows first field - Date. Via a macro, I want to automatically fill in the date field so I don't have to manually repeat the information.
Using the PriorToReset event handler, I tried the following:
Sub Main
Dim defaultDate as string
End Sub
Sub PriorToReset(event)
dim Form
dim DateField
Form=event.source
DateField = Form.getByName("Date")
if DateField.Text = "" then
defaultDate = Date
DateField.Text = defaultDate
else
defaultDate = DateField.Text
end if
End Sub
This does put the current date into an empty row, but when I fill out the remaining fields and attempt to save the row, it objects by saying that the Date field is empty. I'm looking at todays date in that field, but the system acts like its empty. If I backspace over just the last digit, replace it and hit enter it accepts it.
Obviously just dumping the date into the "Text" property does not set the indicator that data has been entered. I also experimented with:
DateField.setPropertyValue("Text", defaultDate)
But that errors out completely.
How do I simulate data entry via a macro?
Set the date property of DateField using a variable declared as an UNO Date Struct.
First, declare the struct. Then set the values of the individual members of the struct (year, month, day). Then set the date property to equal the struct and commit.
Adapt this code example to suit:
Sub change_a_date
Dim adate As New com.sun.star.util.Date
root_form = ThisComponent.Drawpage.Forms
main_frm = root_form.getByName("MainForm")
adate.year = 1990
adate.month = 7
adate.day = 4
main_frm.getByName("date_bx").date = adate
main_frm.getByName("date_bx").commit
End Sub
Declaration based on example for IsStruct function. See also API reference for UNO Date Struct.
I have a fusion table with two date_time columns. The fist one is the start date (Startdatum) and in the other column is the end date (Einddatum).
I want to do a query with the current date, and only show the KML-lines on a map where the current date lies between the start and end date.
I tried to use the code below to create a string with a date format:
var time_date = new Date();
var day = time_date.getDate();
var month = time_date.getMonth()+1;
var year = time_date.getFullYear();
var date = (year+"."+month+"."+day);
To show the KML-lines on the map I tried to use the following code:
layer = new google.maps.FusionTablesLayer({
map: map,
heatmap: { enabled: false },
query: {
select: "col2",
from: "1mOMP1seJq4FdiNTugsfylZaJc8sKcSlfJKUuTJjv",
where: "'Startdatum' <= date AND 'Einddatum' >= date"
},
options: {
styleId: 2,
templateId: 2
}
});
Unfortunatly the map shows all the KMS-lines regardless what date is in one of the columns.
What am I doing wrong?
the where-clause is wrong, it has to be
where: "Startdatum <= '"+date+"' AND Einddatum >= '"+date+"'"
the date-format seems to be wrong. Although the used format yyyy.MM.dd is defined in the documentation, it doesn't work. The format yyyy-MM-dd currently works for me(but it's not defined in the documentation).
var date = (year+"-"+month+"-"+day);
(in case that day and month be less than 10 they wouldn't match the pattern, but that doesn't seem to be an issue)
Beyond that: when you fix these 2 mentioned parts it currently works(for me), but I've tried it a couple of hours ago and got unstable results.
I am new to Appsript Script.Db
We have a spreadsheet whit about 300 row's of date in the first column date : 3-1-2014 5:50:46.
When we do a function loadDatabaseFromSheet() we get the error date Timestamp is not a number.
ScriptDB cannot store Date objects directly; instead, you must store a representation of the >date and reconstruct it later. If you don't intend to search based on dates, then you can >store the numeric timestamp from the Date object like this:
var date = new Date('1/1/2014');
var item = {
timestamp: date.getTime();
}
var record = db.save(item);
Do we have change all the date by hand in a proper way from 3-1-2014 5:50:46 to '3/1/2014'?
Hope that there is a better way to get this done?
// How and where can i make a representation: ('1/1/2014'); I hope i don't have to change
// all the date's in the spreadsheet by hand ?My spreadsheet has 300 Row's, first collumn =
The script DB cannot save Date datatype by default, so what you need to do is covert it to its time representation and save it to DB as the above info suggests.
As for your question regarding the need to change date format, it will not be necessary just do a (new Date(value)).getTime() at the time of saving to the DB. Where value represents the datetime in the first column.