I am trying to figure out how to check if it the current time is Thursday 9:00 AM. I don't care about date in any other way except that it is a Thursday and that it is past 9:00 AM.
I've never written a single google script before and the API here doesn't seem to offer something like what I am looking for: https://developers.google.com/apps-script/reference/contacts/date-field
Here is how far I've gotten:
FORM_OPEN_DATE = "2016-03-09 12:20";
FORM_CLOSE_DATE = "2016-03-09 12:30";
function Initialize() {
if ((FORM_OPEN_DATE !== "") &&
((new Date()).getTime() < parseDate_(FORM_OPEN_DATE).getTime())) {
closeForm();
ScriptApp.newTrigger("openForm")
.timeBased()
.at(parseDate_(FORM_OPEN_DATE))
.create();
}
if (FORM_CLOSE_DATE !== "") {
ScriptApp.newTrigger("closeForm")
.timeBased()
.at(parseDate_(FORM_CLOSE_DATE))
.create();
}
}
My script above works as intended, it will open and close the form between the time frames, however I am trying to figure out how to check based on the day of the week as oppose to exact date. Reason being is that I wish for the form to open itself every thursday at 9:00 AM and close at 8:30 PM.
Is this achievable?
The JavaScript method getDay() returns the day of the week as a number. Sunday is 0, Monday is 1, and so on. So, Thursday would be 4.
if (new Date().getDay() === 4) {//true block};
Related
I have a script working fine however, I cannot work out how to add a popup reminder for after an all day event as started.... example:
All day even is midnight to midnight, I want a notification at 9am that day.
my script uses event.addPopupReminder but this seems like it only works for before the event.
When I manually look, you have the option to add a reminder "On the day at 9am" but how do I code this? using -900 within the code does not work
On the day at 9am
(I've snipped the code)
By default, if the cell is blank, it'll add a notification at 9am the day before, which I want to change to 9am on the day
if (C2 == "") {C2 = '900'}
event.addPopupReminder(C2)
On the spreadsheet, I've entered -900 (for 9am on the day) and -1000 (for 10am on the day), neither have worked.
The function you are asking doesn't exist, but there is a way to do it anyway. You just have to calcul the minutes between 9am and the start of your event manually.
You haven't put a lot of code, so I wrote something that probably need to be adapted to your case.
What the following function do :
Get all events of the day and for each :
Define the minutes between 9am and the start time
Add a reminder
function setReminderTodayEvents() {
var today = new Date();
today.setHours(9);
today.setMinutes(0);
today.setSeconds(0);
var events = CalendarApp.getDefaultCalendar()
.getEventsForDay(today);
for (var x = 0; x < events.length; x++) {
var date_start = events[x].getStartTime();
var delta = (date_start - today) / 1000 / 60; //ms to minute
events[x].addPopupReminder(delta);
}
}
Suggestion : add a trigger to execute that function every day between 7 and 8
References:
getEventsForDay()
getStartTime()
addPopupReminder()
In short, I have code that I would like to trigger on the first day of a specific month. So, for example, I want to run X-script on Jan 1, but not Feb 1st, March 1st etc. Based on my testing, it seems the atDate function requires the year variable and I'd like the trigger to fire every year on Jan 1, rather than just x year.
This is the version of the timed trigger function I've been working with.
function createTrigger() {
ScriptApp.newTrigger("moveNumbersDecember")
.timeBased()
.atDate(xxxx, 1, 1)
.create();
}
Is there any other function I could use, or workaround for atDate()? My other attempts have been with making the A1 cell on a sheet a clock using today(), then trying to make an if function where if A1 = January 1, then run X script. But I'm very new to coding and haven't been able to make it work.
Any ideas/help extremely appreciated.
GAS do not have yearly trigger, but try this workaround. Create a trigger that runs every month. This runs every month BUT only call the function you want to do if January 1 comes via checking the month of current date.
function createMonthlyTrigger() {
// every month, the trigger created will run checkIfJan1()
ScriptApp.newTrigger("checkIfJan1")
.timeBased()
.onMonthDay(1)
.atHour(0)
.create();
}
function checkIfJan1() {
var date = new Date();
// if jan 1, then run moveNumbersDecember()
if (date.getMonth() === 0) {
moveNumbersDecember();
}
}
Background Info
I have a maximum of 15 hours available for work Monday – Friday.
Each day, from Monday – Thursday, I will work a random amount of
hours between 0 – 8.
I can work 0 hours on Monday - Thursday.
On Friday, I will work whatever hours are left from the maximum of 15
hours available.
I must work at least 1 hour on Friday.
Formulas Used
(Cell: C4) Monday: RANDBETWEEN(0-8)
(Cell: D4) Tuesday: RANDBETWEEN(0-8)
(Cell: E4) Wednesday: RANDBETWEEN(0-8)
(Cell: F4) Thursday: RANDBETWEEN(0-8)
(Cell: G4) Friday: 15 – SUM(C4:F4)
Total Hours: SUM(C4:G4)
Issues
The Friday cell will occasionally receive a negative number of hours
worked.
Example Output of Issue
Output
Current Workaround
Update the values generated by RANDBETWEEN by pressing ‘DEL’ on an
empty cell. This forces all the values to change.
Repeat until a positive value is received in the cell for Friday
Google Sheet Settings
Goal
Have the cells update themselves automatically IF a negative value is received in the cell for Friday
Possible Solution/Thoughts
Is there a way to force RANDBETWEEN numbers to update via a formula?
If yes, is there a way to setup a WHILE loop that will update the
RANDBETWEEN values UNTIL the cell for Friday has a positive number?
Is there a way to have a script run on specific cells? The intent is to simulate data for variations on a work schedule.
I did try to accomplish this via a script but wasn’t able to get the cells to update correctly and other times it would not update at all.
function randomTotal()
{
var Monday = SpreadsheetApp.getActiveSheet().getRange('C4');
var Tuesday = SpreadsheetApp.getActiveSheet().getRange('D4');
var Wednesday = SpreadsheetApp.getActiveSheet().getRange('E4');
var Thursday = SpreadsheetApp.getActiveSheet().getRange('F4');
var Friday = SpreadsheetApp.getActiveSheet().getRange('G4');
var FridayValue = SpreadsheetApp.getActiveSheet().getRange('G4').getValue();
while(FridayValue < 0)
{
newTotal(Monday,Tuesday,Wednesday,Thursday,Friday);
FridayValue = SpreadsheetApp.getActiveSheet().getRange('G4').getValue();
}
}
function newTotal(Monday,Tuesday,Wednesday,Thursday,Friday)
{
Monday.setFormula('=RANDBETWEEN(0,8)');
Tuesday.setFormula('=RANDBETWEEN(0,8)');
Wednesday.setFormula('=RANDBETWEEN(0,8)');
Thursday.setFormula('=RANDBETWEEN(0,8)');
Friday.setFormula('=15-SUM(C4:F4)');
}
This can actually be accomplished without Google Apps Script. I would suggest the following formulas for cell D4
=RANDBETWEEN(0,IF(SUM($C4:C4)<=6,8,14-SUM($C4:C4)))
You can then copy/paste this from D4 into E4 and F4 (the formula references will work), and keep C4 and G4 as is. That should do the trick!
You absolutely can accomplish this programmatically, but in general, if it's possible to do without, that's usually the simpler approach.
For a quick explanation of why this works: if the cells to the left sum to <=6, then you can always add up to 8 hours, because it leaves you in the range of <= 14 total. But, if that's not the case, you want to subtract however many hours you already have from 14, as 14 is the max you can have on Mon - Thurs, and get the remaining of at least 1 on Friday.
While majorly overcomplicated given the other answer provided, I did go in and create a script for this.
Here is the code:
function setSame() {
console.log('Start check');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetMaster = ss.getSheetByName("Sheet2");
var sortRange = sheetMaster.getRange('B3:E3');
var forms = ['=RANDBETWEEN(0,8)','=RANDBETWEEN(0,8)','=RANDBETWEEN(0,8)','=RANDBETWEEN(0,8)'];
sortRange.setValues([forms]);
var mon = sheetMaster.getRange('B3').getValue();
sortRange.getCell(1,1).setValue(mon);
var tues = sheetMaster.getRange('C3').getValue();
sortRange.getCell(1,2).setValue(tues);
var wed = sheetMaster.getRange('D3').getValue();
sortRange.getCell(1,3).setValue(wed);
var thurs = sheetMaster.getRange('E3').getValue();
sortRange.getCell(1,4).setValue(thurs);
var fri = sheetMaster.getRange('F3').getValue();
console.log('Monday: '+mon);
console.log('Tuesday: '+tues);
console.log('Wednesday: '+wed);
console.log('Thursday: '+thurs);
console.log('Friday: '+fri);
if(fri < 1) {
console.log("less");
setSame();
}
}
Sheet
The setSame() function is set up with an onChange trigger (Triggers > Add Trigger). The console.log() lines are there for debugging purposes, and are not necessary.
I am sure there are better ways to go about this with a script, but this was the quickest way I could think to solve this.
I'm interested in having my functions be triggered every 30 days. The closest I can find within Google is the once a month option, but that'll vary away from 30 days due to February and the months with 31 days.
Does anyone have any advice on how I could get this to work?
Thanks!
Trigger this every day. It will run the first time then eventually run 30 days later etc.
// trigger this function every day
function myFunction() {
var lastRunDate = new Date(PropertiesService.getScriptProperties().getProperty("lastRunDate"));
var thirtyDaysAgo = new Date(new Date().setDate(new Date().getDate()-30));
if (lastRunDate > thirtyDaysAgo) {
return; //exit because it's not yet been 30 days since last run
} else {
PropertiesService.getScriptProperties().setProperty("lastRunDate", new Date());
}
// do you work here
}
I have a google script that I would like to run automatically every weekday at 8:11 am. I have set my time zones to make sure that everything is correct, but it never seems to work correctly. I am still weak at scripting. Does anyone see where I might have error in this script?
function myFunction(){
try {
var d = new Date();
if (d.getDay() == 6 || d.getDay() == 0) return;
if (d.getHours() != 08 && d.getMinutes() != 11) return; // This will stop the script from running unless it is 8:11am
} catch (e) {
MailApp.sendEmail("pthompson#ucc.on.ca", "Error report", e.message);
}
}
Thank you,
Paul
To run a function based on time, setup a project trigger (under Resources tab) with the Event "Time-driven". You can even setup a notification.
If you wanted to manage this trigger programmatically you could do something like this:
function createTimeTrigger() {
// Once a day at 8AM, near minute 11 for function 'writeSomething()'
var dailyHourNearMinute = ScriptApp.newTrigger("writeSomething")
.timeBased()
.everyDays(1)
.atHour(8)
.nearMinute(11)
.create();
}
You will notice that this creates a trigger for you that can be viewed under 'Current project's triggers'. It will say between 8AM-9AM. As far as google time events, they work well at getting it within the hour but not on the exact minute. The load in the system can also throw this off. So, the best you can really plan for is, between 8AM - 9AM.