UiApp createServerHandler parameter arguments - javascript

Here is the output from a button callback, as you can see it returns a reference to the picker control which is included in the grid control that they were both added to:
[13-09-06 21:59:07:575 ICT] {clientY=83, clientX=100, eventType=click, ctrl=false, meta=false, source=Today, button=1, alt=false, picker=Sat Sep 07 00:00:00 GMT+07:00 2013, screenY=399, screenX=649, y=16, shift=false, x=28}
Here is the code used to hook it all up:
var app = UiApp.createApplication().setWidth(600).setHeight(400);
var submitHandler = app.createServerHandler('eventFormData_Save_');
var dateBtnsHandler = app.createServerHandler('eventDateBtns_');
// create panels
var vPanel = app.createVerticalPanel().setStyleAttributes(css.body);
var sPanel = app.createScrollPanel().setId('sPanel');
// add vPanel to sPanel
sPanel.add(vPanel);
var dToday = getWholeDay(new Date());
var oDateGrid = app.createGrid(3,3);
var oBtnPrev = app.createButton('< Prev').setId('Prev');
var oBtnNext = app.createButton('Next >').setId('Next');
var oBtnToday = app.createButton('Today').setId('Today').setWidth('100');
var oTxtOldDate = app.createTextBox().setId('pickerOldDate').setWidth('50');
var oPickerLabel = app.createLabel('Selected Date:',false).setId('pickerLabel');
var oPicker = app.createDateBox().setId("picker").setName("picker").setWidth('100')
.setFormat(UiApp.DateTimeFormat.DATE_SHORT)
.setValue(dToday).setFocus(true);
oDateGrid.setWidget(0,1,oPickerLabel);
oDateGrid.setWidget(1,0,oBtnPrev).setWidget(1,1,oPicker).setWidget(1,2,oBtnNext);
oDateGrid.setWidget(2,0,oTxtOldDate).setWidget(2,1,oBtnToday);
dateBtnsHandler.addCallbackElement(oDateGrid);
//dateBtnsHandler.addCallbackElement(oTxtOldDate);//tried adding the txtbox explicitly to no effect
oBtnPrev.addClickHandler(outgoingClientHandler).addClickHandler(dateBtnsHandler);
oBtnNext.addClickHandler(outgoingClientHandler).addClickHandler(dateBtnsHandler);
oBtnToday.addClickHandler(outgoingClientHandler).addClickHandler(dateBtnsHandler);
oPicker.addValueChangeHandler(outgoingClientHandler).addValueChangeHandler(dateBtnsHandler);
oTxtOldDate.addValueChangeHandler(dateBtnsHandler);
app.add(oDateGrid);
it's not all the code as it rambles a bit and shouldn't be relevant, while some of what's there relates to later elements - just let me know if it's not clear!
Problem arises when trying to reference the pickerOldDate within the same callback function as for the buttons
{source=picker, eventType=valuechange, picker=Sat Sep 07 00:00:00 GMT+07:00 2013}
So I'm not sure what to be surprised by, the picker being in the button callback parameter, or the pickerOldDate not being in the picker callback parameter. Any advice?
BTW: the point (if it's not clear) is to retain an 'OldValue' for the picker in case users pick a date in the future (not allowed), I need to return the picker value to the one before the callback was invoked. If I've missed a trick in engineering this outcome, I'm happy to go another route - thanks!

Turns out you need to assign the name of any object you hope will appear in the callback, I had only set the Id and it cost me a few hours - Documentation may not be Google's strong point :)
var oTxtOldDate = app.createTextBox().setId('pickerOldDate').setName('pickerOldDate').setWidth('50');
Hope this helps someone else

Related

How to modify my script to loop over multiple calendars instead of just one

I use this script in a google sheet to search a named calendar for events where the title mentions a specific customer name (tracking project dates mostly). This is fine for me to search one calendar, but I'd like to search a group of calendars at once and get all the events from them and then print all the results to the google sheet. I feel like this is trivial, but I'm at a loss of how to properly iterate over the array with the getEvent function
Edit: I was able to figure it out and the script below is quite useful to me for searching many calendars to find all events where a string is matched in that event. The script is run from a menu item added to the sheets ui, and a popup box is shown for the user to pass on the search string. The search checks in the entire event object for the string and you can pass in match, multi-match, negation, etc.
Here is the working code:
// add menu option for
function onOpen () {
var ui = SpreadsheetApp.getUi()
ui.createMenu('Custom Actions')
.addItem('New Calendar Search', 'main')
.addToUi()
}
function main(){
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clearContents();
var header = [["Calendar Address", "Event Title", "Event Location", "Event Start", "Event End"]]
var range = sheet.getRange(1,1,1,5);
range.setValues(header);
// var mycals is an array of google calendar id's; for gsuite, this is the email address of the user which calendar you will be viewing.
var mycals = ['email1#myemail.com','email2#myemail.com','email3#myemail.com']
var ui = SpreadsheetApp.getUi()
var response = ui.prompt('Enter your search string:')
var custname = response.getResponseText()
for (var j=0;j<mycals.length;j++) {
var cal = CalendarApp.getCalendarById(mycals[j]);
// error handling in case you don't have access to any specified calendar
if (!cal) { continue; }
var events = cal.getEvents(new Date("January 1, 2018 00:00:00 EST"), new Date("December 31, 2018 23:59:59 EST"), {search: custname});
for (var i=0;i<events.length;i++) {
var row = sheet.getLastRow() + 1
var details=[[events[i].getOriginalCalendarId(),events[i].getTitle(), events[i].getLocation(), events[i].getStartTime(), events[i].getEndTime()]];
var range=sheet.getRange(row,1,1,5);
range.setValues(details);
}
sheet.sort(4)
}
}
I just get an error "TypeError: Cannot call method "getEvents" of null. (line 8, file "Code")" no matter how I mess around with this code.
Instead of
var mycal = "my.email#myworkdomain.com";
var cal = CalendarApp.getCalendarById(mycal);
try
var cal = CalendarApp.defaulCalendar();
NOTES: It's not possible to use CalendarApp.getCalendarById(Id) to access the default calendar of other users if they didn't shared it with the effective user.
Reference
defaultCalendar()

BIRT: Weird behaviour of parseInt

I am using a report parameter in BIRT.
It a string, which contains the month/year, like: 08/2018
To test the value, I am using the following code. It is located in a Dynamic Text:
var dateStringArray = params["monthYear"].value.split("/");
var date = new Date(parseInt(dateStringArray[1]), parseInt(dateStringArray[0]) - 1, 1);
var t = parseInt(dateStringArray[0]);
t;
If I fill the parameter with 08/2018, I get a NaN, see:
But if I fill the parameter with 07/2018, it is working correctly:
I have tested it with several numbers. It is just not working with 08 and 09. All other number til 10 are working...
This seems to be a weird scenario. Need to raise a bug on this.
But for your workaround, you can make use of the code below in Dynamic Text which is working fine:
var dateStringArray = params["monthYear"].value.split("/");
var monNum;
if (BirtStr.charLength(dateStringArray[0]) == 2)
{monNum = BirtStr.right(dateStringArray[0],1);}
else {monNum = dateStringArray[0];}
monNum;
//var date = new Date(parseInt(dateStringArray[1]), parseInt(dateStringArray[0]) - 1, 1);
var t = parseInt(monNum);
t;

I need to find EST current offset value using javascript

I need to find current EST offset value using javascript without any server side code
Because I am hardcoding Offset value in my javascript file,i need to pass dynamically because of Day Light Saving.
Kindly Help me on that
Thanks in advance..,
Using moment-timezone:
var currentUSEasternTime = moment.tz('America/New_York');
var minutesOffset = currentUSEasternTime.utcOffset(); // -240
var hoursOffset = minutesOffset / 60; // -4
var offsetString1 = currentUSEasternTime.format('Z'); // "-04:00"
var offsetString2 = currentUSEasternTime.format('ZZ'); // "-0400"
Not sure what output you were looking for, so gave you a few options.

Format a date from a timestamp without BST in JavaScript

Consider the following timestamp I get back from the server:
2016-09-15T18:15:00.0000000
If I do the following:
function friendlyTime(timeslot) {
const time = timeslot,
timeDate = new Date(time),
timeHours = `0${timeDate.getHours().toString()}`.slice(-2),
timeMinutes = `0${timeDate.getMinutes().toString()}`.slice(-2);
return `${timeHours}:${timeMinutes}`;
}
const slot = friendlyTime('2016-09-15T18:15:00.0000000');
I get back:
19:15, when what I really want is a reflection of the timestamp without BST, like 18:15.
Is there an easy way to do this at all? My head explodes when it comes to dates and timezones in JavaScript.
It needs to look the same regardless of where anyone is in the world. It shouldn't be reflecting anyone's personal time settings.
Just manipulate the string, one possibility that would support 4 to 6 digit common spec years (or more).
function friendlyTime(timeslot) {
return timeslot.split('T').pop().split(':', 2).join(':');
}
console.log(friendlyTime('2016-09-15T18:15:00.0000000'));
Another, if you only need to support 4 digit years. (Javascript is spec'd for 4)
function friendlyTime(timeslot) {
return timeslot.slice(11, 16);
}
console.log(friendlyTime('2016-09-15T18:15:00.0000000'));
Instead of getHours() and getMinutes() use getUTCHours() and getUTCMinutes(). Also, as Mike McCaughan pointed out in comments, you have to add Z to the end of the string to make sure it will be parsed correctly in all browsers.
function friendlyTime(timeslot) {
const time = timeslot,
timeDate = new Date(time),
timeHours = `0${timeDate.getUTCHours().toString()}`.slice(-2),
timeMinutes = `0${timeDate.getUTCMinutes().toString()}`.slice(-2);
return `${timeHours}:${timeMinutes}`;
}
const slot = friendlyTime('2016-09-15T18:15:00.0000000Z');
console.log(slot);

getFullYear doesn't return anything

Im in the land of JavaScript and I'm trying some functions for my university project, but I got stuck in this part:
function validateDate(){
alert("validateDate");
var date = document.getElementById("dateN");
var yearN = data.getFullYear();
alert(yearN.value);
var dateA = new Date();
var yearA = dateA.getFullYear`enter code here`();
alert(yearA.value);
if(((yearA - yearN)<18) || ((yearA - yearN)>120)){
alert("age between 18 and 120 only.");
data.style.backgroundColor = "red";
}
}
When I try to print the values, nothing happens, Which terrible wrong thing Im doing here guys?
The dateN is comes from another part (which is working =D ).
Sorry if its a very "noob" question,
Thanks in advance!
There is two mistake here :
var date = document.getElementById("dateN");
var yearN = data.getFullYear();
date is refering a DOM element an it is not a date !
data is undefined !
maybe you want make a Date with the value from "dateN" ?
so according to the HTMLElement you have to get his value and create a new Date !
// saying is a input text and a valid date format !
var date = new Date( document.getElementById("dateN").value );
var yearA = dateA.getFullYear`enter code here`();
// ^
// this is not valid o----------------|
You are calling data.getFullYear() -
var yearN = data.getFullYear();
Don't you mean date.getFullYear??

Categories