使用jsdom和nodeJS加载SpreadJS

时间:2018-09-18 04:38:33

标签: javascript node.js jsdom spreadjs grapecity

我正在尝试使用Jsdom加载SpreadJS,但它给出了错误:需要一个完全支持HTML5 Canvas的浏览器来运行SpreadJS。

jsom doc 表示它支持画布。

我已经为jsom安装了canvas-prebuilt

这是我到目前为止所做的代码:

  

index.js

const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const options = {
    resources: 'usable',
    runScripts: 'dangerously',
};

JSDOM.fromFile('index.html', options).then((dom) => {
    setTimeout(() => {
        console.log(dom.window.document.body.textContent.trim());
    }, 1000);
});
  

index.html

<!DOCTYPE html>
<html>
<head>
    <title>SpreadJS ExcelIO</title>
    <script src="libs/jquery-2.1.3.min.js" type="text/javascript"></script>
    <script src="libs/jquery-ui.min.js" type="text/javascript"></script>
    <link href="libs/gc.spread.sheets.excel2013white.10.1.0.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="libs/gc.spread.sheets.all.10.1.0.min.js"></script>
    <script type="text/javascript" src="libs/gc.spread.excelio.10.1.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var workbook = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
            var worksheet = workbook.getActiveSheet();
            worksheet.getCell(3,3).value("SpreadJS");
            worksheet.getCell(5,5).value("SpreadJS");
            worksheet.getCell(7,7).value("SpreadJS");
        });
    </script>
</head>
<body>
    <div id="ss" style="height:600px ; width :100%; ">sss</div>
</body>
</html>

我们将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您应该能够稍微更改代码以获取Spread对象或其中的信息。

index.html:

 $(document).ready(function () {
        var workbook = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
        var worksheet = workbook.getActiveSheet();
        worksheet.getCell(3,3).value("SpreadJS");
        worksheet.getCell(5,5).value("SpreadJS");
        worksheet.getCell(7,7).value("SpreadJS");
        window.workbook = workbook;
    });

index.js:

JSDOM.fromFile('index.html', options).then((dom) => {
setTimeout(() => {
    console.log(dom.window.workbook.getActiveSheet().getCell(3,3).value());
}, 1000);