-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelWorksheetView.cs
545 lines (517 loc) · 19.8 KB
/
ExcelWorksheetView.cs
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*******************************************************************************
* You may amend and distribute as you like, but don't remove this header!
*
* EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
* See https://github.com/JanKallman/EPPlus for details.
*
* Copyright (C) 2011 Jan Källman
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
*
* All code and executables are provided "as is" with no warranty either express or implied.
* The author accepts no liability for any damage or loss of business that this product may cause.
*
* Code change notes:
*
* Author Change Date
* ******************************************************************************
* Jan Källman Initial Release 2009-10-01
* Jan Källman License changed GPL-->LGPL 2011-12-27
*******************************************************************************/
using System;
using System.Xml;
namespace OfficeOpenXml
{
/// <summary>
/// Represents the different view states of the worksheet
/// </summary>
public class ExcelWorksheetView : XmlHelper
{
/// <summary>
/// The worksheet panes after a freeze or split.
/// </summary>
public class ExcelWorksheetPanes : XmlHelper
{
XmlElement _selectionNode = null;
internal ExcelWorksheetPanes(XmlNamespaceManager ns, XmlNode topNode) :
base(ns, topNode)
{
if(topNode.Name=="selection")
{
_selectionNode=topNode as XmlElement;
}
}
const string _activeCellPath = "@activeCell";
/// <summary>
/// Set the active cell. Must be set within the SelectedRange.
/// </summary>
public string ActiveCell
{
get
{
string address = GetXmlNodeString(_activeCellPath);
if (address == "")
{
return "A1";
}
return address;
}
set
{
int fromCol, fromRow, toCol, toRow;
if(_selectionNode==null) CreateSelectionElement();
ExcelCellBase.GetRowColFromAddress(value, out fromRow, out fromCol, out toRow, out toCol);
SetXmlNodeString(_activeCellPath, value);
if (((XmlElement)TopNode).GetAttribute("sqref") == "")
{
SelectedRange = ExcelCellBase.GetAddress(fromRow, fromCol);
}
else
{
//TODO:Add fix for out of range here
}
}
}
private void CreateSelectionElement()
{
_selectionNode=TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
TopNode.AppendChild(_selectionNode);
TopNode=_selectionNode;
}
const string _selectionRangePath = "@sqref";
/// <summary>
/// Selected Cells.Used in combination with ActiveCell
/// </summary>
public string SelectedRange
{
get
{
string address = GetXmlNodeString(_selectionRangePath);
if (address == "")
{
return "A1";
}
return address;
}
set
{
int fromCol, fromRow, toCol, toRow;
if(_selectionNode==null) CreateSelectionElement();
ExcelCellBase.GetRowColFromAddress(value, out fromRow, out fromCol, out toRow, out toCol);
SetXmlNodeString(_selectionRangePath, value);
if (((XmlElement)TopNode).GetAttribute("activeCell") == "")
{
ActiveCell = ExcelCellBase.GetAddress(fromRow, fromCol);
}
else
{
//TODO:Add fix for out of range here
}
}
}
}
private ExcelWorksheet _worksheet;
#region ExcelWorksheetView Constructor
/// <summary>
/// Creates a new ExcelWorksheetView which provides access to all the view states of the worksheet.
/// </summary>
/// <param name="ns"></param>
/// <param name="node"></param>
/// <param name="xlWorksheet"></param>
internal ExcelWorksheetView(XmlNamespaceManager ns, XmlNode node, ExcelWorksheet xlWorksheet) :
base(ns, node)
{
_worksheet = xlWorksheet;
SchemaNodeOrder = new string[] { "sheetViews", "sheetView", "pane", "selection" };
Panes = LoadPanes();
}
#endregion
private ExcelWorksheetPanes[] LoadPanes()
{
XmlNodeList nodes = TopNode.SelectNodes("//d:selection", NameSpaceManager);
if(nodes.Count==0)
{
return new ExcelWorksheetPanes[] { new ExcelWorksheetPanes(NameSpaceManager, TopNode) };
}
else
{
ExcelWorksheetPanes[] panes = new ExcelWorksheetPanes[nodes.Count];
int i=0;
foreach(XmlElement elem in nodes)
{
panes[i++] = new ExcelWorksheetPanes(NameSpaceManager, elem);
}
return panes;
}
}
#region SheetViewElement
/// <summary>
/// Returns a reference to the sheetView element
/// </summary>
protected internal XmlElement SheetViewElement
{
get
{
return (XmlElement)TopNode;
}
}
#endregion
#region TabSelected
private XmlElement _selectionNode = null;
private XmlElement SelectionNode
{
get
{
_selectionNode = SheetViewElement.SelectSingleNode("//d:selection", _worksheet.NameSpaceManager) as XmlElement;
if (_selectionNode == null)
{
_selectionNode = _worksheet.WorksheetXml.CreateElement("selection", ExcelPackage.schemaMain);
SheetViewElement.AppendChild(_selectionNode);
}
return _selectionNode;
}
}
#endregion
#region Public Methods & Properties
/// <summary>
/// The active cell. Single Cell address.
/// This cell must be inside the selected range. If not, the selected range is set to the active cell address
/// </summary>
public string ActiveCell
{
get
{
return Panes[Panes.GetUpperBound(0)].ActiveCell;
}
set
{
var ac = new ExcelAddressBase(value);
if (ac.IsSingleCell==false)
{
throw (new InvalidOperationException("ActiveCell must be a single cell."));
}
/*** Active cell must be inside SelectedRange ***/
var sd = new ExcelAddressBase(SelectedRange.Replace(" ",","));
Panes[Panes.GetUpperBound(0)].ActiveCell = value;
if (IsActiveCellInSelection(ac, sd)==false)
{
SelectedRange = value;
}
}
}
/// <summary>
/// Selected Cells in the worksheet. Used in combination with ActiveCell.
/// If the active cell is not inside the selected range, the active cell will be set to the first cell in the selected range.
/// If the selected range has multiple adresses, these are separated with space. If the active cell is not within the first address in this list, the attribute ActiveCellId must be set (not supported, so it must be set via the XML).
/// </summary>
public string SelectedRange
{
get
{
return Panes[Panes.GetUpperBound(0)].SelectedRange;
}
set
{
var ac = new ExcelAddressBase(ActiveCell);
/*** Active cell must be inside SelectedRange ***/
var sd = new ExcelAddressBase(value.Replace(" ",",")); //Space delimitered here, replace
Panes[Panes.GetUpperBound(0)].SelectedRange = value;
if (IsActiveCellInSelection(ac, sd)==false)
{
ActiveCell = new ExcelCellAddress(sd._fromRow, sd._fromCol).Address;
}
}
}
private bool IsActiveCellInSelection(ExcelAddressBase ac, ExcelAddressBase sd)
{
var c = sd.Collide(ac);
if (c == ExcelAddressBase.eAddressCollition.Equal || c == ExcelAddressBase.eAddressCollition.Inside)
{
return true;
}
else
{
if (sd.Addresses != null)
{
foreach (var sds in sd.Addresses)
{
c = sds.Collide(ac);
if (c == ExcelAddressBase.eAddressCollition.Equal || c == ExcelAddressBase.eAddressCollition.Inside)
{
return true;
}
}
}
}
return false;
}
/// <summary>
/// Indicates if the worksheet is selected within the workbook. NOTE: Setter clears other selected tabs.
/// </summary>
public bool TabSelected
{
get
{
return GetXmlNodeBool("@tabSelected");
}
set
{
SetTabSelected(value, false);
}
}
/// <summary>
/// Indicates if the worksheet is selected within the workbook. NOTE: Setter keeps other selected tabs.
/// </summary>
public bool TabSelectedMulti
{
get
{
return GetXmlNodeBool("@tabSelected");
}
set
{
SetTabSelected(value, true);
}
}
/// <summary>
/// Sets whether the worksheet is selected within the workbook.
/// </summary>
/// <param name="isSelected">Whether the tab is selected, defaults to true.</param>
/// <param name="allowMultiple">Whether to allow multiple active tabs, defaults to false.</param>
public void SetTabSelected(bool isSelected = true, bool allowMultiple = false)
{
if (isSelected)
{
SheetViewElement.SetAttribute("tabSelected", "1");
if (!allowMultiple)
{
// // ensure no other worksheet has its tabSelected attribute set to 1
foreach (ExcelWorksheet sheet in _worksheet._package.Workbook.Worksheets)
sheet.View.TabSelected = false;
}
XmlElement bookView = _worksheet.Workbook.WorkbookXml.SelectSingleNode("//d:workbookView", _worksheet.NameSpaceManager) as XmlElement;
if (bookView != null)
{
bookView.SetAttribute("activeTab", (_worksheet.PositionID - 1).ToString());
}
}
else
SetXmlNodeString("@tabSelected", "0");
}
/// <summary>
/// Sets the view mode of the worksheet to pagelayout
/// </summary>
public bool PageLayoutView
{
get
{
return GetXmlNodeString("@view") == "pageLayout";
}
set
{
if (value)
SetXmlNodeString("@view", "pageLayout");
else
SheetViewElement.RemoveAttribute("view");
}
}
/// <summary>
/// Sets the view mode of the worksheet to pagebreak
/// </summary>
public bool PageBreakView
{
get
{
return GetXmlNodeString("@view") == "pageBreakPreview";
}
set
{
if (value)
SetXmlNodeString("@view", "pageBreakPreview");
else
SheetViewElement.RemoveAttribute("view");
}
}
/// <summary>
/// Show gridlines in the worksheet
/// </summary>
public bool ShowGridLines
{
get
{
return GetXmlNodeBool("@showGridLines", true);
}
set
{
SetXmlNodeString("@showGridLines", value ? "1" : "0");
}
}
/// <summary>
/// Show the Column/Row headers (containg column letters and row numbers)
/// </summary>
public bool ShowHeaders
{
get
{
return GetXmlNodeBool("@showRowColHeaders");
}
set
{
SetXmlNodeString("@showRowColHeaders", value ? "1" : "0");
}
}
/// <summary>
/// Window zoom magnification for current view representing percent values.
/// </summary>
public int ZoomScale
{
get
{
return GetXmlNodeInt("@zoomScale");
}
set
{
if (value < 10 || value > 400)
{
throw new ArgumentOutOfRangeException("Zoome scale out of range (10-400)");
}
SetXmlNodeString("@zoomScale", value.ToString());
}
}
/// <summary>
/// Flag indicating whether the sheet is in 'right to left' display mode. When in this mode,Column A is on the far right, Column B ;is one column left of Column A, and so on. Also,information in cells is displayed in the Right to Left format.
/// </summary>
public bool RightToLeft
{
get
{
return GetXmlNodeBool("@rightToLeft");
}
set
{
SetXmlNodeString("@rightToLeft", value == true ? "1" : "0");
}
}
internal bool WindowProtection
{
get
{
return GetXmlNodeBool("@windowProtection",false);
}
set
{
SetXmlNodeBool("@windowProtection",value,false);
}
}
/// <summary>
/// Reference to the panes
/// </summary>
public ExcelWorksheetPanes[] Panes
{
get;
internal set;
}
string _paneNodePath = "d:pane";
string _selectionNodePath = "d:selection";
/// <summary>
/// Freeze the columns/rows to left and above the cell
/// </summary>
/// <param name="Row"></param>
/// <param name="Column"></param>
public void FreezePanes(int Row, int Column)
{
//TODO:fix this method to handle splits as well.
if (Row == 1 && Column == 1) UnFreezePanes();
string sqRef = SelectedRange, activeCell = ActiveCell;
XmlElement paneNode = TopNode.SelectSingleNode(_paneNodePath, NameSpaceManager) as XmlElement;
if (paneNode == null)
{
CreateNode(_paneNodePath);
paneNode = TopNode.SelectSingleNode(_paneNodePath, NameSpaceManager) as XmlElement;
}
paneNode.RemoveAll(); //Clear all attributes
if (Column > 1) paneNode.SetAttribute("xSplit", (Column - 1).ToString());
if (Row > 1) paneNode.SetAttribute("ySplit", (Row - 1).ToString());
paneNode.SetAttribute("topLeftCell", ExcelCellBase.GetAddress(Row, Column));
paneNode.SetAttribute("state", "frozen");
RemoveSelection();
if (Row > 1 && Column==1)
{
paneNode.SetAttribute("activePane", "bottomLeft");
XmlElement sel=TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
sel.SetAttribute("pane", "bottomLeft");
if (activeCell != "") sel.SetAttribute("activeCell", activeCell);
if (sqRef != "") sel.SetAttribute("sqref", sqRef);
sel.SetAttribute("sqref", sqRef);
TopNode.InsertAfter(sel, paneNode);
}
else if (Column > 1 && Row == 1)
{
paneNode.SetAttribute("activePane", "topRight");
XmlElement sel = TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
sel.SetAttribute("pane", "topRight");
if (activeCell != "") sel.SetAttribute("activeCell", activeCell);
if (sqRef != "") sel.SetAttribute("sqref", sqRef);
TopNode.InsertAfter(sel, paneNode);
}
else
{
paneNode.SetAttribute("activePane", "bottomRight");
XmlElement sel1 = TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
sel1.SetAttribute("pane", "topRight");
string cell = ExcelCellBase.GetAddress(1, Column);
sel1.SetAttribute("activeCell", cell);
sel1.SetAttribute("sqref", cell);
paneNode.ParentNode.InsertAfter(sel1, paneNode);
XmlElement sel2 = TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
cell = ExcelCellBase.GetAddress(Row, 1);
sel2.SetAttribute("pane", "bottomLeft");
sel2.SetAttribute("activeCell", cell);
sel2.SetAttribute("sqref", cell);
sel1.ParentNode.InsertAfter(sel2, sel1);
XmlElement sel3 = TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
sel3.SetAttribute("pane", "bottomRight");
if(activeCell!="") sel3.SetAttribute("activeCell", activeCell);
if(sqRef!="") sel3.SetAttribute("sqref", sqRef);
sel2.ParentNode.InsertAfter(sel3, sel2);
}
Panes=LoadPanes();
}
private void RemoveSelection()
{
//Find selection nodes and remove them
XmlNodeList selections = TopNode.SelectNodes(_selectionNodePath, NameSpaceManager);
foreach (XmlNode sel in selections)
{
sel.ParentNode.RemoveChild(sel);
}
}
/// <summary>
/// Unlock all rows and columns to scroll freely
/// /// </summary>
public void UnFreezePanes()
{
string sqRef = SelectedRange, activeCell = ActiveCell;
XmlElement paneNode = TopNode.SelectSingleNode(_paneNodePath, NameSpaceManager) as XmlElement;
if (paneNode != null)
{
paneNode.ParentNode.RemoveChild(paneNode);
}
RemoveSelection();
Panes=LoadPanes();
SelectedRange = sqRef;
ActiveCell = activeCell;
}
#endregion
}
}