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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么