GAS Sort Multidimensional Array by Date - javascript

My script works sometimes and errors out sometimes. Running a debug shows that my array.sort may be the issue. This is the error:
We're sorry, a server error occurred. Please wait a bit and try again.
If it was the issue, I'm not sure why it would work sometimes and not others.
allData.sort(function(a,b){
a = new Date(a[7]);
b = new Date(b[7]);
return a>b?-1:a<b?1:0;
});
Is there anything wrong with that? The 7th spot of my array contains the date and I want to sort by that.

I believe you need to isolate your ternary operators.
return a>b?-1:(a<b?1:0);

For some reason, GAS seems to like this better:
allData.sort(function(a,b){return((a[7]<b[7])?-1:((a[7]>b[7])?1:0))});
Logger.log(allData)
allData.reverse()
Now it works. Not sure why, but that's the solution.

Related

how to use reducerrors method to handle errors in lightning web component?

Can Anyone explain please why did we write ": [ ] " in line 10. I believe it is the simplest way to write but what exactly is this, can anyone give another example of this to understand please?
you need to look at lines 9 and 10 together. It's just a ?: conditional operator.
If there are errors - use helper function (it probably flattens various error messages, page level, field level, duplicate rules etc) to get 1 simple error message.
Else return an empty array.

javascript formatDate timezone displaying improperly

I'm trying to get this working off a branch of someone else's code, but wanted to take a hack at it, and so far, am not sure where the javascript is defined, etc. Not much knowledge here, but from what I'm understanding, this is suboptimal. I've tried a thing or two, but it breaks the whole page.
The TimeZone seems to display incorrectly: https://i.imgur.com/2OJrQuC.png
And the Javascript is here: https://github.com/mattgphoto/status/blob/master/script.js
It's something to do with formatDate, but am not sure what of the sections needs to be corrected, or if it's all of them. Would someone be able to help?
I think the following line in formatDate function is causing the problem.
return y.replace(/(y+)/g, function (v) {
return x.getFullYear().toString().slice(-v.length)
});
Try replacing that code with
return y;
Hope it works!

Object has no method 'charAt' in jQuery plugin

I am attempting to use the autoNumeric jQuery plug-in which helps with the conversion of various currencies in jQuery.
The plug-in itself works when I use it in a jsFiddle example.
$(function () {
$('.money').autoNumeric('init', {
aSign: '$',
vMin: '-999999999.99',
nBracket: '(,)'
});
});
However, as soon as I integrate it into a big, legacy project, I start receiving the above error on line 194. I know why I'm getting the error - a string is not being passed into the negativeBracket function (negativeBracket(s, nBracket, oEvent) is the signature). Instead, it seems to be a jQuery object - e.fn.init1. I'm confused on how this might be happening. I realize the community may not be able to give a direct answer, but I would love (and will accept as an answer) being pointed in the right direction as nothing has jumped out at me so far.
Update
So, have some additional info that may be of help. It still has me stumped how it's happening (unfortunately, the answers below didn't help to provide any additional insight). When I link in autoNumeric, I key it off of any text field with the class money. It does work as I am typing in the box. I can see see formatting. However, when I tab into a new box, the box I just finished typing in clears itself completely after hitting line 152 in autoNumeric with the same exact error.
#Carlos487 was correct in his answer when he said I have an object that is not a string. I instead have an object that, I believe, is a function. Here's what I'm seeing in Chrome debugger tools:
e.fn.init[1]
> 0: input#price.money required
> context: input#price.money required
length: 1
selector: ""
> __proto__: Object[0]
The "arrowed" items can be further expanded out. I don't know if this provides any more clues, but it's at least something a bit different.
The errors like
no method XXXXX in Object
are produced because you are trying to call obj.XXXX() and obj is not of the desired type, in your particular case a string.
Have you tried in another browser because older or IE can be a little troublesome. I would recomend using chrome developer tools with your legacy app to see if anything else is conflicting or producing the error
I will bet money that you are using a second library which is interfering with jQuery. It has probably overridden $ with its own function.
Try using jQuery instead of $:
jQuery(function () {
jQuery('.money').autoNumeric('init', {
aSign: '$',
vMin: '-999999999.99',
nBracket: '(,)'
});
});
It turns out that the issue was a myriad of issue compounding into the error I saw. A couple things that was happening:
The validator plug-in was wrapping the jQuery object in its own structure (hence the charAt issue).
Once I fixed that, I also learned that some homegrown code was also wiping and rewriting data into the field to provide formatting (which is what autoNumeric is also doing), so autoNumeric would bomb out because it would get a null value and attempt to format it.
There was some other random craziness that also needed cleaned up. So...issue resolved! Still more to work on, but at least this hurdle is past. Thanks all for your help.

