Handling date format in Pentaho using javascripting - javascript

My input excel sheet has field "date" with two different types of values as shown below
2015-03-02 11:06:35
3/2/2015 4:03:53 AM
I am reading them as "string" and performing below logic
var temp = date.getString();
temp = str2date(temp,"dd.MM.yyyy HH:mm:ss");
I get the below error
*Could not apply the given format dd.MM.yyyy HH:mm:ss on the string for 2015-03-02 11:06:35 : Format.parseObject(String) failed*
I tried reading them as "date" , but I got the below error
Unparseable date: " 2015-03-02 11:06:35 "
How can we handle this error?

A couple ideas from the docs. http://wiki.pentaho.com/display/EAI/JavaScript+Values
First try calling getDate() it should be a function associated with the cell in question.
If that doesn't work try calling getNumber() in excel all dates are represented in floating point. The whole number part is days since jan 1 1970 and the fraction part is the percentage of the way through the day. I'm sure if you look around there is a js wrapper around this functionality.
Another idea would be to determine all the numberformat strings. Loop over them catching errors. The one that doesn't throw an error should be the correct one. Or you could write a small regex pattern to better examine the dates coming out.

Finally, I got result. I will explain step by step please follow steps very carefully.
Step 1: Data Grid => I created one field called fail_date i.e String datatype.
Step 2: Modified Java Script Value => I wrote some code like these.Please follow screen shot.
Step 3: Select values => Here i converted date datatype.
Step 4: Modified Java Script Value 2 => Here also i wrote one code.Please follow screen shot.
Step 5: Select values => Finally, I converted require data format.here correct_date field is your required output.
Step 6: Final output screen will be like these.
Step 7: Below screen shot show whole transformation screen.
Hope this helps.

Can you use a library. I've used xdate its really good. And it parses both of these date strings
var xdate = require('xdate');
xdate('2015-03-02 11:06:35').toDate(); // returns a js date object
xdate('3/2/2015 4:03:53 AM').toDate(); // returns another js date object.

Related

getMonth problem with Google Sheets javascript

getMonth is not a function in this code for Google Sheets:
... Logger.log(ss.getSheetByName("Rendimentos").getRange(aportes-cont,3).getValue().getMonth());
while (ss.getSheetByName("Rendimentos").getRange(aportes-cont,3).getValue().getMonth()===numMes){ ... }
The Logger.log line works perfectly. It shows the number of the month required. But the second line show a message error: getMonth() is not a function.
It just don't make sense the same code works inside the Logger.log, but get stucked inside the while loop.
I don't know what to do.
The error occurs when the original cell does NOT contain the date. In this case, the .getMonth() method tries to determine the month from a value that is not the date.
Try replacing your code with the following:
let rangeData = ss.getSheetByName("Rendimentos").getRange('C:C').getValues().flat();
while (new Date(rangeData[aportes-cont]).getMonth()===numMes){
...
}
It is also good practice to reduce the number of times the script accesses the google sheets data - so in the suggested code I first read the data of the entire column C once, and then work with the resulting data as elements of an array

How to make google app script - calendar createEvent() to accept GSheet inputs from cells populated using array formula

A few days ago I got help from Stack Overflow to modify my then Apps Script code used to make calendar events from info on Google sheet, so as to tick a checkbox whenever an entry from the corresponding row is made and subsequently make new events only when the checkbox is unticked.
function addEvent() {
let webinarCalendar = CalendarApp.getCalendarById("blablablablablabla#gmail.com");
let calendarSheet = SpreadsheetApp.getActiveSheet();
let schedule = calendarSheet.getDataRange().getValues();
schedule.splice(0, 1);
const k = 16; // colIndex of checkbok col
const created = schedule.map(e => [e[k]]);
schedule.forEach(function(entry, i) {
if (entry[k] == true) { return; }
webinarCalendar.createEvent(entry[3], entry[14], entry[15], {description: entry[13]});
created[i][0] = true;
});
calendarSheet.getRange(2, k + 1, created.length, 1).setValues(created);
}
This current code worked just fine until 2 days ago when I updated 3 of the 4 cells with the required inputs to work on an array formula so that they get populated automatically whenever a new row entry is made.
The error on the app script console says :
Exception: The parameters (String,String,String,(class)) don't match the method signature for CalendarApp.Calendar.createEvent.
The parameters required for this createEvent() as per documentation are title(string), start time(string), finish time(string) and description(which is inside a javascript object I think and is also a string). To ensure that the datatype did not somehow get changed in the process of creating array formula, I cross checked the cells with an ISTEXT() and all of the inputs returned TRUE.
Second trial that I made was to change the splice() from (0,1) to (0,2) so that it ignores the first row which has the array formula written into the cells, which also did not fix the issue.
I would greatly appreciate if someone could show me what is causing this issue and help me fix it.
I don't know why it worked previously, but startTime and endTime should be Date.
I have checked that you columns are String.
Reference:
createEvent(title, startTime, endTime, options)
The error on the app script console says : Exception: The parameters (String,String,String,(class)) don't match the method signature for CalendarApp.Calendar.createEvent.
This is simply saying it's reading from data that is not in the proper data type. In this case, perhaps, try encasing the entries with 'new Date(entry[x])' for the respective start and end date/time entries.
For people trying to run the scripts, one underlying cause might be the fact that you may be using US locale when the date have been formatted as UK.
(e.g. Date that App Script is looking for is mm/dd/yyyy tt:tt:tt, but if you click in the formula cell it shows as dd/mm/yyyy tt:tt:tt)
What you would do is to go to Files > General > Locale > (Country of Choice) > Save settings.
You would then reload the page and try if the script is working now without that "Cannot find method createEvent(string,string,string)" error.
The line of code to use in your script would be:
SpreadsheetApp.getActive().setSpreadsheetLocale('en_UK');
You could include it in your onOpen trigger function.

