@ssg-coop/luic-calculator-core 1.3.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/README.md +12 -2
  2. package/dist/api/domain.d.ts +5 -0
  3. package/dist/api/domain.js +5 -0
  4. package/dist/config/i18n/en.d.ts +19 -0
  5. package/dist/config/i18n/en.js +19 -0
  6. package/dist/config/i18n/fr.d.ts +19 -0
  7. package/dist/config/i18n/fr.js +19 -0
  8. package/dist/config/types.d.ts +2 -0
  9. package/dist/config/types.js +5 -0
  10. package/dist/generator/WorkbookGenerator.d.ts +4 -0
  11. package/dist/generator/WorkbookGenerator.js +69 -9
  12. package/dist/generator/builders/ChartDataSheetBuilder.d.ts +10 -0
  13. package/dist/generator/builders/ChartDataSheetBuilder.js +160 -7
  14. package/dist/generator/builders/ChartsSheetBuilder.js +103 -2
  15. package/dist/generator/builders/InputDataSheetBuilder.js +4 -3
  16. package/dist/generator/builders/ScenarioDetailsBuilder.d.ts +2 -0
  17. package/dist/generator/builders/ScenarioDetailsBuilder.js +4 -4
  18. package/dist/generator/builders/ScenarioSheetBuilder.d.ts +2 -0
  19. package/dist/generator/builders/ScenarioSheetBuilder.js +205 -25
  20. package/dist/generator/builders/SummarySheetBuilder.d.ts +3 -2
  21. package/dist/generator/builders/SummarySheetBuilder.js +109 -9
  22. package/dist/types/config.types.d.ts +9 -0
  23. package/dist/types/workbook.types.d.ts +52 -1
  24. package/package.json +1 -1
  25. package/dist/package.json +0 -31
  26. package/dist/src/config/appConfig.d.ts +0 -11
  27. package/dist/src/config/appConfig.js +0 -38
  28. package/dist/src/config/core/calculatorUtils.d.ts +0 -17
  29. package/dist/src/config/core/calculatorUtils.js +0 -212
  30. package/dist/src/config/core/genericUtils.d.ts +0 -10
  31. package/dist/src/config/core/genericUtils.js +0 -79
  32. package/dist/src/config/domain/buildings.d.ts +0 -14
  33. package/dist/src/config/domain/buildings.js +0 -282
  34. package/dist/src/config/domain/emissionFactors.d.ts +0 -2
  35. package/dist/src/config/domain/emissionFactors.js +0 -42
  36. package/dist/src/config/domain/energySources.d.ts +0 -18
  37. package/dist/src/config/domain/energySources.js +0 -370
  38. package/dist/src/config/domain/infrastructure.d.ts +0 -8
  39. package/dist/src/config/domain/infrastructure.js +0 -56
  40. package/dist/src/config/domain/neighbourhoodTypes.d.ts +0 -10
  41. package/dist/src/config/domain/neighbourhoodTypes.js +0 -366
  42. package/dist/src/config/domain/regions.d.ts +0 -8
  43. package/dist/src/config/domain/regions.js +0 -423
  44. package/dist/src/config/domain/services.d.ts +0 -8
  45. package/dist/src/config/domain/services.js +0 -48
  46. package/dist/src/config/domain/transportation.d.ts +0 -12
  47. package/dist/src/config/domain/transportation.js +0 -81
  48. package/dist/src/config/i18n/en.d.ts +0 -445
  49. package/dist/src/config/i18n/en.js +0 -488
  50. package/dist/src/config/i18n/fr.d.ts +0 -445
  51. package/dist/src/config/i18n/fr.js +0 -491
  52. package/dist/src/config/types.d.ts +0 -80
  53. package/dist/src/config/types.js +0 -5
  54. package/dist/src/generator/FormulaBuilder.d.ts +0 -58
  55. package/dist/src/generator/FormulaBuilder.js +0 -101
  56. package/dist/src/generator/WorkbookGenerator.d.ts +0 -7
  57. package/dist/src/generator/WorkbookGenerator.js +0 -77
  58. package/dist/src/generator/builders/AssumptionsBuilder.d.ts +0 -20
  59. package/dist/src/generator/builders/AssumptionsBuilder.js +0 -311
  60. package/dist/src/generator/builders/CalculationSheetBuilder.d.ts +0 -20
  61. package/dist/src/generator/builders/CalculationSheetBuilder.js +0 -149
  62. package/dist/src/generator/builders/EmissionFactorsSheetBuilder.d.ts +0 -7
  63. package/dist/src/generator/builders/EmissionFactorsSheetBuilder.js +0 -86
  64. package/dist/src/generator/builders/InputDataSheetBuilder.d.ts +0 -13
  65. package/dist/src/generator/builders/InputDataSheetBuilder.js +0 -114
  66. package/dist/src/generator/builders/ScenarioSheetBuilder.d.ts +0 -19
  67. package/dist/src/generator/builders/ScenarioSheetBuilder.js +0 -1067
  68. package/dist/src/generator/builders/SummarySheetBuilder.d.ts +0 -8
  69. package/dist/src/generator/builders/SummarySheetBuilder.js +0 -304
  70. package/dist/src/index.d.ts +0 -57
  71. package/dist/src/index.js +0 -103
  72. package/dist/src/theme/defaultTheme.d.ts +0 -2
  73. package/dist/src/theme/defaultTheme.js +0 -254
  74. package/dist/src/theme/index.d.ts +0 -2
  75. package/dist/src/theme/index.js +0 -2
  76. package/dist/src/theme/sunsetTheme.d.ts +0 -2
  77. package/dist/src/theme/sunsetTheme.js +0 -310
  78. package/dist/src/types/config.types.d.ts +0 -69
  79. package/dist/src/types/config.types.js +0 -1
  80. package/dist/src/types/scenario.types.d.ts +0 -63
  81. package/dist/src/types/scenario.types.js +0 -1
  82. package/dist/src/types/workbook.types.d.ts +0 -181
  83. package/dist/src/types/workbook.types.js +0 -1
  84. package/dist/src/utils/ReactivityUtils.d.ts +0 -7
  85. package/dist/src/utils/ReactivityUtils.js +0 -26
  86. package/dist/src/utils/cellAddressing.d.ts +0 -16
  87. package/dist/src/utils/cellAddressing.js +0 -35
  88. package/dist/src/utils/formatting.d.ts +0 -9
  89. package/dist/src/utils/formatting.js +0 -32
  90. package/dist/src/writers/JsonExporter.d.ts +0 -11
  91. package/dist/src/writers/JsonExporter.js +0 -14
  92. package/dist/types/blinktagtypes.d.ts +0 -167
  93. package/dist/types/blinktagtypes.js +0 -21
