-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathexample-excel-compatible-spreadsheet-dataview.html
More file actions
244 lines (215 loc) · 8.56 KB
/
Copy pathexample-excel-compatible-spreadsheet-dataview.html
File metadata and controls
244 lines (215 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="shortcut icon" type="image/ico" href="favicon.ico" />
<title>SlickGrid example: Spreadsheet with Excel compatible cut and paste</title>
<link rel="stylesheet" href="../dist/styles/css/example-demo.css" type="text/css"/>
<link rel="stylesheet" href="../dist/styles/css/slick-alpine-theme.css" type="text/css"/>
<link rel="stylesheet" href="../dist/styles/css/slick-icons.css" type="text/css"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<style>
.slick-cell.copied {
background: blue;
background: rgba(0, 0, 255, 0.2);
-webkit-transition: 0.5s background;
}
/** override slick-cell to make it look like Excel sheet */
.slick-container {
--alpine-header-column-height: 20px;
--alpine-header-font-weight: 500;
--alpine-cell-border-width: 0 1px 1px 0;
--alpine-cell-border-color: #d4d4d4;
}
</style>
</head>
<body>
<h2 class="title">Example - Excel Compatible Spreadsheet with DataView</h2>
<div style="position:relative">
<div style="width:600px;">
<div id="myGrid" class="slick-container" style="width:100%;height:300px;"></div>
</div>
<br/>
<div style="width:600px;">
<div id="myGrid2" class="slick-container" style="width:100%;height:300px;" class="example-grid"></div>
</div>
<div class="options-panel">
<h2>
<a href="/examples/index.html" style="text-decoration: none; font-size: 22px">⌂</a>
Excel compatible copy/paste manager using DataView
</h2>
<div>
<strong>Thanks to <a href="https://github.com/Celebio/SlickGrid">Celebio</a>! <a href="http://labs.nereo.com/slick.html">(link to site)</a></strong><br /><br />
This is basically the same example than <a href="example-spreadsheet.html">example-spreadsheet.html</a>,
with the support of external excel-compatible software clipboard<br />
</div>
<h2>Paste from Excel-compatible:</h2>
<ul>
<li>Copy a range of cells to clipboard in Excel</li>
<li>Select a cell on slickgrid</li>
<li>Use Ctrl-V keyboard shortcut to paste from the clipboard</li>
<li>Adds rows to bottom of grid if paste overflows</li>
</ul>
<div>
Each column to be pasted to must either have an editor, or there must be an overall getter/setter for the whole grid.<br/>
The issue is that to copy and paste a block from anywhere to anywhere, the data copied/pasted must be in text format. If data was copied direct, and if the columns of the copy/paste area were not aligned, we might end up copying say a date value into a column that has text data.<br/>
Enforcing the requirement for the editor is the way CellExternalCopyManager makes sure that pasted text data can be converter to and from the native data type of the column.
</div>
<h2>Copy for Excel-compatible:</h2>
<ul>
<li>Select a range of cells with a mouse</li>
<li>Use Ctrl-C shortcut to copy cells</li>
<li>You can paste the tabular data into Excel</li>
</ul>
<h2>Undo/redo support :</h2>
<ul>
<li>Use buttons to undo/redo copy/paste</li>
</ul>
<button onclick="undoRedoBuffer.undo()" style="display: inline-flex; gap: 4px"><span class="sgi sgi-undo"></span> Undo</button>
<button onclick="undoRedoBuffer.redo()" style="display: inline-flex; gap: 4px"><span class="sgi sgi-undo sgi-flip-h"></span> Redo</button>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example-excel-compatible-spreadsheet.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script src="../dist/browser/slick.core.js"></script>
<script src="../dist/browser/slick.interactions.js"></script>
<script src="../dist/browser/slick.grid.js"></script>
<script src="../dist/browser/plugins/slick.autotooltips.js"></script>
<script src="../dist/browser/plugins/slick.cellrangedecorator.js"></script>
<script src="../dist/browser/plugins/slick.cellrangeselector.js"></script>
<script src="../dist/browser/plugins/slick.cellexternalcopymanager.js"></script>
<script src="../dist/browser/plugins/slick.cellselectionmodel.js"></script>
<script src="../dist/browser/slick.editors.js"></script>
<script src="../dist/browser/slick.formatters.js"></script>
<script src="../dist/browser/slick.dataview.js"></script>
<script>
var grid;
var grid2;
var data = [];
var data2 = [];
function getVal(item, columnDef) {
//return dataView.getItemById(item.id)[columnDef.field];
return item[columnDef.field];
}
function setVal(item, columnDef, value) {
item[columnDef.field] = value;
// dataView.updateItem(item.id, item);
}
var options = {
editable: true,
enableAddRow: true,
enableCellNavigation: true,
asyncEditorLoading: false,
autoEdit: false,
dataItemColumnValueExtractor: getVal,
dataItemColumnValueSetter: setVal
};
var undoRedoBuffer = {
commandQueue : [],
commandCtr : 0,
queueAndExecuteCommand : function(editCommand) {
this.commandQueue[this.commandCtr] = editCommand;
this.commandCtr++;
editCommand.execute();
},
undo : function() {
if (this.commandCtr == 0) { return; }
this.commandCtr--;
var command = this.commandQueue[this.commandCtr];
if (command && Slick.GlobalEditorLock.cancelCurrentEdit()) {
command.undo();
}
},
redo : function() {
if (this.commandCtr >= this.commandQueue.length) { return; }
var command = this.commandQueue[this.commandCtr];
this.commandCtr++;
if (command && Slick.GlobalEditorLock.cancelCurrentEdit()) {
command.execute();
}
}
}
// undo shortcut
document.body.addEventListener('keydown', function(e) {
if (e.which == 90 && (e.ctrlKey || e.metaKey)) { // CTRL + (shift) + Z
if (e.shiftKey){
undoRedoBuffer.redo();
} else {
undoRedoBuffer.undo();
}
}
});
var newRowIds = 0;
var pluginOptions = {
clipboardCommandHandler: function(editCommand){ undoRedoBuffer.queueAndExecuteCommand.call(undoRedoBuffer,editCommand); },
readOnlyMode : false,
includeHeaderWhenCopying : false,
newRowCreator: function(count) {
for (var i = 0; i < count; i++) {
var item = {
id: "newRow_" + newRowIds++
}
grid.getData().addItem(item);
}
}
};
var columns = [
{
id: "selector",
name: "",
field: "num",
width: 30
}
];
for (var i = 0; i < 26; i++) {
columns.push({
id: i,
name: String.fromCharCode("A".charCodeAt(0) + i),
field: i,
width: 60//,
//editor: Slick.Editors.Text
});
}
columns[4] = {id: "%", name: "% Complete", field: "percentComplete", width: 95, resizable: false, formatter: Slick.Formatters.PercentCompleteBar, editor: Slick.Editors.PercentComplete};
columns[5] = {id: "start", name: "Start", field: "start", minWidth: 60, editor: Slick.Editors.Flatpickr};
document.addEventListener("DOMContentLoaded", function() {
for (var i = 0; i < 100; i++) {
var d = (data[i] = {});
d["id"] = i;
d["num"] = i;
for (var j = 0; j < 26; j++) {
d[j] = j+i;
}
d["percentComplete"] = Math.round(Math.random() * 100);
d["start"] = new Date(Math.round(Math.random() * 1000000000));
d["weekCalendar"] = [true, true, true, true, true, true, true, true, true, true, false, false, false, false];
}
dataView = new Slick.Data.DataView();
dataView.setItems(data);
grid = new Slick.Grid("#myGrid", dataView, columns, options);
grid.setSelectionModel(new Slick.CellSelectionModel());
grid.registerPlugin(new Slick.AutoTooltips());
// set keyboard focus on the grid
grid.getCanvasNode().focus();
grid.registerPlugin(new Slick.CellExternalCopyManager(pluginOptions));
grid.onCellChange.subscribe(function (e, args) {
dataView.updateItem(args.item.id, args.item);
});
grid.onAddNewRow.subscribe(function (e, args) {
var item = args.item;
var column = args.column;
grid.invalidateRow(data.length);
data.push(item);
grid.updateRowCount();
grid.render();
});
grid2 = new Slick.Grid("#myGrid2", data, columns, options);
grid2.setSelectionModel(new Slick.CellSelectionModel());
grid2.registerPlugin(new Slick.CellExternalCopyManager(pluginOptions));
})
</script>
</body>
</html>