-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathcolumn.ts
173 lines (159 loc) · 5.17 KB
/
column.ts
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
import { SheetModel } from './index';
import { ColumnModel } from './column-model';
import { ChildProperty, Property, Complex } from '@syncfusion/ej2-base';
import { FormatModel, Format, ValidationModel, isInMultipleRange } from '../common/index';
/**
* Configures the Column behavior for the spreadsheet.
*/
export class Column extends ChildProperty<Column> {
/**
* Specifies index of the column. Based on the index, column properties are applied.
*
* @default 0
* @asptype int
*/
@Property(0)
public index: number;
/**
* Specifies width of the column.
*
* @default 64
* @asptype int
*/
@Property(64)
public width: number;
/**
* specifies custom width of the column.
*
* @default false
*/
@Property(false)
public customWidth: boolean;
/**
* To hide/show the column in spreadsheet.
*
* @default false
*/
@Property(false)
public hidden: boolean;
/**
* Specifies format of the column.
*
* @default {}
*/
@Complex<FormatModel>({}, Format)
public format: FormatModel;
/**
* To lock/unlock the column in the protected sheet.
*
* @default true
*/
@Property(true)
public isLocked: boolean;
/**
* Specifies the validation of the column.
*
* @default ''
*/
@Property('')
public validation: ValidationModel;
/**
* Represents whether a column in the sheet is read-only or not. If set to true, it prevents editing the specified cell in the sheet.
*
* @default false
*/
@Property(false)
public isReadOnly: boolean;
}
/**
* @hidden
* @param {SheetModel} sheet - Specifies the sheet.
* @param {number} colIndex - Specifies the colIndex.
* @returns {ColumnModel} - To get Column.
*/
export function getColumn(sheet: SheetModel, colIndex: number): ColumnModel {
if (sheet.columns) {
if (!sheet.columns[colIndex as number]) {
sheet.columns[colIndex as number] = {};
}
} else {
sheet.columns = [];
sheet.columns[colIndex as number] = {};
}
return sheet.columns[colIndex as number];
}
/** @hidden
* @param {SheetModel} sheet - Specifies the sheet.
* @param {number} colIndex - Specifies the colIndex.
* @param {ColumnModel} column - Specifies the column.
* @returns {void} - To set Column.
*/
export function setColumn(sheet: SheetModel, colIndex: number, column: ColumnModel): void {
const curColumn: ColumnModel = getColumn(sheet, colIndex);
Object.keys(column).forEach((key: string): void => {
curColumn[`${key}`] = column[`${key}`];
});
}
/**
* @hidden
* @param {SheetModel} sheet - Specifies the sheet.
* @param {number} index - Specifies the index.
* @param {boolean} skipHidden - Specifies the bool.
* @param {boolean} checkDPR - Specifies the bool.
* @returns {number} - To get Column width.
*/
export function getColumnWidth(sheet: SheetModel, index: number, skipHidden?: boolean, checkDPR?: boolean): number {
let width: number;
if (sheet && sheet.columns && sheet.columns[index as number]) {
if (!skipHidden && sheet.columns[index as number].hidden) { return 0; }
width = (sheet.columns[index as number].width || sheet.columns[index as number].customWidth) ?
sheet.columns[index as number].width : 64;
} else {
width = 64;
}
if (checkDPR && window.devicePixelRatio % 1 > 0) {
const pointValue: number = (width * window.devicePixelRatio) % 1;
return width + (pointValue ? ((pointValue > 0.5 ? (1 - pointValue) : -1 * pointValue) / window.devicePixelRatio) : 0);
} else {
return width;
}
}
/**
* @hidden
* @param {SheetModel} sheet - Specifies the sheet.
* @param {number} startCol - Specifies the startCol.
* @param {number} endCol - Specifies the endCol.
* @param {boolean} checkDPR - Specifies the boolean value.
* @returns {number} - returns the column width.
*/
export function getColumnsWidth(sheet: SheetModel, startCol: number, endCol: number = startCol, checkDPR?: boolean): number {
let width: number = 0;
if (startCol > endCol) {
const swap: number = startCol;
startCol = endCol;
endCol = swap;
}
for (let i: number = startCol; i <= endCol; i++) {
width += getColumnWidth(sheet, i, null, checkDPR);
}
return width;
}
/**
* @hidden
* @param {SheetModel} sheet - Specifies the sheet.
* @param {number} index - Specifies the index.
* @returns {boolean} - returns the boolean value.
*/
export function isHiddenCol(sheet: SheetModel, index: number): boolean {
return sheet.columns[index as number] && sheet.columns[index as number].hidden;
}
/**
* @hidden
* @param {ColumnModel} column - Specifies the column.
* @param {number} rowIndex - Specifies the row index.
* @param {number} colIndex - Specifies the column index.
* @returns {boolean} - Specifies boolean values by checking column validation or not.
*/
export function checkColumnValidation(column: ColumnModel, rowIndex: number, colIndex: number): boolean {
return column && column.validation && (!column.validation.address || isInMultipleRange(column.validation.address, rowIndex, colIndex));
}