javascriptexcelalasql

JavaScript check excel document for matching numbers


I have a simple input form where you type 6 numbers, when you click the button the JavaScript will check a excel document if the numbers you entered matches a row. if so you win if not you loose.

I cant workout how to check the spread sheet to query every row to see is the numbers match.

in the document column c to h has the 6 numbers.

How can i alter my code to check this document ?

Please see code bellow.

Javascript

var lotteryNumbers = alasql('SELECT * FROM XLSX("results")');

function EnterNumber()
{
    var person = prompt("Please enter your raffle number");

    if(person != null)
    {
        checkIfYouHaveWinningNumber(person);
    }
}

function checkIfYouHaveWinningNumber(number)
{
    if(contains(number))
    {
        alert("Congratulation you won - " + number);
    }
    else
    {
        alert("You lose - " + number);
    }
}

function contains(number)
{
    for(var i = 0;i < lotteryNumbers.length;i++)
    {
        if(lotteryNumbers[i] == number)
        {
            return true;
        }
    }
    return false;
}

Solution

  • Try this - ALASQL has the very good querying feature

    The example like below could be helful to query the table

    https://jsfiddle.net/gt2osvha/

    console.clear()
    
    var data = [{a:1,b:10}, {a:21,b:20}, {a:111,b:30}];
    var num = window.prompt();
    if(num) {
        var q = "SELECT a FROM ? WHERE a=" + num;
        if(alasql(q,[data]).length > 0){
          alert("you won"); 
       } else {
       alert("lost");
       }
     }