Skip to content
This repository has been archived by the owner on Jul 25, 2021. It is now read-only.

Commit

Permalink
Merge branch 'sheetname'
Browse files Browse the repository at this point in the history
  • Loading branch information
clarketm committed Dec 24, 2018
2 parents 6fc5b4d + 96ed1db commit 7f6415d
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 42 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ TableExport(document.getElementsByTagName("table"), {
ignoreRows: null, // (Number, Number[]), row indices to exclude from the exported file(s) (default: null)
ignoreCols: null, // (Number, Number[]), column indices to exclude from the exported file(s) (default: null)
trimWhitespace: true, // (Boolean), remove all leading/trailing newlines, spaces, and tabs from cell text in the exported file(s) (default: false)
RTL: false // (Boolean), set direction of the worksheet to right-to-left (default: false)
RTL: false, // (Boolean), set direction of the worksheet to right-to-left (default: false)
sheetname: "id" // (id, String), sheet name for the exported spreadsheet, (default: 'id')
});
```
> **Note:** to use the `xlsx` filetype, you must include [js-xlsx](https://github.com/SheetJS/js-xlsx/blob/master/dist/xlsx.core.min.js); reference the [`Add-Ons`](#add-ons) section.
Expand All @@ -137,6 +138,7 @@ TableExport(document.getElementsByTagName("table"), {
* [`ignoreCols`](https://tableexport.v3.travismclarke.com/examples/ignore-row-cols-cells.html)
* [`trimWhitespace`](https://tableexport.v3.travismclarke.com/examples/whitespace.html)
* [`RTL`](https://tableexport.v3.travismclarke.com/examples/right-to-left.html)
* [`sheetname`](https://tableexport.v3.travismclarke.com/examples/sheetname.html)

### Methods

Expand Down Expand Up @@ -385,6 +387,7 @@ When used alongside Bootstrap, there are four custom classes **`.xlsx`, `.xls`,
* [`ignoreCols`](https://tableexport.v3.travismclarke.com/examples/ignore-row-cols-cells.html)
* [`trimWhitespace`](https://tableexport.v3.travismclarke.com/examples/whitespace.html)
* [`RTL`](https://tableexport.v3.travismclarke.com/examples/right-to-left.html)
* [`sheetname`](https://tableexport.v3.travismclarke.com/examples/sheetname.html)

#### Customizing Settings
* [`ignoreCSS`](https://tableexport.v3.travismclarke.com/examples/ignore-row-cols-cells.html)
Expand Down
38 changes: 20 additions & 18 deletions dist/js/tableexport.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,8 @@
context.rows = settings.headers ? _nodesArray(el.querySelectorAll("thead > tr")).concat(context.rows) : context.rows;
context.rows = settings.footers ? context.rows.concat(_nodesArray(el.querySelectorAll("tfoot > tr"))) : context.rows;
context.thAdj = settings.headers ? el.querySelectorAll("thead > tr").length : 0;
context.filename =
settings.filename === "id"
? el.getAttribute("id")
? el.getAttribute("id")
: self.defaultFilename
: settings.filename
? settings.filename
: self.defaultFilename;
context.filename = settings.filename === "id" ? el.getAttribute("id") || self.defaultFilename : settings.filename || self.defaultFilename;
context.sheetname = settings.sheetname === "id" ? el.getAttribute("id") || self.defaultSheetname : settings.sheetname || self.defaultSheetname;
context.uuid = _uuid(el);

/**
Expand Down Expand Up @@ -164,7 +158,8 @@
ignoreRows: null, // (Number, Number[]), row indices to exclude from the exported file(s) (default: null)
ignoreCols: null, // (Number, Number[]), column indices to exclude from the exported file(s) (default: null)
trimWhitespace: true, // (Boolean), remove all leading/trailing newlines, spaces, and tabs from cell text in the exported file(s) (default: false)
RTL: false // (Boolean), set direction of the worksheet to right-to-left (default: false)
RTL: false, // (Boolean), set direction of the worksheet to right-to-left (default: false)
sheetname: "id" // (id, String), sheet name for the exported spreadsheet, (default: 'id')
},
/**
* Constants
Expand Down Expand Up @@ -197,6 +192,11 @@
* @memberof TableExport.prototype
*/
defaultFilename: "myDownload",
/**
* Sheetname fallback for exported files.
* @memberof TableExport.prototype
*/
defaultSheetname: "myWorksheet",
/**
* Class applied to each export button element.
* @memberof TableExport.prototype
Expand Down Expand Up @@ -398,7 +398,8 @@
mimeType: format.mimeType,
fileExtension: format.fileExtension,
merges: rcMap.merges,
RTL: settings.RTL
RTL: settings.RTL,
sheetname: settings.sheetname
});

var hashKey = _hashCode({ uuid: context.uuid, type: key });
Expand Down Expand Up @@ -542,8 +543,9 @@
mimeType = object.mimeType,
fileExtension = object.fileExtension,
merges = object.merges,
RTL = object.RTL;
this.export2file(data, mimeType, filename, fileExtension, merges, RTL);
RTL = object.RTL,
sheetname = object.sheetname;
this.export2file(data, mimeType, filename, fileExtension, merges, RTL, sheetname);
},
/**
* Excel Workbook constructor
Expand Down Expand Up @@ -577,9 +579,9 @@
* @param merges {Object[]}
* @param RTL {Boolean}
*/
export2file: function(data, mime, name, extension, merges, RTL) {
export2file: function(data, mime, name, extension, merges, RTL, sheetname) {
var format = extension.slice(1);
data = this.getRawData(data, extension, name, merges, RTL);
data = this.getRawData(data, extension, name, merges, RTL, sheetname);

if (_isMobile && (format === _FORMAT.CSV || format === _FORMAT.TXT)) {
var dataURI = "data:" + mime + ";" + this.charset + "," + data;
Expand All @@ -605,17 +607,17 @@
return key;
}
},
getRawData: function(data, extension, name, merges, RTL) {
getRawData: function(data, extension, name, merges, RTL, sheetname) {
var key = extension.substring(1);

if (_isEnhanced(key)) {
var wb = new this.Workbook(),
ws = this.createSheet(data, merges),
bookType = this.getBookType(key);

name = name || "";
wb.SheetNames.push(name);
wb.Sheets[name] = ws;
sheetname = sheetname || "";
wb.SheetNames.push(sheetname);
wb.Sheets[sheetname] = ws;
wb.Workbook.Views[0] = { RTL: RTL };
var wopts = {
bookType: bookType,
Expand Down
2 changes: 1 addition & 1 deletion dist/js/tableexport.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions dist/tableexport.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export declare class TableExport {
*/
defaultFilename: string;

/**
* Sheetname fallback for exported files.
*/
defaultSheetname: string;

/**
* Class applied to each export button element.
*/
Expand Down Expand Up @@ -190,6 +195,7 @@ interface Defaults {
ignoreCols?: number[];
trimWhitespace?: boolean;
RTL?: boolean;
sheetname?: string;
}

/**
Expand Down
11 changes: 8 additions & 3 deletions examples/defaults.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ <h1>Defaults</h1>
ignoreRows: null,
ignoreCols: null,
trimWhitespace: true,
RTL: false
RTL: false,
sheetname: 'id'
</pre>
</article>

Expand Down Expand Up @@ -94,7 +95,9 @@ <h1>Defaults</h1>
ignoreCols: null, // (Number, Number[]), column indices to exclude from the exported file(s) (default: null)
ignoreCSS: '.tableexport-ignore', // (selector, selector[]), selector(s) to exclude cells from the exported file(s) (default: '.tableexport-ignore')
emptyCSS: '.tableexport-empty', // (selector, selector[]), selector(s) to replace cells with an empty string in the exported file(s) (default: '.tableexport-empty')
trimWhitespace: true // (Boolean), remove all leading/trailing newlines, spaces, and tabs from cell text in the exported file(s) (default: true)
trimWhitespace: true, // (Boolean), remove all leading/trailing newlines, spaces, and tabs from cell text in the exported file(s) (default: true)
RTL: false, // (Boolean), set direction of the worksheet to right-to-left (default: false)
sheetname: 'id' // (id, String), sheet name for the exported spreadsheet, (default: 'id')
});
// **** jQuery **************************
// $(DefaultTable).tableExport({
Expand All @@ -108,7 +111,9 @@ <h1>Defaults</h1>
// ignoreCols: null,
// ignoreCSS: '.tableexport-ignore',
// emptyCSS: '.tableexport-empty',
// trimWhitespace: false
// trimWhitespace: false,
// RTL: false,
// sheetname: 'id'
// });
// **************************************

Expand Down
158 changes: 158 additions & 0 deletions examples/sheetname.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sheetname</title>
<link href="../dist/css/tableexport.min.css" rel="stylesheet">
<link href="./examples.css" rel="stylesheet">
</head>
<body>
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-TL44T9" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

<a id="example-link" href="./"> << Examples</a>

<iframe src="./defaults.html" frameborder="0" width="100%" scrolling="no" onload="this.height=screen.height/2.5;"></iframe>

<main>
<h1>Sheetname<span class="small">- string</span>
<code class="small">sheetname: 'super-worksheet'</code>
<span class="note"> ... OR any <code>string</code> besides <code>"id"</code></span>
</h1>
<table id="sheetname-table-1">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Age</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Thor Walton</td>
<td>Regional Director</td>
<td>45</td>
<td>$98,540</td>
</tr>
<tr>
<td>Travis Clarke</td>
<td>Software Engineer</td>
<td>30</td>
<td>$275,000</td>
</tr>
<tr>
<td>Suki Burks</td>
<td>Office Manager</td>
<td>22</td>
<td>$67,670</td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="disabled"></td>
<td class="disabled"></td>
<td class="disabled"></td>
<th>$441,210</th>
</tr>
</tfoot>
</table>

<p class="info">export the table to <code>.xlsx</code> and look at the sheetname of the downloaded file. Compared to the export from the <b>defaults</b> table above, you will notice that, <code>sheetname: 'super-worksheet'</code><i>creates a worksheet with the same name as the provided string </i>(<b>super-worksheet</b>)<i> instead of the table element's</i> <code>id</code>.</p>
</main>

<br>

<main>
<h1>Sheetname<span class="small">- falsey</span>
<code class="small">sheetname: false</code>
<span class="note"> ... OR any <code>falsey: false|null|undefined|""</code> value</span>
</h1>
<table id="sheetname-table-2">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Age</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Thor Walton</td>
<td>Regional Director</td>
<td>45</td>
<td>$98,540</td>
</tr>
<tr>
<td>Travis Clarke</td>
<td>Software Engineer</td>
<td>30</td>
<td>$275,000</td>
</tr>
<tr>
<td>Suki Burks</td>
<td>Office Manager</td>
<td>22</td>
<td>$67,670</td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="disabled"></td>
<td class="disabled"></td>
<td class="disabled"></td>
<th>$441,210</th>
</tr>
</tfoot>
</table>

<p class="info">
export the table to <code>.xlsx</code> and look at the sheetname of the downloaded file. Compared to the export from the <b>defaults</b> table above, you will notice that, <code>sheetname: false</code><i> uses a <b>fallback</b> value for the sheetname; the fallback (or default) can be configured with the </i>(<code>defaultSheetname</code> prototype property).
</p>

<p>
<b>NOTE:</b> <code>defaultSheetname</code> is also used for <code>sheetname: 'id'</code> if the export <i>table</i> element does <b>NOT</b> have an <code>id</code> attribute.
</p>

<br>

</main>

<script type="text/javascript" src="../bower_components/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="../bower_components/js-xlsx/dist/xlsx.core.min.js"></script>
<script type="text/javascript" src="../bower_components/blobjs/Blob.min.js"></script>
<script type="text/javascript" src="../bower_components/file-saverjs/FileSaver.min.js"></script>
<script type="text/javascript" src="../dist/js/tableexport.min.js"></script>
<script type="text/javascript" src="../assets/js/analytics.js"></script>
<script>

// Default sheetname if `sheetname: false|null|undefined|""`
TableExport.prototype.defaultSheetname = 'fallback-name';
// **** jQuery **************************
// $.fn.tableExport.defaultSheetname = 'fallback-name';
// **************************************

var SheetnameTable1 = document.getElementById('sheetname-table-1');
new TableExport(SheetnameTable1, {
sheetname: 'super-worksheet'
});
// **** jQuery **************************
// $(SheetnameTable1).tableExport({
// sheetname: 'super-worksheet'
// });
// **************************************

var SheetnameTable2 = document.getElementById('sheetname-table-2');
new TableExport(SheetnameTable2, {
sheetname: false // OR null OR undefined OR ""
});
// **** jQuery **************************
// $(SheetnameTable2).tableExport({
// sheetname: null
// });
// **************************************

</script>

</body>
</html>
Loading

0 comments on commit 7f6415d

Please sign in to comment.