【问题标题】:TypeScript unable to load / use table2excelTypeScript 无法加载/使用 table2excel
【发布时间】:2018-11-23 01:59:46
【问题描述】:

我在我的用于 SharePoint Online 的 TypeScript webpart 中使用了这个 plugin

我已将jquerytable2excel 包含在脚本中,与table2excel 无关的所有内容都可以正常工作。

import * as $ from 'jquery';
require('table2excel');

我已经使用npm i table2excel 安装了“table2excel”

然后,当我尝试使用 table2excel 时,它会返回以下错误: ```

$(...).table2excel 不是函数 ```

(<any>$("#ViewTablehidden")).table2excel({
    exclude: ".noExl",
    name: "title",
    filename: "title",
    fileext: ".xls",
    exclude_img: true,
    exclude_links: true,
    exclude_inputs: true
});

为什么我不能让它工作?

【问题讨论】:

  • 这个? // npm install table2excel --save import 'table2excel';常量 Table2Excel = 窗口.Table2Excel; const table2excel = new Table2Excel(选项);
  • @Cristian 不工作,它说,属性“table2excel”在类型窗口中不存在

标签: jquery typescript sharepoint-online


【解决方案1】:

你拉错了包裹。您需要使用jquery-table2excel 而不是table2excel

看起来 NPM 包超时了。该包可通过 Bower 获得(您可以将其引用为导入),或者您可以直接链接到 CDN 资产。这是一个example,向您展示直接从 URL 导入。

$('#download').on('click', function(){
  $(".table2excel").table2excel({
    exclude: ".noExl",
    name: "Excel Document Name",
    filename: "myExcelFile.xls",
    fileext: ".xls",
    exclude_img: true,
    exclude_links: true,
    exclude_inputs: true
  });  
});
body {
  font-family: Arial;
  margin: 0;
  padding: 0;
}

table {
  border-collapse: collapse;
}

table thead tr th {
  background: #f0f0f0;
  border-bottom: 2px solid #ddd;
}

table th,
table td {
  padding: 5px;
}

button {
  background: navy;
  color: white;
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.rawgit.com/rainabba/jquery-table2excel/1.1.0/dist/jquery.table2excel.min.js"></script>

<table class="table2excel" data-tableName="Test Table 1">
  <thead>
    <tr class="noExl"><th>This shouldn't get exported</th><th>This shouldn't get exported either</th></tr>
    <tr><th>This Should get exported as a header</th><th>This should too</th></tr>
  </thead>
  <tbody>
    <tr>
      <td>data1a with a <a href="#">link one</a> and <a href="#">link two</a>.</td>
      <td>data1b with a <img src="image_file.jpg" alt="image">.</td></tr>
    <tr>
      <td>data2a with a <input tyle="text" value="text value">.</td>
      <td>data2b with a <input tyle="text" value="second text value">.</td>
    </tr>
  </tbody>
  <tfoot>
    <tr><td colspan="2">This footer spans 2 cells</td></tr>
  </tfoot>
</table>

<table class="table2excel" data-tableName="Test Table 2">
  <thead>
    <tr class="noExl"><td>This shouldn't get exported</td><td>This shouldn't get exported either</td></tr>
    <tr><td>This Should get exported as a header</td><td>This should too</td></tr>
  </thead>
  <tbody>
    <tr><td>data1a</td><td>data1b</td></tr>
    <tr><td>data2a</td><td>data2b</td></tr>
  </tbody>
  <tfoot>
    <tr><td colspan="2">This footer spans 2 cells</td></tr>
  </tfoot>
</table>

<button id="download">Download</button>

或者使用table2excel(不是jQuery插件)

安装

npm i table2excel --save

使用

import 'table2excel';

const Table2Excel = window.Table2Excel;

const table2excel = new Table2Excel({
  exclude: ".noExl",
  name: "Excel Document Name",
  filename: "myExcelFile",
  exclude_img: true,
  exclude_links: true,
  exclude_inputs: true
});

table2excel.export(document.querySelector('.table2excel'));

【讨论】:

  • 当我使用以下行 import 'table2excel'; 我得到这个错误:Unexpected anonymous AMD define
  • 我无法发布 sn-p,因为这是导入 npm 文件的 SharePoint 框架问题。
  • 这个库是一个jquery插件,所以不能导入table2excel。而且,虽然不需要导入
  • 我提供了一个使用 NPM 库的辅助示例。 NPM 包不是 jQuery 插件。它是 table2excel 库的独立版本。
猜你喜欢
  • 2021-12-12
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-18
  • 2021-11-29
  • 1970-01-01
  • 2023-02-17
相关资源
最近更新 更多