Saturday, March 9, 2019

Remove control characters from a string

function removeCC(s) {
  /* Remove NewLine, Carriage Return and Tab characters from a String  */

  r = "";
  for (i=0; i < s.length; i++) {
    if (s.charAt(i) != '\n' &&
        s.charAt(i) != '\r' &&
        s.charAt(i) != '\t') {
      r += s.charAt(i);
      }
    }
  return r;
  }

Same thing with regular expression


function removeCC(s){ 
  return s.replace(/[\n\r\t]/g," "); 
}

Friday, March 1, 2019

Custom button in DataTable

DataTable Example:

(1)Add custom button

"buttons": [{"text": ' Custom Button',
        "action": function () {
                                alert("Custom Button Clicked");
}
        },
]


(2) Button Menu

"buttons":[
                {
                extend: 'collection',
                text: 'Menu',
                className: "menuMain",
                autoClose: true,
                buttons: [
                    {
                        text: 'Item 1',
                        action: function () {
                            alert("Item 1 Selected");
                        }
                    },
                    {
                        text: 'Item 2',
                        action: function () {
                            alert("Item 2 Selected");
                        }
                    }
                ]                           
                             },
                  ]


Monday, February 25, 2019

Get index from a JSON object with value

var data = [{"name":"placeHolder","section":"right"},{"name":"Overview","section":"left"},{"name":"ByFunction","section":"left"},{"name":"Time","section":"left"},{"name":"allFit","section":"left"},{"name":"allbMatches","section":"left"},{"name":"allOffers","section":"left"},{"name":"allInterests","section":"left"},{"name":"allResponses","section":"left"},{"name":"divChanged","section":"right"}];

// Google Chrome user.
var index = data.findIndex(obj => obj.name=="allInterests");

console.log(index);


// for IE user

var index = -1;
for (var i = 0; i < data.length; ++i) {
    if (data[i].name == "allInterests") {
        index = i;
        break;
    }

}

console.log(index);

Monday, January 21, 2019

how to use toLocaleString() and tofixed(2) in javascript


numObj.toLocaleString([locales [, options]])

toLocaleString takes 2 arguments. The first is the locale, the second are the options. As for the options, you are looking for:

minimumFractionDigits

The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).

To be able to set the options without setting the locale, you can pass undefined as first argument:

var num = 2046430;
num.toLocaleString(undefined, {minimumFractionDigits: 2}) // 2,046,430.00

However this also allows the fraction to be longer than 2 digits. So we need to look for one more option called maximumFractionDigits. (Also on that MDN page)

var num = 2046430.123;
num.toLocaleString(undefined, {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
}) // 2,046,430.12

Tuesday, January 1, 2019

Get Height and Width of a Cell in Excel

Private Sub Worksheet_Activate()
    Dim getHeight As Single
    Dim getWidth As Single
   
    For i = 1 To 10
        getHeight = Range("A" + Trim(Str(i))).Height
        getWidth = Range(Chr(65 + i) + "1").Width
     
        Me.UsedRange(i, 1) = "Row  cm : " + Str(Round((getHeight / 72) * 2.54, 2))
        Me.UsedRange(1, i + 1) = Chr(65 + i) + "1 :" + Str(Round((getWidth / 72) * 2.54, 2))
     
    Next
End Sub

Friday, November 30, 2018

10 Things That Require ZERO Talent



B+ with Yourself
  1. Being on Time
  2. Work Ethic
  3. Effort
  4. Body Language
  5. Energy
  6. Attitude
  7. Passion
  8. Being Coach-able
  9. Doing Extra
  10. Being Prepared

Monday, November 5, 2018

Best way to get FILE NAMES from any Folder in Excel


This is one of the easy way to get File Names from your Windows Folder via "Name Manager".


  • At first, create a new Excel worksheet.
  • Then, select Cell A1.
  • Next, go to “Formulas” tab and click “Name Manager” button.
  • In the popup dialog box, click “New” button.


  • Subsequently, in the next dialog box, input “Files” in “Name” field.
  • And change “=Sheet1!$A$1” to “=FILES(Sheet1!$A$1)” in “Refers to:” field.



  • Afterwards, click “OK” and close “Name Manager” dialog box.
  • Later, copy the Windows folder path in Cell A1 and add “\*” at the end of the path.
  • Next, select the Cell A3 and input the function “=INDEX(Files,ROW()-2)”.



At once, a file in this Windows folder will be listed, like the following image.

  • Finally, just copy this function down several rows to list the other files until you see the “#REF!” error. The error means that all files have been listed.