-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetFormulas.ts
More file actions
466 lines (353 loc) · 18.3 KB
/
setFormulas.ts
File metadata and controls
466 lines (353 loc) · 18.3 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
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
function main(workbook: ExcelScript.Workbook) {
const sheetNames = ["PHONE 6.100 AC-PC", "PHONE 6.200 AC-PC", "PHONE 7.200 ICU"];
for (const name of sheetNames) {
const ps = workbook.getWorksheet(name);
if (!ps) continue;
const config = getSheetConfig(name);
const protection = ps.getProtection();
if (name === "PHONE 7.200 ICU") {
protection.pauseProtection("72icu");
} else {
protection.pauseProtection("imsorrydave");
}
// ✅ Temporarily pause protection (will retain password)
protection.pauseProtection();
// ✅ Correct logic for ICU vs AC-PC processing
if (ps.getName() === "PHONE 7.200 ICU" || ps.getName() === "PHONE 6.200 ICU") {
applyFormulasICU(ps, config.dataRowStart, config.dataRowEnd);
applyBorders(ps, `A8:M${config.dataRowEnd}`);
condFormattingICU(ps, config.dataRowStart, config.dataRowEnd);
// dataValidationICU(ps, config.dataRowStart, config.dataRowEnd);
// clearAndSetAllowEdit(ps, config.editableRanges);
} else {
applyFormulas(ps, config.dataRowStart, config.dataRowEnd);
nameAlerts(ps, config.dataRowStart, config.dataRowEnd);
applyBorders(ps, `A6:I${config.dataRowEnd}`);
condFormatting(ps, config.dataRowStart, config.dataRowEnd);
dataValidation(ps, config.dataRowStart, config.dataRowEnd);
clearAndSetAllowEdit(ps, config.editableRanges);
try {
const fontRange = ps.getRange(`A${config.dataRowStart}:I${config.dataRowEnd}`);
const font = fontRange.getFormat().getFont();
font.setName("Times New Roman");
font.setBold(true);
font.setSize(14);
fontRange.getFormat().setShrinkToFit(true);
} catch (err) {
console.log("Error setting font: " + err.message);
}
nameAlerts(ps, config.dataRowStart, config.dataRowEnd);
makeAllRowsUppercase(ps, config.dataRowStart, config.dataRowEnd);
}
// ✅ Resume protection (will restore original password & settings)
protection.resumeProtection();
}
}
function applyFormulas(ps: ExcelScript.Worksheet, startRow: number, endRow: number) {
const eRange = `E${startRow}:E${endRow}`;
const gRange = `G${startRow}:G${endRow}`;
const dRange = `D${startRow}:D${endRow}`;
const bRange = `B${startRow}:B${endRow}`;
const cRange = `C${startRow}:C${endRow}`;
const rnFormula = `=SUM(IF(FREQUENCY(IF(${eRange}<>"", MATCH(${eRange}, ${eRange}, 0)), IF(${eRange}<>"", MATCH(${eRange}, ${eRange}, 0))) > 0, 1)) + IF(COUNTIF(${eRange}, C4)=0, 1, 0) + SUM(IF(${gRange} = "RESOURCE",1,0))`;
const nctFormula = `=SUM(IF(FREQUENCY(IF(${gRange}<>"", MATCH(${gRange}, ${gRange}, 0)), IF(${gRange}<>"", MATCH(${gRange}, ${gRange}, 0))) > 0, 1)) + SUM(IF(${gRange} = "RESOURCE",-1,0))`;
const acFormula = `=COUNTIFS(${dRange},"*AC*",${bRange},"<>*(ADMIT)*")`;
const pcFormula = `=COUNTIFS(${dRange},"*PC*",${bRange},"<>*(ADMIT)*")`;
const otherFormulas = {
chargeFormula: '=IF(C4<>"",XLOOKUP(C4,\'RN-NCT LIST\'!F:F,\'RN-NCT LIST\'!G:G), "")',
sgeFormula: `=COUNTIFS(${cRange},"*SGE*",${bRange},"<>*(ADMIT)*")`,
sgtFormula: `=COUNTIFS(${cRange},"*SGT*",${bRange},"<>*(ADMIT)*")`,
scrFormula: `=COUNTIFS(${cRange},"*SCR*",${bRange},"<>*(ADMIT)*")`,
sgoFormula: `=COUNTIFS(${cRange},"*SGO*",${bRange},"<>*(ADMIT)*")`,
covidFormula: `=COUNTIFS(${bRange},"*(+)*",${bRange},"<>*(ADMIT)*")`,
sitterFormula: `=SUM(COUNTIFS(${bRange},{"*(SIT)*","*(TS)*","*(72)*"},${bRange},"<>*(ADMIT)*"))`
};
// --- Autofill F column ---
const fStartCell = `F${startRow}`;
const fStart = ps.getRange(fStartCell);
fStart.setFormula(`=IF(E${startRow}<>"",XLOOKUP(E${startRow},'RN-NCT LIST'!F:F,'RN-NCT LIST'!G:G),"")`);
if (endRow > startRow) {
const fFillRange = ps.getRange(`F${startRow}:F${endRow}`);
fStart.autoFill(fFillRange, ExcelScript.AutoFillType.fillDefault);
}
// --- Autofill H column ---
const hStartCell = `H${startRow}`;
const hStart = ps.getRange(hStartCell);
hStart.setFormula(`=IF(G${startRow}<>"",XLOOKUP(G${startRow},'RN-NCT LIST'!F:F,'RN-NCT LIST'!G:G),"")`);
if (endRow > startRow) {
const hFillRange = ps.getRange(`H${startRow}:H${endRow}`);
hStart.autoFill(hFillRange, ExcelScript.AutoFillType.fillDefault);
}
// --- Summary formulas ---
ps.getRange("O7").setFormula(rnFormula);
ps.getRange("O8").setFormula(nctFormula);
ps.getRange("O11").setFormula(acFormula);
ps.getRange("O12").setFormula(pcFormula);
ps.getRange("O13").setFormula(`=COUNTIFS(${bRange}, "<>*ADMIT*", ${bRange}, "<>")`);
ps.getRange("F4").setFormulaLocal(otherFormulas.chargeFormula);
ps.getRange("O16").setFormulaLocal(otherFormulas.sgeFormula);
ps.getRange("O17").setFormulaLocal(otherFormulas.sgtFormula);
ps.getRange("O18").setFormulaLocal(otherFormulas.scrFormula);
ps.getRange("O19").setFormulaLocal(otherFormulas.sgoFormula);
ps.getRange("O21").setFormula(`=COUNTIFS(${bRange}, "<>*ADMIT*", ${bRange}, "<>")`);
ps.getRange("O24").setFormulaLocal(otherFormulas.covidFormula);
ps.getRange("O27").setFormulaLocal(otherFormulas.sitterFormula);
}
function applyFormulasICU(ps: ExcelScript.Worksheet, startRow: number, endRow: number) {
const eRange = `G${startRow}:G${endRow}`;
const gRange = `G${startRow}:G${endRow}`;
const dRange = `D${startRow}:D${endRow}`;
//const bRange = `B${startRow}:B${endRow}`;
const cRange = `C${startRow}:C${endRow}`;
const rnFormula = `=SUM(IF(FREQUENCY(IF(${eRange}<>"", MATCH(${eRange}, ${eRange}, 0)), IF(${eRange}<>"", MATCH(${eRange}, ${eRange}, 0))) > 0, 1)) + IF(COUNTIF(${eRange}, C4)=0, 1, 0) + SUM(IF(${gRange} = "RESOURCE",1,0))`;
//const nctFormula = `=SUM(IF(FREQUENCY(IF(${gRange}<>"", MATCH(${gRange}, ${gRange}, 0)), IF(${gRange}<>"", MATCH(${gRange}, ${gRange}, 0))) > 0, 1)) + SUM(IF(${gRange} = "RESOURCE",-1,0))`;
const otherFormulas = {
chargeFormula: '=IF(C4<>"",XLOOKUP(C4,\'RN-NCT LIST\'!E:E,\'RN-NCT LIST\'!F:F), "")',
};
// --- Autofill H column ---
const hStartCell = `I${startRow}`;
const hStart = ps.getRange(hStartCell);
hStart.setFormula(`=IF(G${startRow}<>"",XLOOKUP(G${startRow},'RN-NCT LIST'!E:E,'RN-NCT LIST'!F:F),"")`);
if (endRow > startRow) {
const hFillRange = ps.getRange(`I${startRow}:I${endRow}`);
hStart.autoFill(hFillRange, ExcelScript.AutoFillType.fillDefault);
}
// --- Summary formulas ---
ps.getRange("O7").setFormula(rnFormula);
ps.getRange("F4").setFormulaLocal(otherFormulas.chargeFormula);
// ps.getRange("O21").setFormula(`=COUNTIFS(${bRange}, "<>*ADMIT*", ${bRange}, "<>")`);
const cells = ["I10", "I12", "I14", "I16", "I18", "I20"];
for (let cell of cells) {
ps.getRange(cell).getFormat().getFill().setColor("FFFFFF");
}
}
function makeAllRowsUppercase(sheet: ExcelScript.Worksheet, startRow: number, endRow: number) {
const range = sheet.getRangeByIndexes(startRow - 1, 0, endRow - startRow + 1, sheet.getUsedRange().getColumnCount());
const values = range.getValues();
const uppercased = values.map(row =>
row.map(cell => typeof cell === "string" ? cell.toUpperCase() : cell)
);
range.setValues(uppercased);
}
function applyBorders(sheet: ExcelScript.Worksheet, rangeAddress: string) {
let borders = [
ExcelScript.BorderIndex.edgeTop,
ExcelScript.BorderIndex.edgeRight,
ExcelScript.BorderIndex.edgeLeft,
ExcelScript.BorderIndex.edgeBottom,
ExcelScript.BorderIndex.insideHorizontal,
ExcelScript.BorderIndex.insideVertical
];
let weights = [ExcelScript.BorderWeight.thick, ExcelScript.BorderWeight.thick, ExcelScript.BorderWeight.thick,
ExcelScript.BorderWeight.thick, ExcelScript.BorderWeight.thin, ExcelScript.BorderWeight.thin];
borders.forEach((edge, index) => {
const border = sheet.getRange(rangeAddress).getFormat().getRangeBorder(edge);
border.setStyle(ExcelScript.BorderLineStyle.continuous);
border.setWeight(weights[index]);
border.setColor("000000");
});
}
function condFormatting(sheet: ExcelScript.Worksheet, startRow: number, endRow: number) {
const patientName = sheet.getRange(`B${startRow}:B${endRow}`);
try {
const formats = patientName.getConditionalFormats();
for (let i = formats.length - 1; i >= 0; i--) {
formats[i].delete();
}
} catch (error) {
console.log("Error clearing formats: " + error.message);
}
applyConditionalFormatting(patientName, '(S)', 'red', '00b0f0');
applyConditionalFormatting(patientName, '(P)', 'red', '00b0f0');
const formulaRule = patientName.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
formulaRule.getCustom().getRule().setFormula(`=AND(M${startRow}>1, M${endRow}<5)`);
let format = formulaRule.getCustom().getFormat();
format.getFill().setColor("33ccff");
format.getFont().setBold(true);
}
function condFormattingICU(sheet: ExcelScript.Worksheet, startRow: number, endRow: number) {
const patientName = sheet.getRange(`D${startRow}:D${endRow}`);
try {
const formats = patientName.getConditionalFormats();
for (let i = formats.length - 1; i >= 0; i--) {
formats[i].delete();
}
} catch (error) {
console.log("Error clearing formats: " + error.message);
}
applyConditionalFormatting(patientName, '(S)', 'red', '00b0f0');
applyConditionalFormatting(patientName, '(P)', 'red', '00b0f0');
const formulaRule = patientName.addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
formulaRule.getCustom().getRule().setFormula(`=AND(N${startRow}>1, N${endRow}<5)`);
let format = formulaRule.getCustom().getFormat();
format.getFill().setColor("33ccff");
format.getFont().setBold(true);
}
// Helper function for conditional formatting
function applyConditionalFormatting(range: ExcelScript.Range, text: string, color1: string, color2: string) {
const textComparison = range.addConditionalFormat(ExcelScript.ConditionalFormatType.containsText).getTextComparison();
textComparison.setRule({ text, operator: ExcelScript.ConditionalTextOperator.contains });
textComparison.getFormat().getFill().setColor(color1);
textComparison.getFormat().getFont().setBold(true);
}
function dataValidation(sheet: ExcelScript.Worksheet, startRow: number, endRow: number) {
const rnRange = sheet.getRange(`E${startRow}:E${endRow}`);
const nctRange = sheet.getRange(`G${startRow}:G${endRow}`);
const locRange = sheet.getRange(`D${startRow}:D${endRow}`);
const teamRange = sheet.getRange(`C${startRow}:C${endRow}`);
const shiftRange = sheet.getRange("C5");
[rnRange, nctRange, locRange, teamRange, shiftRange].forEach(r => r.getDataValidation()?.clear());
const listRule = (src: string): ExcelScript.DataValidationRule => ({ list: { source: src, inCellDropDown: true } });
const errorAlert = (msg: string): ExcelScript.DataValidationErrorAlert => ({
message: msg, showAlert: false, style: ExcelScript.DataValidationAlertStyle.warning, title: msg
});
rnRange.getDataValidation()?.setRule(listRule("='RN-NCT LIST'!$F:$F"));
rnRange.getDataValidation()?.setIgnoreBlanks(true);
rnRange.getDataValidation()?.setErrorAlert(errorAlert("rn not found"));
nctRange.getDataValidation()?.setRule(listRule("='RN-NCT LIST'!$F:$F"));
nctRange.getDataValidation()?.setIgnoreBlanks(true);
nctRange.getDataValidation()?.setErrorAlert(errorAlert("nct not found"));
locRange.getDataValidation()?.setRule(listRule("AC,PC"));
locRange.getDataValidation()?.setIgnoreBlanks(true);
locRange.getDataValidation()?.setErrorAlert(errorAlert("loc doesn't exist"));
teamRange.getDataValidation()?.setRule(listRule("=$B$78:$B$128"));
teamRange.getDataValidation()?.setIgnoreBlanks(true);
teamRange.getDataValidation()?.setErrorAlert(errorAlert("wrong team"));
shiftRange.getDataValidation()?.setRule(listRule("7 AM,7 PM"));
shiftRange.getDataValidation()?.setErrorAlert(errorAlert("not valid shift"));
}
function dataValidationICU(sheet: ExcelScript.Worksheet, startRow: number, endRow: number) {
const rnRange = sheet.getRange(`G${startRow}:G${endRow}`);
const teamRange = sheet.getRange(`C${startRow}:C${endRow}`);
const shiftRange = sheet.getRange("D3");
[rnRange, teamRange, shiftRange].forEach(r => r.getDataValidation()?.clear());
const listRule = (src: string): ExcelScript.DataValidationRule => ({ list: { source: src, inCellDropDown: true } });
const errorAlert = (msg: string): ExcelScript.DataValidationErrorAlert => ({
message: msg, showAlert: false, style: ExcelScript.DataValidationAlertStyle.warning, title: msg
});
rnRange.getDataValidation()?.setRule(listRule("='RN-NCT LIST'!$F:$F"));
rnRange.getDataValidation()?.setIgnoreBlanks(true);
rnRange.getDataValidation()?.setErrorAlert(errorAlert("rn not found"));
teamRange.getDataValidation()?.setRule(listRule("=$B$78:$B$128"));
teamRange.getDataValidation()?.setIgnoreBlanks(true);
teamRange.getDataValidation()?.setErrorAlert(errorAlert("wrong team"));
shiftRange.getDataValidation()?.setRule(listRule("7 AM,7 PM"));
shiftRange.getDataValidation()?.setErrorAlert(errorAlert("not valid shift"));
}
function allowEdit(sheet: ExcelScript.Worksheet) {
const config = getSheetConfig(sheet.getName());
const protection = sheet.getProtection();
if(sheet.getName() === "PHONE 7.200 ICU"){
if (protection.getProtected()) protection.pauseProtectionForAllAllowEditRanges("72icu");
}
else {
if (protection.getProtected()) protection.pauseProtectionForAllAllowEditRanges("imsorrydave");
}
// Lock all cells first (default)
sheet.getUsedRange().getFormat().getProtection().setLocked(true);
// Unlock only editable ranges from config
for (const rangeStr of config.editableRanges) {
try {
const range = sheet.getRange(rangeStr);
range.getFormat().getProtection().setLocked(false);
} catch (err) {
console.log(`Could not unlock range ${rangeStr}: ${err.message}`);
}
}
// Re-apply protection after locking/unlocking
protection.protect();
console.log("Protection reapplied with config-defined editable ranges.");
}
/**
* Clears all existing allowToEdit settings and then sets new allowToEdit ranges.
*
* @param workbook The workbook object in the Excel script.
* @param editableRanges An array of string ranges that should be editable, e.g., ["A1:C10", "D1:D10"]
*/
function clearAndSetAllowEdit(sheet: ExcelScript.Worksheet, editableRanges: string[]) {
const protection = sheet.getProtection();
// Fully unprotect the sheet
// Add new editable ranges
for (let i = 0; i < editableRanges.length; i++) {
try {
protection.addAllowEditRange(`EditRange${i + 1}`, editableRanges[i], { password: "imsorrydave" });
} catch (err) {
console.log(`Error adding allow edit range '${editableRanges[i]}': ${err.message}`);
}
}
}
function nameAlerts(sheet: ExcelScript.Worksheet, startRow: number, endRow: number) {
const range = sheet.getRange(`B${startRow}:B${endRow}`);
const data = range.getValues();
const numRows = data.length;
const columnMValues: (number | string)[][] = [];
const excluded = ["(S)", "(P)", "(SIT)", "(ADMIT)"];
const redFill = ["(S)", "(P)"];
const blueFill = ["(SIT)", "(ADMIT)"];
const redRGB = "#FF0000", blueRGB = "#0000FF";
function clean(name: string): string {
name = name.split(",")[0];
excluded.forEach(text => {
name = name.replace(text, "");
});
return name.trim().toLowerCase().replace(/[^a-z]/g, "");
}
function normalize(name: string): string {
return name.replace(/(.)\1+/g, "$1");
}
const names = data.map(row => row[0] ? clean(row[0] as string) : "");
const normNames = names.map(name => normalize(name));
const counts: { [key: string]: number } = {};
normNames.forEach(name => { if (name) counts[name] = (counts[name] || 0) + 1; });
for (let i = 0; i < numRows; i++) {
const fullName = data[i][0] as string;
const cell = sheet.getRange(`B${startRow + i}`);
const fill = cell.getFormat().getFill().getColor();
if (!fullName) {
columnMValues.push([0]);
continue;
}
const cleaned = clean(fullName);
const normalized = normalize(cleaned);
const words = cleaned.split(" ");
const hasDupWords = new Set(words).size !== words.length;
const isMultiple = counts[normalized] > 1;
columnMValues.push([isMultiple && !hasDupWords ? 2 : 1]);
if (fill === redRGB && !redFill.some(t => fullName.includes(t))) cell.getFormat().getFill().setColor("#FFFFFF");
if (fill === blueRGB && !blueFill.some(t => fullName.includes(t))) cell.getFormat().getFill().setColor("#FFFFFF");
}
sheet.getRangeByIndexes(startRow - 1, 12, numRows, 1).setValues(columnMValues);
}
function getSheetConfig(sheetName: string): { range: string, editableRanges: string[], dataRowStart: number, dataRowEnd: number } {
if (sheetName === "PHONE 6.100 AC-PC") {
return {
range: "A7:I38",
editableRanges: ["C4", "C5", "B7:B38", "C7:C38", "D7:D38", "E7:E38", "G7:G38", "L3:L5"],
dataRowStart: 7,
dataRowEnd: 38
};
} else if (sheetName === "PHONE 6.200 AC-PC") {
return {
range: "A7:I26",
editableRanges: ["C4", "C5", "B7:B26", "C7:C26", "D7:D26", "E7:E26", "G7:G26", "L3:L5"],
dataRowStart: 7,
dataRowEnd: 26
};
} else if (sheetName === "PHONE 7.200 ICU") {
return {
range: "A9:M20",
editableRanges: ["C4", "C5", "C9:C20", "D9:D20", "G9:G20", "Q5:Q7", "J9:J20", "M9:M20"],
dataRowStart: 9,
dataRowEnd: 20
};
} else if (sheetName === "PHONE 6.200 ICU") {
return {
range: "A7:I18",
editableRanges: ["C4", "C5", "B7:B18", "C7:C18", "D7:D18", "G7:G18","Q5:Q7"],
dataRowStart: 7,
dataRowEnd: 18
};
} else {
throw new Error(`Unsupported sheet: ${sheetName}`);
}
}