Getting VALID range of Excel Cells in javascript

In summary: Thanks for letting us know!In summary, a solution for validating a text field in javascript in EXTJS 4 framework, based on the selected value of a text box, is to use the Excel Range object's Cells property and the ASPOSE function CellsHelper.cellNameToIndex to get the valid cells. The framework does not affect the question and a possible solution could involve using a regular expression to validate the input and then checking the column and row ranges.
  • #1
iamjon.smith
117
3
Mod note: The OP has found an answer to his question.
I am validating a text field in javascript in EXTJS 4 framework, based on the selected value of a text box:

Code:
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:

Code:
else if (record.get("type") === "CELL_VALUE" && record.get("apply_before") === true)
    {
            if (rangeFrom != valid excel cell location**problem here**) || (rangeTo != null)
                    isValid = false;
    }

The only direction I have been given so far is:

ASPOSE function: CellsHelper.cellNameToIndex may be used to get the valid cells

but I have no Idea how to implement this as a solution. Please provide any help that is available.

I am as stuck as I can get at this point, as I have no idea where to go from the code that is posted...
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I don't know if this is what you're looking for, but maybe it will be helpful. The Excel Range object has a property named Cells (see http://msdn.microsoft.com/en-us/library/ff196273(v=office.15).aspx). The Cells property represents the cells in a specified range.

The page in the link above has a VBA example that works with the cells in the range A1 to C5.

Code:
Worksheets("Sheet1").Activate 
Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True

The first parameter in Cells is the row number, and the second is the column number, with A = 1, B = 2, and so on.

I don't know anything about the EXTJS 4 framework, but if it works with Excel, it ought to expose ranges and cells.
 
  • #3
I guess the framework really doesn't affect the question. The issue is that I have a textbox that a user can enter a value into (rangeFrom). When the user hits the save button, I need to check and see if the value entered is a valid excel cell location where the value can be anything from

A1 (first possible valid Cell in a worksheet)

to

XFD1048576 (last possible valid Cell in a worksheet)
 
  • #4
Solution

Code:
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);
}

A friend on stack overflow (no link for respect for this site) provided me with this solution that worked perfectly, but I don't know how to mark a question as resolved on this forum.
 
Last edited:
  • #5
There isn't a way to do it, but I added a note in the first post.
 

1. How do I get the valid range of Excel cells in JavaScript?

To get the valid range of Excel cells in JavaScript, you can use the getRange() method from the Range object. This method takes in two parameters, the start and end cells, and returns a Range object that represents the selected range.

2. Can I specify a specific worksheet to get the valid range of cells from?

Yes, you can specify a specific worksheet to get the valid range of cells from using the getSheetByName() method. This method takes in the name of the worksheet as a parameter and returns a Sheet object, which you can then use to get the range of cells.

3. Is it possible to get the valid range of cells from a specific row or column?

Yes, you can get the valid range of cells from a specific row or column by using the getRange() method with a single parameter. For example, to get the valid range of cells from the 5th row, you can use getRange(5). This will return a Range object that represents all the cells in the 5th row.

4. What is the difference between a Range object and a Sheet object?

A Range object represents a selected range of cells, while a Sheet object represents a specific worksheet. You can use the Range object to manipulate the data within the selected cells, while the Sheet object allows you to access and modify the properties of a specific worksheet, such as its name and formatting.

5. How do I loop through the cells in a valid range using JavaScript?

To loop through the cells in a valid range, you can use the getValues() method from the Range object to get an array of values from the selected range. Then, you can use a for loop to iterate through the array and access each cell's value. Alternatively, you can use the forEach() method to iterate through each cell in the range without converting it to an array first.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
5K
  • Materials and Chemical Engineering
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
733
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Precalculus Mathematics Homework Help
Replies
13
Views
304
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top