Home » excel » excel cell reference in javascript

excel cell reference in javascript

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a combobox and a textbox in EXTJS 4.

else if (record.get("type") === "CELL_VALUE" && record.get("apply_before") === true)
    {
        Ext.Msg.alert("Field Validation", "I am Validating the CELL VALUE COMBO VALUE"); //alert message for testing only, this is where validation will occur
    }

In this scenario, “type” is a standard combobox and “appy_before” is a checkbox. If type = Cell Value and apply_before is checked, I need to validate a text box (range_from) contains a valid excel cell location, and that a second text box (range_to) is empty and if validation passes, set isValid = true, else isValid = false.

I know part of it should be as follows:

else if (record.get("type") === "CELL_VALUE" && record.get("apply_before") === true)
    {
            if (rangeFrom != (A1 - XFD1048576)**problem here**) || (rangeTo != null)
                    isValid = false;
    }

can anyone please provide a little more direction please as I have no clue how to check for the valid cell location where the range of cells can be anything from

A1 (first possible valid cell) to XFD1048576 (the last possible valid cell)
How to&Answers:

I put together a little validation function for you:

function validateRange(coordinate) {
    // A little input validation
    if (typeof coordinate != "string" || !coordinate.length) 
        return false;
    // Find the first occurrence of a digit
    var startIndex = coordinate.search(/[\d+]/);
    // The column is the part from the beginning up until the first digit
    var column = coordinate.substring(0, startIndex).toUpperCase();
    // The row is the remainder of the string
    var row = parseInt(coordinate.substring(startIndex), 10);
    // The column is sortable alphabetically so we can check its range,
    // and the row is numeric so we can check it's range as well
    return (column >= "A"   && row >= 1) && 
           (column <= "XFD" && row <= 1048576);
}

Give it a whirl:

var str = "XFD100000";
var result = validateRange(str);

Applied to your context:

if (!validateRange(rangeFrom)) {

I don’t know exactly how fool-proof this is, but maybe it would give you a good starting point.

Demo

Answer:

function validateRange(coordinate) {
    if (typeof coordinate != "string" || !coordinate.length) 
        return false;
    var startIndex = coordinate.search(/[\d+]/);
    var column = coordinate.substring(0, startIndex).toUpperCase();
    var row = parseInt(coordinate.substring(startIndex), 10);
    return (column >= "A" && ((column.length<3 && column <= "ZZ") ||(column.length==3 && column <= "XFD")) && (row >= 1 && row <= 1048576));
}