javascript localstorage

So, here's my code:
localStorage.setItem("nhac",for(p=0;p<opener.document.getElementsByName('link').length;p++){opener.document.getElementsByName('link').item(p).style.backgroundColor})
I'm so confused i can't think anymore xD, I've been trying all day long to make this work, i can't even explain it,sorry.
I have a popup which i opened from the main page. From the pop up window, im retrieving the elements from which i want to save the background-color so, by saving it in localstorage, it will always remain like that.
I've done others that work well, like this
localStorage.setItem("color_chosen2", opener.document.getElementById('header').style.color);
I don't know why it doesn't work, can anyone help me? It gives me some kind of error( the first one)
thanks
Edit:(sorry, I was in a hurry when i posted this)
The error it gives is "SyntaxError: syntax error"
(I'm using firefox, by the way)
Using the javascript console, this code
for(p=0;p<opener.document.getElementsByName('link').length;p++){opener.document.getElementsByName('link').item(p).style.backgroundColor}
returns the background color of the elements perfectly, but when I try to use it as the value in localStorage , it gives an error.
And no, there's no problem with the syntax localStorage.setItem, it's how it is, thanks.
Edit2: if you need to see how it works better, here's my website. the pop-up opens from the link in the bottom center.
You are trying to set a for loop into a setter which accepts a String. You must set each of the styles individually and with a unique key value.
Also, I don't think item is a method...
Try this...
items = opener.document.getElementsByName('link');
for ( p = 0; p < items.length; p++ ){
localStorage.setItem( p + 'nhac', items[p].style.backgroundColor );
}
Well, noone really effectively helped me, but at least 2 people tried to help, so thank you.
Anyway, i don't know how, I had an idea and the code works now...to anyone interested:
if(localStorage.getItem("color_chosen3")){
for(p=0;p<document.getElementsByName('link').length;p++){document.getElementsByName('link').item(p).style.backgroundColor=localStorage.getItem("color_chosen3")}}}
Problem solved, question closed...whatever. thanks

Why is 'indexOf' not returning anything?

The book I am reading tells me to open up the JavaScript console and try the code "foo: bar".indexOf(":"). I've tried it in many ways. I tried removing quotation marks, putting it inside a show() and alert() function. I just can't seem to tease anything out.
Has something changed in JavaScript? Has the author made a mistake? Am I supposed to get no return? Do I need to append document.write, perhaps? Any help greatly appreciated.
Yes something changed in Firefox 5+
However the console (ctrl-shift-k) still works
In the error console (ctrl-shift-J) you will need to wrap it in alert:
foo:bar is a property definition in json, and indexOf is supposed to deal with a left value (string variable, constant, or at least something that can have characters in it. I don't know why the book you are reading wants you to do this, but it doesn't seem to be correct. The correct way to use indexOf would be :
var myObject = {
foo:"bar"
}
alert(myObject.foo.indexOf("a"));
try like follows, it should work. Generally the indexOf() will return -1 if the value to search for never occurs.
var str="foo:bar";
document.write(str.indexOf(":") + "<br />");
The output should be 3

Categories

Resources