简体   繁体   English

使用xlsx.js实时更新div

[英]Live updating div with xlsx.js

I've got a basic webpage, with a number of divs. 我有一个基本网页,其中包含许多div。 I'm using the xlsx.js package to read data from a number of xlsx files, stored on the webpage's server (ie in the web page's root directory). 我正在使用xlsx.js包从存储在网页服务器(即网页根目录)中的多个xlsx文件读取数据。 I'm polling the xlsx files every second to monitor for changes. 我每秒都要轮询xlsx文件以监视更改。

Below is a basic representation of my code: 下面是我的代码的基本表示形式:

The HTML div setup: HTML div设置:

<script type="text/javascript" src="xlsx.js"></script>

<div class="container" id="main_div">STARTING STRING</div>

The JS: JS:

<script type="text/javascript">
    (function(window, document, undefined) {
        window.onload = run;

        function run() {
            var Source = "Data.xlsx";
            var GetData = new XMLHttpRequest();
            GetData.open("GET", Source, true);
            GetData.responseType = "arraybuffer";

            GetData.onload = function(e) {
                //Receive and process XLSX data into an array
                //Assign 'Cell' as a particular cell in the worksheet
                var Cell = Worksheet['A1'];
                var CellValue = Cell.v;

                //Assign CellValue to the div
                document.getElementById('main_div').innerHTML = CellValue;

                //Start a 1 second poll
                var DataPoll = setTimeout(run, 1000);
            }

            GetData.send();

        }

    })(window, document, undefined);
</script>

The issue I'm having is that when saving changes to cell A1 in the worksheet (on the server itself), main_div (on the client webpage) doesn't always update to the new value. 我遇到的问题是,将更改保存到工作表中的单元格A1(在服务器本身上)时,main_div(在客户端网页上)并不总是更新为新值。 Even on refreshing the client webpage, it will often retain the previous value of A1. 即使刷新客户端网页,它也通常会保留以前的A1值。 I thought this might be a cache issue, so I've used all the required cache-control HTML meta parameters. 我认为这可能是缓存问题,所以我使用了所有必需的缓存控制HTML元参数。

Should I be clearing the div at the start of the run() function? 我应该在run()函数开始时清除div吗?

EDIT: Some further clarifications. 编辑:一些进一步的澄清。 I've been running the webpage on the server itself (localhost), to eliminate the possibility of it being a server connection issue. 我一直在服务器本身(本地主机)上运行该网页,以消除它成为服务器连接问题的可能性。 I've checked the IIS log files, which show only 200 and 304 HTTP status codes. 我检查了IIS日志文件,该文件仅显示200和304 HTTP状态代码。

I've checked the network requests in Chrome Developer Tools, which has highlighted what I initially believed to be the problem. 我已经在Chrome开发者工具中检查了网络请求,突出了我最初认为的问题。 The Data.xlsx file isn't actually being checked each second. 实际上并不是每秒都在检查Data.xlsx文件。 It's 'checking' the file from the cache (HTTP Status Code), which would obviously not contain any changes made to the Excel file. 它正在从缓存(HTTP状态代码)“检查”文件,该文件显然不会包含对Excel文件所做的任何更改。

Given that this indeed looks like a cache issue, and that I've already got all the HTML meta cache-control characteristics set, are there additional steps I can take to prevent any caching of the .xlsx file? 鉴于这确实看起来像是一个缓存问题,并且我已经设置了所有HTML元数据缓存控制特性,是否可以采取其他步骤来防止.xlsx文件的缓存?

在此处输入图片说明

Can you evade caching by using 您可以通过使用来逃避缓存吗

var Source = "Data.xlsx?" + new Date().getTime();

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM