dongnue2071 2016-07-15 13:31
浏览 48
已采纳

使用while循环使用PhpExcel获取行

I am looking to create javascript arrays with rows taken from an xlsx spread sheet using PHPExcel.

Here is my code

$document.ready({
    var rows = new Array();
    var vals = new Array();

    var i = 0;
while(){
    rows[i] = getRow(i);
    vals[i] = getVal(i);
    i++;
}
});

function getRow(i){
    if(window.XMLHttpRequest){
        xmlhttp= new XMLHttpRequest();
    }else{
        xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
    }


    xmlhttp.onreadystatechange = function (){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            return xmlhttp.responseText;
        }

    }


    xmlhttp.open('GET', 'data.inc.php?x='+i, true);
    xmlhttp.send();

}   

function getVal(i){
    if(window.XMLHttpRequest){
        xmlhttp= new XMLHttpRequest();
    }else{
        xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
    }


    xmlhttp.onreadystatechange = function (){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            return xmlhttp.responseText;
        }

    }


    xmlhttp.open('GET', 'include.inc.php?x='+i, true);
    xmlhttp.send();

}   

I am not sure what to check for in the parameter of the while loop (Im assuming we do not know how many rows are in the spreadsheet)

Is that my only issue or this the wrong way to go about it?

Also the function getRow return the entire row and getVal returns one column that will be important elsewhere on the page.

  • 写回答

1条回答 默认 最新

  • doudao8283 2016-07-15 15:14
    关注

    There are different approaches you can use:

    1) Synchronise your requests:

    function getRow(i, rows){
        ...
    
        xmlhttp.onreadystatechange = function (){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                if xmlhttp.responseText != '' {
                    rows[i] = xmlhttp.responseText;
                    getRow(i+1, rows)
                } else {
                    // call function that works with rows.
                }
            }
        }
        ...
    }   
    

    This function is only called with getRows(0, rows) (no loop!).

    This is definitely the slowest approach because every request is started as soon as the previous request has finished.

    2) Send number of rows first:

    You could send the number of rows with the first or with each request, so javascript knows how many rows there are. Then you can loop over the rows and create asynchronous calls as you do now.

    3) Send all rows at once:

    I don't know your use case, but it looks like a waste of time to call every row separately. Why not make one call and return all of the data at once with linebreaks as delimiters (or something else suiting your data). If your data is really huge you could still break down the data into large chunks and combine this with option 1 or option 2.

    4) Send data at page load:

    Not sure if this is an option it looks like it is since you execute your function on document.ready. You could consider writing your data into a special hidden div and read the data from there (with javascript) into your variables. That way you avoid all the ajax calls. This is probably the fastest if you want to load all data anyway.

    Note: you might consider using jQuery which makes working with ajax a lot easier. Check jQuery get for example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?