NaN when trying to retrieve date from a SharePoint list using JavaScript

I've found partial solutions but retrieving the date from a SharePoint list adds one more level of fun. The code I've got which is returning NaN is:
var LifeCycleStart = new Date(item.DeviceAvailableFrom);
Obviously the SharePoint column is DeviceAvailableFrom.
I'm not familiar with Sharepoint, but I'd first check that the value of item.DeviceAvailableFrom is either a positive integer, or a string containing a date that Date's constructor is able to parse.

AngularJS/HTML input week format error

I'm using AngularJS to for user to choose week from a input week tag, but there is something bizarre qbout the format.
I've read that the format should be yyyy-W##, and I do this already, but angularJS still gives me this error:
Error: [ngModel:datefmt] Expected `2015-W51` to be a date
http://errors.angularjs.org/1.4.8/ngModel/datefmt?p0=2015-W51
and this is my tag:
<input type="week" ng-if="header.type==='week'" ng-model="entry[header.className]" ng-change="vos.fieldSelectionChanged(field_id,entry.record_id)"/>
So as you see, the input type is week, here, I'm using this tag inside of a ng-repeat, so I'm loading the data from the entry[] array, and the week loaded is 2015-W51.
So please tell me what I'm doing wrong , is there a best practice using this tag with AngularJS?
Thanks !
Edit 1 - more code
I've found this:
http://codepen.io/empirefox/pen/MYyoao
It demonstrates how to use input week with a date, but the problem is that I don't have the data as dates.
And at the right part of the page, you have this :
$scope.value = new Date(2013, 0, 3);
Change it to this :
$scope.value = "2015-W20";
And it gives me the same error. So if we can solve this one, I think we can also solve the problem on my page.
Angular uses native Date object to handle all its datetime-related inputs, but unfortunately Date instances does not have any methods to handle week numbers. It would be the best solution to stick with moment.js.
Coercion from string to ng-model-enabled value (in controller etc.):
$scope.value = moment("2015-W20").toDate();
Coercion from ng-model-enabled value to string (in form submit handler etc.):
moment($scope.value).format("YYYY-[W]WW"); // returns "2015-W20"

jqPlot Graph "TypeError"

In my Firebug, it is showing me:
TypeError: this[r]._ticks[0] is undefined
But in my JS Array, the items are well constructed. Should not be the data problem.
So, as i said it should not be the data problem. The mystery is:
The problem is not consistent, just that SOMETIME JUST GOT WHOLE BLANK.
I have multiple log ranges (e.g, Show 1 week, Show 1 day, Show 1 hour), then sometime some got BLANK like now and at the same time, graphs are OK in some time ranges. And next time, this range got OK and next range got problem. (With this same code, same data)
I am super sure all my Array Rooms are filled with respective data inside. (I got no blank or corrupted array rooms)
I'm outta idea already :S
Please help.
I have the same problem this day, and resoled this by hard coding the second array value is number but not string type. You can do this simply in php code: $output["1"]=(double)$var, then try to use json_encode to output the data.
Here is the difference example:
valid array format:
var data=[[['2014-01', 2],['2014-02',5.12],['2014-03',13.1],['2014-04',33.6],['2014-05',85.9],['2014-06',219.9],['2014-07',39.9],['2014-08',99.9]]];
invalid array format:
var data=[[["2014-02","1920.97"],["2014-03","2040.92"],["2014-04","1704.52"],["2014-05","1630.88"],["2014-06","207.43"]]]
Use parseFloat(var) or parseInt(var) before passing data to the graph.
Where var is your variable or value which you are passing to graph.

Categories