From e914c07fad239d0f1654a5b9e27e1cdc510ead26 Mon Sep 17 00:00:00 2001 From: ashish-egov Date: Tue, 11 Jun 2024 12:36:19 +0530 Subject: [PATCH] Feat : freezed target columns --- .../src/server/utils/campaignUtils.ts | 2 +- .../src/server/utils/excelUtils.ts | 81 +++++++------------ 2 files changed, 29 insertions(+), 54 deletions(-) diff --git a/utilities/project-factory/src/server/utils/campaignUtils.ts b/utilities/project-factory/src/server/utils/campaignUtils.ts index c86dbb3c586..5f158560cc5 100644 --- a/utilities/project-factory/src/server/utils/campaignUtils.ts +++ b/utilities/project-factory/src/server/utils/campaignUtils.ts @@ -1367,7 +1367,7 @@ async function appendDistricts(request: any, workbook: any, uniqueDistrictsForMa } newSheetData.push(rowData); } - createNewSheet(request, workbook, newSheetData, uniqueData, localizationMap, districtLevelRowBoundaryCodeMap, configurableColumnHeadersFromSchemaForTargetSheet); + await createNewSheet(request, workbook, newSheetData, uniqueData, localizationMap, districtLevelRowBoundaryCodeMap, configurableColumnHeadersFromSchemaForTargetSheet); logger.info(`${uniqueDataFromLevelForDifferentTabs} - ${differentTabsBasedOnLevel} boundary data generation completed`) } } diff --git a/utilities/project-factory/src/server/utils/excelUtils.ts b/utilities/project-factory/src/server/utils/excelUtils.ts index 295a52b5137..b6277fa752a 100644 --- a/utilities/project-factory/src/server/utils/excelUtils.ts +++ b/utilities/project-factory/src/server/utils/excelUtils.ts @@ -1,5 +1,5 @@ import * as ExcelJS from "exceljs"; -import { throwError } from "./genericUtils"; +import { changeFirstRowColumnColour, throwError } from "./genericUtils"; import { httpRequest } from "./request"; import { logger } from "./logger"; import config from "../config"; @@ -160,34 +160,6 @@ function addDataToSheet(sheet: any, sheetData: any, firstRowColor: any = '93C47D } } -// function lockTargetFields(newSheet: any, targetColumnNumber: any, boundaryCodeColumnIndex: any) { -// // Make every cell locked by default -// newSheet.eachRow((row: any) => { -// row.eachCell((cell: any) => { -// cell.protection = { locked: true }; -// }); -// }); - -// // Unlock cells in the target column -// if (targetColumnNumber > -1) { -// newSheet.eachRow((row: any) => { -// const cell = row.getCell(targetColumnNumber); // Excel columns are 1-based -// cell.protection = { locked: false }; -// }); -// } - -// // Hide the boundary code column -// if (boundaryCodeColumnIndex !== -1) { -// newSheet.getColumn(boundaryCodeColumnIndex + 1).hidden = true; -// } - -// // Protect the sheet with a password (optional) -// newSheet.protect('passwordhere', { -// selectLockedCells: true, -// selectUnlockedCells: true -// }); -// } - function lockTargetFields(newSheet: any, columnsNotToBeFreezed: any, boundaryCodeColumnIndex: any) { // Make every cell locked by default @@ -198,30 +170,33 @@ function lockTargetFields(newSheet: any, columnsNotToBeFreezed: any, boundaryCod }); // // Get headers in the first row and filter out empty items - // const headers = newSheet.getRow(1).values.filter((header:any) => header); - // console.log(headers, "Filtered Headers in the first row"); - - // // Unlock cells in the target columns - // if (Array.isArray(columnsNotToBeFreezed) && columnsNotToBeFreezed.length > 0) { - // columnsNotToBeFreezed.forEach((header) => { - // const targetColumnNumber = headers.indexOf(header) + 1; // Excel columns are 1-based - // console.log(`Header: ${header}, Target Column Index: ${targetColumnNumber}`); - // if (targetColumnNumber > -1) { - // newSheet.eachRow((row:any) => { - // const cell = row.getCell(targetColumnNumber); - // changeFirstRowColumnColour(newSheet, 'B6D7A8', targetColumnNumber); - // cell.protection = { locked: false }; - // }); - // } else { - // console.error(`Header "${header}" not found in the first row`); - // } - // }); - // } - - // // Hide the boundary code column - // if (boundaryCodeColumnIndex !== -1) { - // newSheet.getColumn(boundaryCodeColumnIndex + 1).hidden = true; // Excel columns are 1-based - // } + const headers = newSheet.getRow(1).values.filter((header: any) => header); + console.log(headers, "Filtered Headers in the first row"); + + // Unlock cells in the target columns + if (Array.isArray(columnsNotToBeFreezed) && columnsNotToBeFreezed.length > 0) { + columnsNotToBeFreezed.forEach((header) => { + const targetColumnNumber = headers.indexOf(header) + 1; // Excel columns are 1-based + console.log(`Header: ${header}, Target Column Index: ${targetColumnNumber}`); + if (targetColumnNumber > -1) { + newSheet.eachRow((row: any, rowNumber: number) => { + changeFirstRowColumnColour(newSheet, 'B6D7A8', targetColumnNumber); + if (rowNumber === 1) return; + + const cell = row.getCell(targetColumnNumber); + cell.protection = { locked: false }; + }); + + } else { + console.error(`Header "${header}" not found in the first row`); + } + }); + } + + // Hide the boundary code column + if (boundaryCodeColumnIndex !== -1) { + newSheet.getColumn(boundaryCodeColumnIndex + 1).hidden = true; // Excel columns are 1-based + } // Protect the sheet with a password (optional) newSheet.protect('passwordhere', {