Skip to content

Commit

Permalink
Feat : freezed target columns
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-egov committed Jun 11, 2024
1 parent dcde91f commit e914c07
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
}
}
Expand Down
81 changes: 28 additions & 53 deletions utilities/project-factory/src/server/utils/excelUtils.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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', {
Expand Down

0 comments on commit e914c07

Please sign in to comment.