FIX_BDS_EXCEL_EXPORT

This commit is contained in:
VENKATESHWARAN 2025-09-06 15:42:56 +05:30
parent 99a48f3130
commit 876ef10958

View File

@ -176,40 +176,166 @@ $(document).ready(function() {
dom: "<'row'<'col-sm-6'f><'col-sm-6 text-right'B>>" + // Filter left, buttons right
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>", // Pagination at the bottom, without page length selector
// buttons: [
// {
// extend: 'csv',
// text: 'CSV',
// title: 'Policy-Tranction-BDS-List',
// },
// {
// extend: 'excel',
// text: 'Excel',
// title: 'Policy-Tranction-BDS-List',
// customize: function(xlsx) {
// var sheet = xlsx.xl.worksheets['sheet1.xml'];
// var total = 0;
// // 🔹 Force column K (11) and L (12) as string
// $('row c[r^="K"], row c[r^="L"]', sheet).attr('t', 'inlineStr');
// // Find cells in column AB (28th column in Excel)
// $('row c[r^="AB"]', sheet).each(function() {
// var value = parseFloat($('v', this).text()); // Get the value inside the cell
// if (!isNaN(value)) {
// total += value;
// }
// });
// // Get the last row index and append the total row
// var lastRow = $('row:last', sheet);
// var rowIndex = parseInt(lastRow.attr('r')) + 2; // Find the next row index
// var totalRow = '<row r="' + rowIndex + '">' +
// '<c t="inlineStr" r="AA' + rowIndex + '"><is><t>Total</t></is></c>' + // Insert "Total" in AA
// '<c t="n" r="AB' + rowIndex + '"><v>' + total.toFixed(2) + '</v></c>' + // Insert sum in AB
// '</row>';
// // Append the new total row to the sheet
// $(sheet).find('sheetData').append(totalRow);
// },
// exportOptions: {
// orthogonal: 'sort'
// },
// customizeData: function (data) {
// for (var i = 0; i < data.body.length; i++) {
// for (var j = 0; j < data.body[i].length; j++) {
// // Check if the column is the 9th index (10th column) and 10th index (11th column)
// if (j === 9 || j === 10) {
// data.body[i][j] = '\u200C' + data.body[i][j];
// }
// }
// }
// }
// }
// ],
buttons: [
{
extend: 'csv',
text: 'CSV',
title: 'Policy-Tranction-BDS-List',
customizeData: function (data) {
for (var i = 0; i < data.body.length; i++) {
for (var j = 0; j < data.body[i].length; j++) {
// force text format by prefixing a tab
data.body[i][j] = '\t' + data.body[i][j];
}
}
}
},
{
extend: 'excel',
text: 'Excel',
title: 'Policy-Tranction-BDS-List',
customize: function(xlsx) {
// customize: function (xlsx) {
// var sheet = xlsx.xl.worksheets['sheet1.xml'];
// var total = 0;
// // 🔹 Force column K (11) and L (12) as string
// // $('row c[r^="K"], row c[r^="L"]', sheet).attr('t', 'inlineStr');
// // 🔹 Convert column K (11th) & L (12th) to inline string
// $('row c[r^="K"], row c[r^="L"]', sheet).each(function () {
// var cell = $(this);
// console.log(cell);
// var val = cell.find('v').text(); // get value
// if (val) {
// cell.attr('t', 'inlineStr'); // mark as text
// cell.remove('v'); // remove <v>
// cell.append('<is><t>' + val + '</t></is>'); // replace with inline string
// }
// });
// // Calculate total in AB (28th column)
// $('row c[r^="AB"]', sheet).each(function () {
// var value = parseFloat($('v', this).text());
// if (!isNaN(value)) {
// total += value;
// }
// });
// // Append total row
// var lastRow = $('row:last', sheet);
// var rowIndex = parseInt(lastRow.attr('r')) + 2;
// var totalRow = '<row r="' + rowIndex + '">' +
// '<c t="inlineStr" r="AA' + rowIndex + '"><is><t>Total</t></is></c>' +
// '<c t="n" r="AB' + rowIndex + '"><v>' + total.toFixed(2) + '</v></c>' +
// '</row>';
// $(sheet).find('sheetData').append(totalRow);
// },
customize: function (xlsx) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
var total = 0;
// 🔹 Force column K (11) and L (12) as string
$('row c[r^="K"], row c[r^="L"]', sheet).attr('t', 'inlineStr');
function forceAsText(cell) {
var val = cell.find('v').text();
if (val) {
// Remove old <v>
cell.find('v').remove();
// Find cells in column AB (28th column in Excel)
$('row c[r^="AB"]', sheet).each(function() {
var value = parseFloat($('v', this).text()); // Get the value inside the cell
if (!isNaN(value)) {
total += value;
// Force as inline string
cell.attr('t', 'inlineStr');
cell.append('<is><t>' + val + '</t></is>');
}
}
function forceAsTextPreserveOriginal(cell) {
// Get the original cell value from the cell's text content
var vElement = cell.find('v');
if (vElement.length > 0) {
var originalValue = vElement.text();
// Remove the <v> element
vElement.remove();
// Set cell type to inline string and preserve the original value
cell.attr('t', 'inlineStr');
cell.append('<is><t>' + originalValue + '</t></is>');
}
}
// 🔹 Force K column as text (working correctly)
$('row c[r^="K"]', sheet).each(function () {
forceAsText($(this));
});
// Get the last row index and append the total row
// 🔹 Force L column as text with better preservation
$('row c[r^="L"]', sheet).each(function () {
forceAsTextPreserveOriginal($(this));
});
// 🔹 Calculate total in AB (28th column)
$('row c[r^="AB"] v', sheet).each(function () {
var value = parseFloat($(this).text());
if (!isNaN(value)) total += value;
});
// 🔹 Append total row
var lastRow = $('row:last', sheet);
var rowIndex = parseInt(lastRow.attr('r')) + 2; // Find the next row index
var rowIndex = parseInt(lastRow.attr('r')) + 2;
var totalRow = '<row r="' + rowIndex + '">' +
'<c t="inlineStr" r="AA' + rowIndex + '"><is><t>Total</t></is></c>' + // Insert "Total" in AA
'<c t="n" r="AB' + rowIndex + '"><v>' + total.toFixed(2) + '</v></c>' + // Insert sum in AB
'<c t="inlineStr" r="AA' + rowIndex + '"><is><t>Total</t></is></c>' +
'<c t="n" r="AB' + rowIndex + '"><v>' + total.toFixed(2) + '</v></c>' +
'</row>';
// Append the new total row to the sheet
$(sheet).find('sheetData').append(totalRow);
},
exportOptions: {
@ -218,8 +344,8 @@ $(document).ready(function() {
customizeData: function (data) {
for (var i = 0; i < data.body.length; i++) {
for (var j = 0; j < data.body[i].length; j++) {
// Check if the column is the 9th index (10th column) and 10th index (11th column)
if (j === 9 || j === 10) {
// Force 10th (index 9) & 11th (index 10) as text
if (j === 9 || j === 10 || j === 11 || j === 12 ) {
data.body[i][j] = '\u200C' + data.body[i][j];
}
}
@ -227,6 +353,7 @@ $(document).ready(function() {
}
}
],
language: {
search: "_INPUT_",
searchPlaceholder: "Search..."