0

I have a C# web form where a user inputs data about an excel file into a number of text boxes, including:

  • TXT_FirstDataRow - integer field which represents the first row that contains data
  • TXT_LastDataColumn - string field which represents the last column that contains data
  • TXT_Range - string field which contains the calculated data range using the above two fields
  • TXT_ColumnCount - The number of columns in the calculated range

I am currently using jQuery to automatically calculate the data range using the code below which works perfectly:

$('#TXT_FirstDataRow,#TXT_LastDataColumn').keyup(function () {

    var row = $('#TXT_FirstDataRow').val();
    var column = $('#TXT_LastDataColumn').val();
    var range = 'A' + row + ':' + column; //All ranges start at column A

    $('#TXT_Range').val(range);
});

I would now like to automatically populate the TXT_ColumnCount with the count of the columns. For example:

Range |Column Count
------+------------
A1:C  |3
A7:BX |76

I imagine that this should be made easier as the ranges will always be starting from column A, so the column count should just be the equivalent column number of TXT_LastDataColumn, but I am afraid even this is beyond my slightly limited jQuery knowledge.

Any help would be greatly appreciated.

Thanks

Simon
  • 1,293
  • 5
  • 21
  • 39
  • are you allowed to use excel interop? – Rex Apr 04 '17 at 17:54
  • @Rex Thanks for your reply, I was hoping to do something neat in jQuery/Javascript rather than using any libraries such as npoi or epplus. Also the the server that the web app will be running on does not have office installed so I guess interop is not an option either. – Simon Apr 04 '17 at 18:00
  • take a look at [this](https://github.com/OfficeDev/office-js-docs/blob/master/reference/excel/range.md) example. It is in Javascript. – Rex Apr 04 '17 at 18:09

1 Answers1

0

I found an answer on SO that helped me, link below:

Convert excel column alphabet (e.g. AA) to number (e.g., 25)

function letterToNumbers(string) {
    string = string.toUpperCase();
    var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', sum = 0, i;
    for (i = 0; i < string.length; i++) {
        sum += Math.pow(letters.length, i) * (letters.indexOf(string.substr(((i + 1) * -1), 1)) + 1);
    }
    return sum;
}
Community
  • 1
  • 1
Simon
  • 1,293
  • 5
  • 21
  • 39