@@ -1,1067 +0,0 @@
1
- import { FormattingUtils } from '../../utils/formatting';
2
- import { FormulaBuilder } from '../FormulaBuilder';
3
- import energySourcesConfigFn from '../../config/domain/energySources';
4
- import transportationConfigFn from '../../config/domain/transportation';
5
- import buildingsConfigFn from '../../config/domain/buildings';
6
- import neighbourhoodTypesConfigFn from '../../config/domain/neighbourhoodTypes';
7
- export class ScenarioSheetBuilder {
8
- config;
9
- constructor(config) {
10
- this.config = config;
11
- }
12
- build(scenario, tables, refs) {
13
- const rows = [];
14
- const mergedRanges = [];
15
- let currentRow = 1;
16
- const calcContexts = [];
17
- let calcSheetCurrentRow = 1;
18
- const sheetNameStr = scenario.title.length > 28 ? scenario.title.substring(0, 28) : scenario.title;
19
- const calcSheetName = sheetNameStr.length > 26 ? sheetNameStr.substring(0, 26) + 'i18n:worksheets.calcSuffix' : sheetNameStr + 'i18n:worksheets.calcSuffix';
20
- const escapedCalcSheetName = calcSheetName.replace(/'/g, "''");
21
- const c = this.config.theme.scenario;
22
- const padEmptyCells = (count, color) => {
23
- const cols = ['C', 'D', 'E'];
24
- return cols.slice(0, count).map(col => ({
25
- column: col,
26
- value: '',
27
- format: { fill: { color } }
28
- }));
29
- };
30
- // 1. Scenario Name
31
- rows.push({
32
- rowIndex: currentRow++,
33
- cells: [
34
- { column: 'A', value: 'i18n:forms.fields.scenarioTitle', format: FormattingUtils.boldText(c.colors.topRow) },
35
- { column: 'B', value: scenario.title, format: { fill: { color: c.colors.topRow } } },
36
- ...padEmptyCells(3, c.colors.topRow)
37
- ]
38
- });
39
- // 2. Scenario Description
40
- rows.push({
41
- rowIndex: currentRow++,
42
- cells: [
43
- { column: 'A', value: 'i18n:forms.fields.scenarioDescription', format: FormattingUtils.boldText(c.colors.topRow) },
44
- { column: 'B', value: scenario.description, format: { fill: { color: c.colors.topRow } } },
45
- ...padEmptyCells(3, c.colors.topRow)
46
- ]
47
- });
48
- // 3. Province
49
- rows.push({
50
- rowIndex: currentRow++,
51
- cells: [
52
- { column: 'A', value: 'i18n:forms.fields.province', format: FormattingUtils.boldText(c.colors.topRow) },
53
- { column: 'B', value: scenario.provId, format: { fill: { color: c.colors.topRow } } },
54
- ...padEmptyCells(3, c.colors.topRow)
55
- ]
56
- });
57
- // 4. Year of Full Build-out
58
- rows.push({
59
- rowIndex: currentRow++,
60
- cells: [
61
- { column: 'A', value: 'i18n:forms.fields.buildOutYear', format: FormattingUtils.boldText(c.colors.topRow) },
62
- { column: 'B', value: scenario.year || 2050, format: { fill: { color: c.colors.topRow } } },
63
- ...padEmptyCells(3, c.colors.topRow)
64
- ]
65
- });
66
- // 5. Neighbourhood Type
67
- const neighbourhoodTypeRow = currentRow;
68
- rows.push({
69
- rowIndex: currentRow++,
70
- cells: [
71
- { column: 'A', value: 'i18n:forms.fields.neighbourhoodType', format: FormattingUtils.boldText(c.colors.topRow) },
72
- { column: 'B', value: `i18n:appConfig.neighbourhoods.neighbourhoodTypes.${scenario.neighbourhoodType}` || 'Custom', format: { fill: { color: c.colors.topRow } } },
73
- ...padEmptyCells(3, c.colors.topRow)
74
- ]
75
- });
76
- // 6. LUIC Hyperlink
77
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
78
- rows.push({
79
- rowIndex: currentRow++,
80
- cells: [
81
- {
82
- column: 'A',
83
- value: 'i18n:appInfo.worksheetAttribution',
84
- format: c.fonts.hyperlink,
85
- hyperlink: {
86
- address: 'https://luic-ciut.ssg.coop',
87
- textToDisplay: 'i18n:appInfo.worksheetAttribution'
88
- }
89
- }
90
- ]
91
- });
92
- // 7. Modified
93
- rows.push({
94
- rowIndex: currentRow++,
95
- cells: [
96
- { column: 'A', value: 'i18n:general.modified', format: c.fonts.modified },
97
- { column: 'B', value: new Date(scenario.datetimeModified || Date.now()).toLocaleString() }
98
- ]
99
- });
100
- currentRow++; // Spacer (Row 8)
101
- // Category: Buildings
102
- rows.push({
103
- rowIndex: currentRow++,
104
- cells: [
105
- { column: 'A', value: 'i18n:scenario.modules.buildings', format: FormattingUtils.header(c.colors.buildingsHeader) },
106
- { column: 'B', value: '', format: FormattingUtils.header(c.colors.buildingsHeader) },
107
- { column: 'C', value: '', format: FormattingUtils.header(c.colors.buildingsHeader) },
108
- { column: 'D', value: '', format: FormattingUtils.header(c.colors.buildingsHeader) },
109
- { column: 'E', value: '', format: FormattingUtils.header(c.colors.buildingsHeader) }
110
- ]
111
- });
112
- let dwellingQtyRows = [];
113
- let firstBuilding = true;
114
- for (const [key, bldg] of Object.entries(scenario.buildings || {})) {
115
- if (!firstBuilding) {
116
- rows.push({ rowIndex: currentRow++, cells: [] }); // Spacer
117
- }
118
- firstBuilding = false;
119
- // Building Type Header
120
- rows.push({
121
- rowIndex: currentRow++,
122
- cells: [
123
- { column: 'B', value: `i18n:appConfig.buildings.homeTypes.${key}`, format: { font: { bold: true }, ...c.headers.buildingsSub } },
124
- { column: 'C', value: '', format: c.headers.buildingsSub },
125
- { column: 'D', value: '', format: c.headers.buildingsSub },
126
- { column: 'E', value: '', format: c.headers.buildingsSub }
127
- ]
128
- });
129
- const inputsStartRow = currentRow;
130
- const qtyRef = refs[`buildings.${key}.qty`] || 0;
131
- const qtyRow = currentRow; // This is the row index for the 'Number of Dwellings'
132
- dwellingQtyRows.push(qtyRow);
133
- rows.push({
134
- rowIndex: currentRow++,
135
- cells: [
136
- { column: 'B', value: '', format: c.data.grayFill },
137
- { column: 'C', value: 'i18n:forms.fields.numberDwellings', format: { font: { bold: true } } },
138
- { column: 'D', formula: typeof qtyRef === 'string' && qtyRef.startsWith('=') ? qtyRef : `=${qtyRef}`, format: { horizontalAlignment: 'Right' } }
139
- ]
140
- });
141
- // euiCol matches the column on Assumptions_Buildings
142
- let euiCol = 2; // B equals 2 (single)
143
- if (key === 'row')
144
- euiCol = 3; // C
145
- else if (key === 'apt')
146
- euiCol = 4; // D
147
- const colLetter = String.fromCharCode(64 + euiCol); // B, C, or D
148
- const sizeRef = `i18n:worksheets.assumptions_buildings!$${colLetter}$2`;
149
- const sizeRow = currentRow;
150
- rows.push({
151
- rowIndex: currentRow++,
152
- cells: [
153
- { column: 'B', value: '', format: c.data.grayFill },
154
- { column: 'C', value: 'i18n:forms.fields.floorspacePerUnit', format: { font: { bold: true } } },
155
- { column: 'D', formula: `=${sizeRef}`, format: { ...FormattingUtils.number(0), horizontalAlignment: 'Right' } },
156
- { column: 'E', value: 'm2', format: { horizontalAlignment: 'Left' } }
157
- ]
158
- });
159
- const totalSpaceRow = currentRow;
160
- rows.push({
161
- rowIndex: currentRow++,
162
- cells: [
163
- { column: 'B', value: '', format: c.data.grayFill },
164
- { column: 'C', value: 'i18n:forms.fields.floorspaceTotal', format: { font: { bold: true } } },
165
- { column: 'D', formula: `=D${qtyRow}*D${sizeRow}`, format: { ...FormattingUtils.number(0), horizontalAlignment: 'Right' } },
166
- { column: 'E', value: 'm2', format: { horizontalAlignment: 'Left' } }
167
- ]
168
- });
169
- const codeIdRow = currentRow;
170
- rows.push({
171
- rowIndex: currentRow++,
172
- cells: [
173
- { column: 'B', value: '', format: c.data.grayFill },
174
- { column: 'C', value: 'i18n:forms.fields.buildingEnergyCodeShort', format: { font: { bold: true } } },
175
- { column: 'D', value: `i18n:appConfig.buildings.buildingEnergyCodes.${bldg.buildingEnergyCode}`, format: { horizontalAlignment: 'Right' } }
176
- ]
177
- });
178
- const euiRow = currentRow;
179
- rows.push({
180
- rowIndex: currentRow++,
181
- cells: [
182
- { column: 'B', value: '', format: c.data.grayFill },
183
- { column: 'C', value: 'i18n:forms.fields.euiShort', format: { font: { bold: true } } },
184
- { column: 'D', value: bldg.eui ?? 0, format: { horizontalAlignment: 'Right' } },
185
- { column: 'E', value: `MJ/m2`, format: { horizontalAlignment: 'Left' } }
186
- ]
187
- });
188
- rows.push({
189
- rowIndex: currentRow++,
190
- cells: [
191
- { column: 'B', value: '', format: c.data.grayFill }
192
- ]
193
- }); // Spacer
194
- rows.push({
195
- rowIndex: currentRow++,
196
- cells: [
197
- { column: 'B', value: '', format: c.data.grayFill },
198
- { column: 'C', value: 'Energy Mix', format: { font: { bold: true } } }
199
- ]
200
- });
201
- const energyMixRows = [];
202
- if (bldg.energySources) {
203
- for (const [esKey, esDetail] of Object.entries(bldg.energySources)) {
204
- let label = esKey;
205
- if (esKey === 'grid')
206
- label = 'Grid Electricity';
207
- if (esKey === 'NG')
208
- label = 'Natural Gas';
209
- energyMixRows.push({ sourceId: esKey, rowIdx: currentRow });
210
- rows.push({
211
- rowIndex: currentRow++,
212
- cells: [
213
- { column: 'B', value: '', format: c.data.grayFill },
214
- { column: 'C', value: `i18n:appConfig.energySources.energySources.${esKey === 'NG' ? 'natGas' : esKey}`, format: { font: { bold: true } } },
215
- { column: 'D', value: (esDetail.value || 0) / 100, format: { numberFormat: '0.0%', horizontalAlignment: 'Right' } }
216
- ]
217
- });
218
- }
219
- }
220
- rows.push({
221
- rowIndex: currentRow++,
222
- cells: [
223
- { column: 'B', value: '' }
224
- ]
225
- }); // Spacer
226
- const inputsEndRow = currentRow - 1;
227
- // Apply "i18n:scenario.inputsDefaults" label to the middle row of the group in Column B
228
- const inputsMiddleRowIdx = Math.floor((inputsStartRow + inputsEndRow) / 2);
229
- const inputsMiddleRow = rows.find(r => r.rowIndex === inputsMiddleRowIdx);
230
- if (inputsMiddleRow) {
231
- const bCell = inputsMiddleRow.cells.find(c => c.column === 'B');
232
- if (bCell) {
233
- bCell.value = 'i18n:scenario.inputsDefaults';
234
- bCell.format = c.data.labelFormat;
235
- }
236
- }
237
- const outputsStartRow = currentRow;
238
- const energyUseRow = currentRow;
239
- rows.push({
240
- rowIndex: currentRow++,
241
- cells: [
242
- { column: 'B', value: '', format: c.data.grayFill },
243
- { column: 'C', value: 'i18n:scenario.energyUse', format: { font: { bold: true } } },
244
- { column: 'D', formula: `=D${totalSpaceRow}*D${euiRow}*0.001`, format: { numberFormat: '#,##0', horizontalAlignment: 'Right' } },
245
- { column: 'E', value: 'GJ/yr', format: { horizontalAlignment: 'Left' } }
246
- ]
247
- });
248
- rows.push({
249
- rowIndex: currentRow++,
250
- cells: [
251
- { column: 'B', value: '', format: c.data.grayFill },
252
- { column: 'C', value: 'i18n:scenario.emissions', format: { font: { bold: true } } }
253
- ]
254
- });
255
- if (bldg.emissions) {
256
- const emKeys = ['CO2', 'CH4', 'N2O', 'i18n:general.total'];
257
- const numMixes = energyMixRows.length;
258
- calcSheetCurrentRow += 1; // item header
259
- const gasTotalRows = {};
260
- for (const gas of ['CO2', 'CH4', 'N2O']) {
261
- gasTotalRows[gas] = calcSheetCurrentRow + 4 + numMixes;
262
- calcSheetCurrentRow += 6 + numMixes;
263
- }
264
- calcSheetCurrentRow += 2; // total and spacer
265
- calcContexts.push({
266
- category: 'Buildings',
267
- itemName: key,
268
- energyMixRefs: energyMixRows.map(m => ({ sourceId: m.sourceId, mixRow: m.rowIdx })),
269
- energyUseRow: energyUseRow
270
- });
271
- emKeys.forEach((gas, idx) => {
272
- const rowCells = [
273
- { column: 'B', value: '', format: c.data.grayFill },
274
- { column: 'C', value: gas, format: { font: { bold: true } } }
275
- ];
276
- if (gas === 'i18n:general.total') {
277
- const sumFormula = `=SUM(D${currentRow - 3}:D${currentRow - 1})`;
278
- rowCells.push({ column: 'D', formula: sumFormula, format: { numberFormat: '#,##0.00', horizontalAlignment: 'Right' } });
279
- rowCells.push({ column: 'E', value: 'tCO2e/yr', format: { horizontalAlignment: 'Left' } });
280
- }
281
- else {
282
- const formula = `='${escapedCalcSheetName}'!$B$${gasTotalRows[gas]}`;
283
- const fallbackVal = bldg.emissions[gas] || 0;
284
- rowCells.push({ column: 'D', formula, value: fallbackVal, format: { numberFormat: '#,##0.00', horizontalAlignment: 'Right' } });
285
- rowCells.push({ column: 'E', value: 'tCO2e/yr', format: { horizontalAlignment: 'Left' } });
286
- }
287
- rows.push({ rowIndex: currentRow++, cells: rowCells });
288
- });
289
- }
290
- const outputsEndRow = currentRow - 1;
291
- // Apply "i18n:scenario.outputs" label to the middle row of the group in Column B
292
- const outputsMiddleRowIdx = Math.floor((outputsStartRow + outputsEndRow) / 2);
293
- const outputsMiddleRow = rows.find(r => r.rowIndex === outputsMiddleRowIdx);
294
- if (outputsMiddleRow) {
295
- const bCell = outputsMiddleRow.cells.find(c => c.column === 'B');
296
- if (bCell) {
297
- bCell.value = 'i18n:scenario.outputs';
298
- bCell.format = c.data.labelFormat;
299
- }
300
- }
301
- }
302
- currentRow++; // Spacer
303
- const totalDwellingsRef = dwellingQtyRows.length > 0 ? `SUM(${dwellingQtyRows.map(r => `D${r}`).join(',')})` : '0';
304
- const escapedScenarioSheetName = sheetNameStr.replace(/'/g, "''");
305
- const totalDwellingsForCalcSheet = dwellingQtyRows.length > 0
306
- ? `SUM(${dwellingQtyRows.map(r => `'${escapedScenarioSheetName}'!D${r}`).join(',')})`
307
- : '0';
308
- // Category: Services
309
- if (scenario.services) {
310
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
311
- rows.push({
312
- rowIndex: currentRow++,
313
- cells: [
314
- { column: 'A', value: 'i18n:scenario.modules.services', format: FormattingUtils.header(c.colors.servicesHeader) }
315
- ]
316
- });
317
- const servicesStartRow = currentRow;
318
- // Student Calculation Metadata (to be used in Calc sheet)
319
- // Note: We'll put these in the Calc sheet.
320
- // The Scenario sheet school item will reference the Calc sheet's student count.
321
- const totalStudentsCalcCell = `__TOTAL_STUDENTS_REF__`;
322
- const inputsStartRow = currentRow;
323
- // Horizontal Table Headers for Inputs
324
- rows.push({
325
- rowIndex: currentRow++,
326
- cells: [
327
- { column: 'A', value: '', format: c.data.grayFill },
328
- { column: 'B', value: 'i18n:scenario.modules.service', format: FormattingUtils.header(c.colors.servicesSub) },
329
- { column: 'C', value: 'i18n:forms.fields.capitalCosts ($/i18n:general.dwelling)', format: FormattingUtils.header(c.colors.servicesSub) },
330
- { column: 'D', value: 'i18n:forms.fields.maintCosts ($/i18n:general.dwelling/i18n:general.yearAbbr)', format: FormattingUtils.header(c.colors.servicesSub) },
331
- { column: 'E', value: '', format: FormattingUtils.header(c.colors.servicesSub) },
332
- ]
333
- });
334
- for (const [key, srv] of Object.entries(scenario.services)) {
335
- const srvIdCell = `"i18n:appConfig.services.serviceTypes.${key}"`;
336
- const capLookup = `VLOOKUP(${srvIdCell},${tables.services},2,FALSE)`;
337
- const maintLookup = `VLOOKUP(${srvIdCell},${tables.services},3,FALSE)`;
338
- rows.push({
339
- rowIndex: currentRow++,
340
- cells: [
341
- { column: 'A', value: '', format: c.data.grayFill },
342
- { column: 'B', value: `i18n:appConfig.services.serviceTypes.${key}`, format: { font: { bold: true } } },
343
- {
344
- column: 'C',
345
- formula: `=${capLookup}`,
346
- format: FormattingUtils.currency(0)
347
- },
348
- {
349
- column: 'D',
350
- formula: `=${maintLookup}`,
351
- format: FormattingUtils.currency(0)
352
- }
353
- ]
354
- });
355
- }
356
- const inputsEndRow = currentRow - 1;
357
- const inputsMiddleRowIdx = Math.floor((inputsStartRow + inputsEndRow) / 2);
358
- const inputsMiddleRow = rows.find(r => r.rowIndex === inputsMiddleRowIdx);
359
- if (inputsMiddleRow) {
360
- const aCell = inputsMiddleRow.cells.find(c => c.column === 'A');
361
- if (aCell) {
362
- aCell.value = 'i18n:scenario.inputsDefaults';
363
- aCell.format = c.data.labelFormat;
364
- }
365
- else {
366
- inputsMiddleRow.cells.push({ column: 'A', value: 'i18n:scenario.inputsDefaults', format: c.data.labelFormat });
367
- }
368
- }
369
- rows.push({
370
- rowIndex: currentRow++,
371
- cells: [
372
- { column: 'A', value: '', format: c.data.grayFill }
373
- ]
374
- }); // Spacer
375
- const outputsStartRow = currentRow;
376
- // Horizontal Table Headers for i18n:scenario.outputs
377
- rows.push({
378
- rowIndex: currentRow++,
379
- cells: [
380
- { column: 'A', value: '', format: c.data.grayFill },
381
- { column: 'B', value: 'i18n:scenario.modules.service', format: FormattingUtils.header(c.colors.servicesSub) },
382
- { column: 'C', value: 'i18n:forms.fields.capitalCosts (i18n:general.total)', format: FormattingUtils.header(c.colors.servicesSub) },
383
- { column: 'D', value: 'i18n:forms.fields.maintCosts (i18n:general.total/yr)', format: FormattingUtils.header(c.colors.servicesSub) },
384
- { column: 'E', value: '', format: FormattingUtils.header(c.colors.servicesSub) },
385
- ]
386
- });
387
- const outputsDataStartRow = currentRow;
388
- for (const [key, srv] of Object.entries(scenario.services)) {
389
- let srvQtyFormula;
390
- if (key === 'school') {
391
- srvQtyFormula = totalStudentsCalcCell;
392
- }
393
- else {
394
- srvQtyFormula = `=${totalDwellingsRef}`;
395
- }
396
- const srvIdCell = `"i18n:appConfig.services.serviceTypes.${key}"`;
397
- const capLookup = `VLOOKUP(${srvIdCell},${tables.services},2,FALSE)`;
398
- const maintLookup = `VLOOKUP(${srvIdCell},${tables.services},3,FALSE)`;
399
- rows.push({
400
- rowIndex: currentRow++,
401
- cells: [
402
- { column: 'A', value: '', format: c.data.grayFill },
403
- { column: 'B', value: `i18n:appConfig.services.serviceTypes.${key}`, format: { font: { bold: true } } },
404
- {
405
- column: 'C',
406
- formula: new FormulaBuilder().start().ref(srvQtyFormula).multiplyBy(capLookup).build(),
407
- format: FormattingUtils.currency(0)
408
- },
409
- {
410
- column: 'D',
411
- formula: new FormulaBuilder().start().ref(srvQtyFormula).multiplyBy(maintLookup).build(),
412
- format: FormattingUtils.currency(0)
413
- }
414
- ]
415
- });
416
- }
417
- // Total row
418
- const outputsDataEndRow = currentRow - 1;
419
- rows.push({
420
- rowIndex: currentRow++,
421
- cells: [
422
- { column: 'A', value: '', format: c.data.grayFill },
423
- { column: 'B', value: 'i18n:general.total', format: { font: { bold: true } } },
424
- { column: 'C', formula: `=SUM(C${outputsDataStartRow}:C${outputsDataEndRow})`, format: { ...FormattingUtils.currency(0), font: { bold: true } } },
425
- { column: 'D', formula: `=SUM(D${outputsDataStartRow}:D${outputsDataEndRow})`, format: { ...FormattingUtils.currency(0), font: { bold: true } } }
426
- ]
427
- });
428
- const outputsEndRow = currentRow - 1;
429
- const outputsMiddleRowIdx = Math.floor((outputsStartRow + outputsEndRow) / 2);
430
- const outputsMiddleRow = rows.find(r => r.rowIndex === outputsMiddleRowIdx);
431
- if (outputsMiddleRow) {
432
- const aCell = outputsMiddleRow.cells.find(c => c.column === 'A');
433
- if (aCell) {
434
- aCell.value = 'i18n:scenario.outputs';
435
- aCell.format = c.data.labelFormat;
436
- }
437
- else {
438
- outputsMiddleRow.cells.push({ column: 'A', value: 'i18n:scenario.outputs', format: c.data.labelFormat });
439
- }
440
- }
441
- currentRow++; // Spacer
442
- }
443
- // Category: Infrastructure
444
- if (scenario.infrastructure) {
445
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
446
- rows.push({
447
- rowIndex: currentRow++,
448
- cells: [
449
- { column: 'A', value: 'i18n:scenario.modules.infrastructure', format: FormattingUtils.header(c.colors.infraHeader) }
450
- ]
451
- });
452
- const infrastructureStartRow = currentRow;
453
- // Quantities Section
454
- rows.push({
455
- rowIndex: currentRow++,
456
- cells: [
457
- { column: 'A', value: '', format: c.data.grayFill },
458
- { column: 'B', value: 'i18n:scenario.modules.infrastructure', format: { font: { bold: true } } },
459
- { column: 'C', value: 'i18n:forms.fields.length (m)', format: { font: { bold: true } } }
460
- ]
461
- });
462
- const qtyRows = {};
463
- for (const [key, infRaw] of Object.entries(scenario.infrastructure)) {
464
- const qtyRef = refs[`infrastructure.${key}.qty`] || infRaw.qty;
465
- const refNoEq = typeof qtyRef === 'string' ? qtyRef.replace('=', '') : qtyRef;
466
- qtyRows[key] = currentRow;
467
- rows.push({
468
- rowIndex: currentRow++,
469
- cells: [
470
- { column: 'A', value: '', format: c.data.grayFill },
471
- { column: 'B', value: `i18n:appConfig.infrastructure.infrastructureTypes.${key}`, format: { font: { bold: true } } },
472
- typeof qtyRef === 'string' ? { column: 'C', formula: `=${refNoEq}`, format: { numberFormat: '#,##0', horizontalAlignment: 'Right' } }
473
- : { column: 'C', value: qtyRef, format: { numberFormat: '#,##0', horizontalAlignment: 'Right' } }
474
- ]
475
- });
476
- }
477
- rows.push({
478
- rowIndex: currentRow++,
479
- cells: [
480
- { column: 'A', value: 'i18n:scenario.inputsDefaults', format: c.data.labelFormat }
481
- ]
482
- });
483
- // Inputs Section
484
- const inputsStartRow = currentRow;
485
- rows.push({
486
- rowIndex: currentRow++,
487
- cells: [
488
- { column: 'A', value: '', format: c.data.grayFill },
489
- { column: 'B', value: 'i18n:scenario.modules.infrastructure', format: FormattingUtils.header(c.colors.infraSub) },
490
- { column: 'C', value: 'i18n:forms.fields.capitalCosts ($/m)', format: FormattingUtils.header(c.colors.infraSub) },
491
- { column: 'D', value: 'i18n:forms.fields.maintCosts ($/m/i18n:general.yearAbbr)', format: FormattingUtils.header(c.colors.infraSub) },
492
- { column: 'E', value: '', format: FormattingUtils.header(c.colors.infraSub) },
493
- ]
494
- });
495
- const inputsRows = {};
496
- for (const [key, infRaw] of Object.entries(scenario.infrastructure)) {
497
- inputsRows[key] = currentRow;
498
- const capRef = refs[`infrastructure.${key}.capCostIntensity`] || infRaw.capCostIntensity;
499
- const maintRef = refs[`infrastructure.${key}.maintCostIntensity`] || infRaw.maintCostIntensity;
500
- const capFormula = typeof capRef === 'string' ? `=${capRef.replace('=', '')}` : null;
501
- const maintFormula = typeof maintRef === 'string' ? `=${maintRef.replace('=', '')}` : null;
502
- const cCell = { column: 'C', format: FormattingUtils.currency(0) };
503
- if (capFormula)
504
- cCell.formula = capFormula;
505
- else
506
- cCell.value = capRef;
507
- const dCell = { column: 'D', format: FormattingUtils.currency(0) };
508
- if (maintFormula)
509
- dCell.formula = maintFormula;
510
- else
511
- dCell.value = maintRef;
512
- rows.push({
513
- rowIndex: currentRow++,
514
- cells: [
515
- { column: 'A', value: '', format: c.data.grayFill },
516
- { column: 'B', value: `i18n:appConfig.infrastructure.infrastructureTypes.${key}`, format: { font: { bold: true } } },
517
- cCell,
518
- dCell
519
- ]
520
- });
521
- }
522
- const inputsEndRow = currentRow - 1;
523
- currentRow++; // Spacer
524
- // i18n:scenario.outputs Section
525
- const outputsStartRow = currentRow;
526
- rows.push({
527
- rowIndex: currentRow++,
528
- cells: [
529
- { column: 'A', value: '', format: c.data.grayFill },
530
- { column: 'B', value: 'i18n:scenario.modules.infrastructure', format: FormattingUtils.header(c.colors.infraSub) },
531
- { column: 'C', value: 'i18n:forms.fields.capitalCosts (i18n:general.total)', format: FormattingUtils.header(c.colors.infraSub) },
532
- { column: 'D', value: 'i18n:forms.fields.maintCosts (i18n:general.total/yr)', format: FormattingUtils.header(c.colors.infraSub) },
533
- { column: 'E', value: '', format: FormattingUtils.header(c.colors.infraSub) },
534
- ]
535
- });
536
- const outputsDataStartRow = currentRow;
537
- for (const [key, infRaw] of Object.entries(scenario.infrastructure)) {
538
- const qtyFormula = `C${qtyRows[key]}`;
539
- const capRef = `C${inputsRows[key]}`;
540
- const maintRef = `D${inputsRows[key]}`;
541
- rows.push({
542
- rowIndex: currentRow++,
543
- cells: [
544
- { column: 'A', value: '', format: c.data.grayFill },
545
- { column: 'B', value: `i18n:appConfig.infrastructure.infrastructureTypes.${key}`, format: { font: { bold: true } } },
546
- {
547
- column: 'C',
548
- formula: new FormulaBuilder().start().ref(qtyFormula).multiplyBy(capRef).build(),
549
- format: FormattingUtils.currency(0)
550
- },
551
- {
552
- column: 'D',
553
- formula: new FormulaBuilder().start().ref(qtyFormula).multiplyBy(maintRef).build(),
554
- format: FormattingUtils.currency(0)
555
- }
556
- ]
557
- });
558
- }
559
- // Total row
560
- const outputsDataEndRow = currentRow - 1;
561
- rows.push({
562
- rowIndex: currentRow++,
563
- cells: [
564
- { column: 'A', value: '', format: c.data.grayFill },
565
- { column: 'B', value: 'i18n:general.total', format: { font: { bold: true } } },
566
- { column: 'C', formula: `=SUM(C${outputsDataStartRow}:C${outputsDataEndRow})`, format: { ...FormattingUtils.currency(0), font: { bold: true } } },
567
- { column: 'D', formula: `=SUM(D${outputsDataStartRow}:D${outputsDataEndRow})`, format: { ...FormattingUtils.currency(0), font: { bold: true } } }
568
- ]
569
- });
570
- const outputsEndRow = currentRow - 1;
571
- const outputsMiddleRowIdx = Math.floor((outputsStartRow + outputsEndRow) / 2);
572
- const outputsMiddleRow = rows.find(r => r.rowIndex === outputsMiddleRowIdx);
573
- if (outputsMiddleRow) {
574
- const aCell = outputsMiddleRow.cells.find(c => c.column === 'A');
575
- if (aCell) {
576
- aCell.value = 'i18n:scenario.outputs';
577
- aCell.format = c.data.labelFormat;
578
- }
579
- else {
580
- outputsMiddleRow.cells.push({ column: 'A', value: 'i18n:scenario.outputs', format: c.data.labelFormat });
581
- }
582
- }
583
- currentRow++; // Spacer
584
- }
585
- // Category: Transportation
586
- if (scenario.transportation) {
587
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
588
- rows.push({
589
- rowIndex: currentRow++,
590
- cells: [
591
- { column: 'A', value: 'i18n:scenario.modules.transportation', format: FormattingUtils.header(c.colors.transHeader) }
592
- ]
593
- });
594
- for (const [key, tptRaw] of Object.entries(scenario.transportation)) {
595
- const tpt = tptRaw;
596
- const isNonMotorized = (key === 'walk' || key === 'bike');
597
- // Transportation Type Header
598
- rows.push({
599
- rowIndex: currentRow++,
600
- cells: [
601
- { column: 'B', value: `i18n:appConfig.transportation.transportationTypes.${key}`, format: { font: { bold: true }, ...c.headers.transSub } },
602
- { column: 'C', value: '', format: c.headers.transSub },
603
- { column: 'D', value: '', format: c.headers.transSub },
604
- { column: 'E', value: '', format: c.headers.transSub }
605
- ]
606
- });
607
- const qtyRef = refs[`transportation.${key}.qty`] || 0;
608
- const refNoEq = typeof qtyRef === 'string' ? qtyRef.replace('=', '') : qtyRef;
609
- const inputsStartRow = currentRow;
610
- rows.push({
611
- rowIndex: currentRow++,
612
- cells: [
613
- { column: 'B', value: '', format: c.data.grayFill },
614
- { column: 'C', value: 'i18n:forms.fields.annualKT', format: { font: { bold: true } } },
615
- { column: 'D', formula: `=${refNoEq}`, format: { numberFormat: '#,##0', horizontalAlignment: 'Right' } },
616
- { column: 'E', value: 'VKT/i18n:general.dwelling', format: { horizontalAlignment: 'Left' } }
617
- ]
618
- });
619
- const vktDwellingRow = currentRow - 1;
620
- // Total VKT row
621
- rows.push({
622
- rowIndex: currentRow++,
623
- cells: [
624
- { column: 'B', value: '', format: c.data.grayFill },
625
- { column: 'D', formula: `=D${vktDwellingRow}*${totalDwellingsRef}`, format: { numberFormat: '#,##0', horizontalAlignment: 'Right' } },
626
- { column: 'E', value: 'i18n:forms.fields.totalVKT', format: { horizontalAlignment: 'Left' } }
627
- ]
628
- });
629
- const energyMixRowsTpt = [];
630
- if (!isNonMotorized) {
631
- rows.push({
632
- rowIndex: currentRow++,
633
- cells: [
634
- { column: 'B', value: '', format: c.data.grayFill }
635
- ]
636
- }); // Spacer
637
- rows.push({
638
- rowIndex: currentRow++,
639
- cells: [
640
- { column: 'B', value: '', format: c.data.grayFill },
641
- { column: 'C', value: 'i18n:forms.fields.energyMix', format: { font: { bold: true } } }
642
- ]
643
- });
644
- if (tpt.energySources) {
645
- for (const [esKey, esDetail] of Object.entries(tpt.energySources)) {
646
- let label = esKey;
647
- if (esKey === 'gasoline')
648
- label = 'Gasoline';
649
- if (esKey === 'grid')
650
- label = 'Grid Electricity';
651
- if (esKey === 'diesel')
652
- label = 'Biodiesel Fuel';
653
- if (esKey === 'hydrogen')
654
- label = 'Hydrogen';
655
- energyMixRowsTpt.push({ sourceId: esKey, rowIdx: currentRow });
656
- rows.push({
657
- rowIndex: currentRow++,
658
- cells: [
659
- { column: 'B', value: '', format: c.data.grayFill },
660
- { column: 'C', value: `i18n:appConfig.energySources.energySources.${esKey === 'NG' ? 'natGas' : esKey}`, format: { font: { bold: true } } },
661
- { column: 'D', value: (esDetail.value || 0) / 100, format: { numberFormat: '0.0%', horizontalAlignment: 'Right' } }
662
- ]
663
- });
664
- }
665
- }
666
- rows.push({
667
- rowIndex: currentRow++,
668
- cells: [
669
- { column: 'B', value: '', format: c.data.grayFill }
670
- ]
671
- }); // Spacer
672
- }
673
- const inputsEndRow = currentRow - 1;
674
- const inputsMiddleRowIdx = Math.floor((inputsStartRow + inputsEndRow) / 2);
675
- const inputsMiddleRow = rows.find(r => r.rowIndex === inputsMiddleRowIdx);
676
- if (inputsMiddleRow) {
677
- const bCell = inputsMiddleRow.cells.find(c => c.column === 'B');
678
- if (bCell) {
679
- bCell.value = 'i18n:scenario.inputsDefaults';
680
- bCell.format = c.data.labelFormat;
681
- }
682
- }
683
- if (!isNonMotorized) {
684
- const outputsStartRow = currentRow;
685
- const energyUseRowTpt = currentRow;
686
- rows.push({
687
- rowIndex: currentRow++,
688
- cells: [
689
- { column: 'B', value: '', format: c.data.grayFill },
690
- { column: 'C', value: 'i18n:scenario.energyUse', format: { font: { bold: true } } },
691
- { column: 'D', value: (tpt.energyUse || 0) / 1000, format: { numberFormat: '#,##0.00', horizontalAlignment: 'Right' } },
692
- { column: 'E', value: 'GJ/i18n:general.yearAbbr', format: { horizontalAlignment: 'Left' } }
693
- ]
694
- });
695
- rows.push({
696
- rowIndex: currentRow++,
697
- cells: [
698
- { column: 'B', value: '', format: c.data.grayFill },
699
- { column: 'C', value: 'i18n:scenario.emissions', format: { font: { bold: true } } }
700
- ]
701
- });
702
- if (tpt.emissions) {
703
- const emKeys = ['CO2', 'CH4', 'N2O', 'i18n:general.total'];
704
- const numMixes = energyMixRowsTpt.length;
705
- calcSheetCurrentRow += 1;
706
- const gasTotalRows = {};
707
- for (const gas of ['CO2', 'CH4', 'N2O']) {
708
- gasTotalRows[gas] = calcSheetCurrentRow + 4 + numMixes;
709
- calcSheetCurrentRow += 6 + numMixes;
710
- }
711
- calcSheetCurrentRow += 2;
712
- calcContexts.push({
713
- category: 'Transportation',
714
- itemName: key,
715
- energyMixRefs: energyMixRowsTpt.map(m => ({ sourceId: m.sourceId, mixRow: m.rowIdx })),
716
- energyUseRow: energyUseRowTpt
717
- });
718
- emKeys.forEach((gas, idx) => {
719
- const rowCells = [
720
- { column: 'B', value: '', format: c.data.grayFill },
721
- { column: 'C', value: gas, format: { font: { bold: true } } }
722
- ];
723
- if (gas === 'i18n:general.total') {
724
- const sumFormula = `=SUM(D${currentRow - 3}:D${currentRow - 1})`;
725
- rowCells.push({ column: 'D', formula: sumFormula, format: { numberFormat: '#,##0.00', horizontalAlignment: 'Right' } });
726
- rowCells.push({ column: 'E', value: 'tCO2e/i18n:general.yearAbbr', format: { horizontalAlignment: 'Left' } });
727
- }
728
- else {
729
- const formula = `='${escapedCalcSheetName}'!$B$${gasTotalRows[gas]}`;
730
- const fallbackVal = tpt.emissions[gas] || 0;
731
- rowCells.push({ column: 'D', formula, value: fallbackVal, format: { numberFormat: '#,##0.00', horizontalAlignment: 'Right' } });
732
- rowCells.push({ column: 'E', value: 'tCO2e/i18n:general.yearAbbr', format: { horizontalAlignment: 'Left' } });
733
- }
734
- rows.push({ rowIndex: currentRow++, cells: rowCells });
735
- });
736
- }
737
- const outputsEndRow = currentRow - 1;
738
- const outputsMiddleRowIdx = Math.floor((outputsStartRow + outputsEndRow) / 2);
739
- const outputsMiddleRow = rows.find(r => r.rowIndex === outputsMiddleRowIdx);
740
- if (outputsMiddleRow) {
741
- const bCell = outputsMiddleRow.cells.find(c => c.column === 'B');
742
- if (bCell) {
743
- bCell.value = 'i18n:scenario.outputs';
744
- bCell.format = c.data.labelFormat;
745
- }
746
- }
747
- }
748
- currentRow++; // Spacer
749
- if (isNonMotorized) {
750
- currentRow++; // Extra spacer for non-motorized as requested
751
- }
752
- }
753
- }
754
- // Category: Municipal Revenue
755
- if (scenario.revenue) {
756
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
757
- rows.push({
758
- rowIndex: currentRow++,
759
- cells: [
760
- { column: 'A', value: 'i18n:scenario.modules.revenueLong', format: FormattingUtils.header('#ea9999') }
761
- ]
762
- });
763
- const revSubHeaderFill = { fill: { color: "#f4cccc" } };
764
- let devChargesStartRow = -1;
765
- let devChargesEndRow = -1;
766
- let propTaxesStartRow = -1;
767
- let propTaxesEndRow = -1;
768
- // Development Charges
769
- if (scenario.revenue.developmentCharges) {
770
- rows.push({
771
- rowIndex: currentRow++,
772
- cells: [
773
- { column: 'B', value: 'i18n:appConfig.revenue.revenueTypes.developmentCharges', format: { font: { bold: true }, ...revSubHeaderFill } },
774
- { column: 'C', value: '$/i18n:general.dwelling', format: { font: { bold: true }, ...revSubHeaderFill, horizontalAlignment: 'Center' } },
775
- { column: 'D', value: 'i18n:general.total', format: { font: { bold: true }, ...revSubHeaderFill, horizontalAlignment: 'Center' } },
776
- ]
777
- });
778
- devChargesStartRow = currentRow;
779
- for (const [bType, rev] of Object.entries(scenario.revenue.developmentCharges)) {
780
- const qtyRef = refs[`buildings.${bType}.qty`] || (scenario.buildings && scenario.buildings[bType] ? scenario.buildings[bType].qty : 0);
781
- const refStr = typeof qtyRef === 'string' && qtyRef.startsWith('=') ? qtyRef.substring(1) : qtyRef;
782
- const perUnitRef = refs[`revenue.developmentCharges.${bType}.perUnit`] || rev.perUnit || 0;
783
- const perUnitStr = typeof perUnitRef === 'string' && perUnitRef.startsWith('=') ? perUnitRef.substring(1) : perUnitRef;
784
- const perUnitCell = typeof perUnitRef === 'string'
785
- ? { column: 'C', formula: `=${perUnitStr}`, format: FormattingUtils.currency(0) }
786
- : { column: 'C', value: perUnitRef, format: FormattingUtils.currency(0) };
787
- rows.push({
788
- rowIndex: currentRow,
789
- cells: [
790
- { column: 'B', value: `i18n:appConfig.buildings.homeTypes.${bType}`, format: { font: { bold: true } } },
791
- perUnitCell,
792
- { column: 'D', formula: `=${refStr}*C${currentRow}`, format: FormattingUtils.currency(0) }
793
- ]
794
- });
795
- currentRow++;
796
- }
797
- devChargesEndRow = currentRow - 1;
798
- rows.push({
799
- rowIndex: currentRow++,
800
- cells: []
801
- }); // Spacer
802
- }
803
- // Property Taxes
804
- if (scenario.revenue.propertyTaxes) {
805
- rows.push({
806
- rowIndex: currentRow++,
807
- cells: [
808
- { column: 'B', value: 'i18n:appConfig.revenue.revenueTypes.propertyTaxes', format: { font: { bold: true }, ...revSubHeaderFill } },
809
- { column: 'C', value: '$/i18n:general.dwelling/i18n:general.yearAbbr', format: { font: { bold: true }, ...revSubHeaderFill, horizontalAlignment: 'Center' } },
810
- { column: 'D', value: 'i18n:general.total/i18n:general.yearAbbr', format: { font: { bold: true }, ...revSubHeaderFill, horizontalAlignment: 'Center' } }
811
- ]
812
- });
813
- propTaxesStartRow = currentRow;
814
- for (const [bType, rev] of Object.entries(scenario.revenue.propertyTaxes)) {
815
- const qtyRef = refs[`buildings.${bType}.qty`] || (scenario.buildings && scenario.buildings[bType] ? scenario.buildings[bType].qty : 0);
816
- const refStr = typeof qtyRef === 'string' && qtyRef.startsWith('=') ? qtyRef.substring(1) : qtyRef;
817
- const perUnitRef = refs[`revenue.propertyTaxes.${bType}.perUnit`] || rev.perUnit || 0;
818
- const perUnitStr = typeof perUnitRef === 'string' && perUnitRef.startsWith('=') ? perUnitRef.substring(1) : perUnitRef;
819
- const perUnitCell = typeof perUnitRef === 'string'
820
- ? { column: 'C', formula: `=${perUnitStr}`, format: FormattingUtils.currency(0) }
821
- : { column: 'C', value: perUnitRef, format: FormattingUtils.currency(0) };
822
- rows.push({
823
- rowIndex: currentRow,
824
- cells: [
825
- { column: 'B', value: `i18n:appConfig.buildings.homeTypes.${bType}`, format: { font: { bold: true } } },
826
- perUnitCell,
827
- { column: 'D', formula: `=${refStr}*C${currentRow}`, format: FormattingUtils.currency(0) }
828
- ]
829
- });
830
- currentRow++;
831
- }
832
- propTaxesEndRow = currentRow - 1;
833
- rows.push({
834
- rowIndex: currentRow++,
835
- cells: []
836
- }); // Spacer
837
- }
838
- // Total Revenue
839
- rows.push({
840
- rowIndex: currentRow++,
841
- cells: [
842
- { column: 'B', value: 'i18n:scenario.modules.revenueTotal', format: { font: { bold: true }, ...revSubHeaderFill } },
843
- { column: 'C', value: '', format: revSubHeaderFill },
844
- { column: 'D', value: '', format: revSubHeaderFill },
845
- { column: 'E', value: '', format: revSubHeaderFill }
846
- ]
847
- });
848
- if (devChargesStartRow !== -1) {
849
- rows.push({
850
- rowIndex: currentRow++,
851
- cells: [
852
- { column: 'C', value: 'i18n:appConfig.revenue.revenueTypes.developmentCharges', format: { font: { bold: true } } },
853
- { column: 'D', formula: `=SUM(D${devChargesStartRow}:D${devChargesEndRow})`, format: FormattingUtils.currency(0) }
854
- ]
855
- });
856
- }
857
- if (propTaxesStartRow !== -1) {
858
- rows.push({
859
- rowIndex: currentRow++,
860
- cells: [
861
- { column: 'C', value: 'i18n:appConfig.revenue.revenueTypes.propertyTaxes', format: { font: { bold: true } } },
862
- { column: 'D', formula: `=SUM(D${propTaxesStartRow}:D${propTaxesEndRow})`, format: FormattingUtils.currency(0) }
863
- ]
864
- });
865
- }
866
- }
867
- currentRow++; // Spacer before Assumptions & References
868
- this.appendAssumptionsAndReferences(scenario, currentRow, rows, mergedRanges);
869
- currentRow = rows[rows.length - 1].rowIndex + 1;
870
- const sheetName = scenario.title.length > 28 ? scenario.title.substring(0, 28) : scenario.title;
871
- // Force 'charts' array to exist as requested
872
- const charts = [];
873
- const sheet = {
874
- name: sheetName,
875
- sheetType: 'scenario',
876
- visibility: 'Visible',
877
- tabColor: null, // explicit null as per mock
878
- showGridlines: false,
879
- customProperties: {
880
- // Not emitting ssg-scenarioId as per mock
881
- 'ssg-type': 'scenario'
882
- },
883
- columnWidths: [
884
- { range: 'A:A', width: c.columns.A.width },
885
- { range: 'B:B', width: c.columns.B.width },
886
- { range: 'C:C', width: c.columns.C.width },
887
- { range: 'D:D', width: c.columns.D.width },
888
- { range: 'E:E', width: c.columns.E.width }
889
- ],
890
- mergedRanges,
891
- charts,
892
- rows
893
- };
894
- let studentCalc;
895
- if (scenario.services) {
896
- studentCalc = {
897
- totalDwellingsFormula: `=${totalDwellingsForCalcSheet}`,
898
- studentFactorLookup: `VLOOKUP('${escapedScenarioSheetName}'!$B$${neighbourhoodTypeRow},${tables.neighbourhoods},2,FALSE)`
899
- };
900
- }
901
- return { sheet, calcContexts, studentCalc };
902
- }
903
- appendAssumptionsAndReferences(scenario, currentRow, rows, mergedRanges) {
904
- const c = this.config.theme.scenario;
905
- const t = (key) => `i18n:${key}`;
906
- const energySourcesConfig = energySourcesConfigFn(t, scenario.provId);
907
- const transportationConfig = transportationConfigFn(t);
908
- const buildingsConfig = buildingsConfigFn(t);
909
- const neighbourhoodConfig = neighbourhoodTypesConfigFn(t);
910
- // Helper: appends domain config reference arrays (each ref is [label, optionalUrl?])
911
- const appendRefs = (refs) => {
912
- refs.forEach((ref) => {
913
- const label = ref[0] || '';
914
- const link = ref[1];
915
- const cells = [{ column: 'A', value: label }];
916
- if (link) {
917
- cells.push({
918
- column: 'B',
919
- value: link,
920
- format: c.fonts.hyperlink,
921
- hyperlink: { address: link, textToDisplay: link }
922
- });
923
- }
924
- rows.push({ rowIndex: currentRow++, cells });
925
- });
926
- };
927
- const neighbourhood = scenario.neighbourhoodType
928
- ? neighbourhoodConfig.neighbourhoods[scenario.neighbourhoodType]
929
- : null;
930
- // --- Main Header ---
931
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
932
- rows.push({
933
- rowIndex: currentRow++,
934
- cells: [{ column: 'A', value: 'i18n:scenario.assumptionsRefs', format: FormattingUtils.header(c.colors.topRow) }]
935
- });
936
- // --- Buildings References ---
937
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
938
- rows.push({
939
- rowIndex: currentRow++,
940
- cells: [{ column: 'A', value: 'i18n:scenario.modules.buildingsRefs', format: FormattingUtils.header(c.colors.buildingsSub) }]
941
- });
942
- // Sector-level description & references (e.g. home size source)
943
- const homesSectorRef = neighbourhood?.sectorReferences?.homes;
944
- if (homesSectorRef?.description) {
945
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: homesSectorRef.description }] });
946
- }
947
- if (homesSectorRef?.references?.length) {
948
- appendRefs(homesSectorRef.references);
949
- }
950
- // Building-code-specific references from codes actually used in the scenario
951
- const codesUsed = buildingsConfig.getAllBuildingCodesForScenario(scenario);
952
- codesUsed.forEach((code) => {
953
- if (!code)
954
- return;
955
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: `i18n:appConfig.buildings.buildingEnergyCodes.${code.id}`, format: { font: { bold: true } } }] });
956
- if (code.description) {
957
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: code.description }] });
958
- }
959
- if (code.references?.length) {
960
- appendRefs(code.references);
961
- }
962
- });
963
- currentRow++; // Spacer
964
- // --- Services References ---
965
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
966
- rows.push({
967
- rowIndex: currentRow++,
968
- cells: [{ column: 'A', value: 'i18n:scenario.modules.servicesRefs', format: FormattingUtils.header(c.colors.servicesSub) }]
969
- });
970
- const servicesSectorRef = neighbourhood?.sectorReferences?.services;
971
- if (servicesSectorRef?.references?.length) {
972
- appendRefs(servicesSectorRef.references);
973
- }
974
- currentRow++; // Spacer
975
- // --- Infrastructure References ---
976
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
977
- rows.push({
978
- rowIndex: currentRow++,
979
- cells: [{ column: 'A', value: 'i18n:scenario.modules.infrastructureRefs', format: FormattingUtils.header(c.colors.infraSub) }]
980
- });
981
- const infraSectorRef = neighbourhood?.sectorReferences?.infrastructure;
982
- if (infraSectorRef?.references?.length) {
983
- appendRefs(infraSectorRef.references);
984
- }
985
- currentRow++; // Spacer
986
- // --- Transportation Assumptions & References ---
987
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
988
- rows.push({
989
- rowIndex: currentRow++,
990
- cells: [{ column: 'A', value: 'i18n:scenario.modules.transportationRefs', format: FormattingUtils.header(c.colors.transSub) }]
991
- });
992
- // Fuel Consumption breakdown — only sources actually used in the scenario
993
- const puv = transportationConfig.transportationTypes.puv;
994
- const transit = transportationConfig.transportationTypes.transit;
995
- const scenarioPuvSources = Object.keys(scenario.transportation?.puv?.energySources || {});
996
- const scenarioTransitSources = Object.keys(scenario.transportation?.transit?.energySources || {});
997
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'i18n:forms.fields.fuelConsumption', format: { font: { bold: true } } }] });
998
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'i18n:appConfig.transportation.transportationTypes.puv', format: { font: { bold: true } } }] });
999
- Object.entries(puv.fuelConsumption || {}).filter(([srcId]) => scenarioPuvSources.includes(srcId)).forEach(([srcId, val]) => {
1000
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: `i18n:appConfig.energySources.energySources.${srcId}` }, { column: 'B', value: `${val} MJ/100km`, format: { horizontalAlignment: 'Right' } }] });
1001
- });
1002
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'i18n:appConfig.transportation.transportationTypes.transit', format: { font: { bold: true } } }] });
1003
- Object.entries(transit.fuelConsumption || {}).filter(([srcId]) => scenarioTransitSources.includes(srcId)).forEach(([srcId, val]) => {
1004
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: `i18n:appConfig.energySources.energySources.${srcId}` }, { column: 'B', value: `${val} MJ/100km`, format: { horizontalAlignment: 'Right' } }] });
1005
- });
1006
- // Transportation references from neighbourhood sectorReferences (falls back to global config)
1007
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'i18n:general.referencesTitle', format: { font: { bold: true } } }] });
1008
- const transSectorRef = neighbourhood?.sectorReferences?.transportation;
1009
- if (transSectorRef?.description) {
1010
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: transSectorRef.description }] });
1011
- }
1012
- const transRefs = (transSectorRef?.references?.length)
1013
- ? transSectorRef.references
1014
- : transportationConfig.transportationSectorReferences.references;
1015
- appendRefs(transRefs);
1016
- currentRow++; // Spacer
1017
- // --- Energy Source Emissions Factors ---
1018
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
1019
- rows.push({
1020
- rowIndex: currentRow++,
1021
- cells: [{ column: 'A', value: 'i18n:scenario.energyEFs', format: FormattingUtils.header(c.colors.topRow) }]
1022
- });
1023
- const evalYear = scenario.year || 2025;
1024
- // Use only the energy sources actually used in the scenario; fall back to a default list
1025
- const scenarioEnergySources = energySourcesConfig.getAllEnergySourcesForScenario(scenario);
1026
- const srcIds = Object.keys(scenarioEnergySources).length > 0
1027
- ? Object.keys(scenarioEnergySources)
1028
- : ['grid', 'natGas', 'heatingOil', 'gasoline', 'biodiesel', 'hydrogen'];
1029
- srcIds.forEach(srcId => {
1030
- const src = energySourcesConfig.energySources[srcId];
1031
- if (!src)
1032
- return;
1033
- const unit = (src.efUnit && src.efUnit !== 'MJ') ? 'g/kWh' : 'g/MJ';
1034
- const co2 = energySourcesConfig.getEFForSourceByIdGasYear(srcId, 'CO2', evalYear);
1035
- const ch4 = energySourcesConfig.getEFForSourceByIdGasYear(srcId, 'CH4', evalYear);
1036
- const n2o = energySourcesConfig.getEFForSourceByIdGasYear(srcId, 'N2O', evalYear);
1037
- const total = co2 + ch4 + n2o;
1038
- rows.push({
1039
- rowIndex: currentRow++,
1040
- cells: [
1041
- { column: 'A', value: `i18n:appConfig.energySources.energySources.${srcId}`, format: { font: { bold: true } } },
1042
- { column: 'B', value: `${total.toFixed(4)} ${unit}`, format: { horizontalAlignment: 'Right' } }
1043
- ]
1044
- });
1045
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'CO2' }, { column: 'B', value: `${co2} ${unit}`, format: { horizontalAlignment: 'Right' } }] });
1046
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'CH4' }, { column: 'B', value: `${ch4} ${unit}`, format: { horizontalAlignment: 'Right' } }] });
1047
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'N2O' }, { column: 'B', value: `${n2o} ${unit}`, format: { horizontalAlignment: 'Right' } }] });
1048
- // References per energy source (includes provincial overrides from regions.ts)
1049
- if (src.references?.length) {
1050
- appendRefs(src.references);
1051
- }
1052
- });
1053
- currentRow++; // Spacer
1054
- // --- GWP ---
1055
- mergedRanges.push(`A${currentRow}:E${currentRow}`);
1056
- rows.push({
1057
- rowIndex: currentRow++,
1058
- cells: [{ column: 'A', value: 'GWP', format: FormattingUtils.header(c.colors.topRow) }]
1059
- });
1060
- if (energySourcesConfig.gwp.references?.length) {
1061
- appendRefs(energySourcesConfig.gwp.references);
1062
- }
1063
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'CO2' }, { column: 'B', value: 1, format: { horizontalAlignment: 'Right' } }] });
1064
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'CH4' }, { column: 'B', value: energySourcesConfig.gwp.values.CH4 || 25, format: { horizontalAlignment: 'Right' } }] });
1065
- rows.push({ rowIndex: currentRow++, cells: [{ column: 'A', value: 'N2O' }, { column: 'B', value: energySourcesConfig.gwp.values.N2O || 298, format: { horizontalAlignment: 'Right' } }] });
1066
- }
1067
- }