92 lines
3.2 KiB
TypeScript
92 lines
3.2 KiB
TypeScript
import axios from "axios";
|
||
import * as XLSX from "xlsx";
|
||
import { saveAs } from "file-saver";
|
||
export function handleAxiosError(error: unknown) {
|
||
if (axios.isAxiosError(error)) {
|
||
// اینجا میدونیم که خطا از axios است
|
||
return error.response?.data?.error?.message;
|
||
} else {
|
||
return "Unexpected error";
|
||
}
|
||
}
|
||
|
||
export const handleExport = (data: any, type: any) => {
|
||
// ۱. تبدیل دیتا به یک Worksheet
|
||
const worksheet = XLSX.utils.json_to_sheet(data);
|
||
|
||
// ۲. ایجاد یک Workbook جدید
|
||
const workbook = XLSX.utils.book_new();
|
||
XLSX.utils.book_append_sheet(workbook, worksheet, "Data");
|
||
|
||
if (type === "excel") {
|
||
// خروجی اکسل
|
||
const excelBuffer = XLSX.write(workbook, {
|
||
bookType: "xlsx",
|
||
type: "array",
|
||
});
|
||
const blob = new Blob([excelBuffer], {
|
||
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||
});
|
||
saveAs(blob, "data.xlsx");
|
||
} else if (type === "spss") {
|
||
// برای SPSS، بهترین فرمت CSV است که در SPSS به خوبی باز میشود
|
||
const csvData = XLSX.utils.sheet_to_csv(worksheet);
|
||
const blob = new Blob([csvData], { type: "text/csv;charset=utf-8;" });
|
||
saveAs(blob, "data.csv"); // فایل CSV در SPSS به راحتی Import میشود
|
||
}
|
||
};
|
||
|
||
export const exportToExcel = (
|
||
type: "spss" | "excel",
|
||
formattedData: any,
|
||
filename: string = "Report",
|
||
) => {
|
||
// تبدیل دادهها به فرمت قابل فهم برای XLSX
|
||
|
||
let dataToProcess = Array.isArray(formattedData)
|
||
? formattedData
|
||
: [formattedData];
|
||
const worksheet = XLSX.utils.json_to_sheet(formattedData);
|
||
|
||
const workbook = XLSX.utils.book_new();
|
||
XLSX.utils.book_append_sheet(workbook, worksheet, filename);
|
||
|
||
if (type === "excel") {
|
||
// خروجی اکسل
|
||
const excelBuffer = XLSX.write(workbook, {
|
||
bookType: "xlsx",
|
||
type: "array",
|
||
});
|
||
const blob = new Blob([excelBuffer], {
|
||
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||
});
|
||
saveAs(blob, `${filename}.xlsx`);
|
||
} else if (type === "spss") {
|
||
// برای SPSS، بهترین فرمت CSV است که در SPSS به خوبی باز میشود
|
||
const csvData = XLSX.utils.sheet_to_csv(worksheet);
|
||
const blob = new Blob([csvData], { type: "text/csv;charset=utf-8;" });
|
||
saveAs(blob, `${filename}.csv`); // فایل CSV در SPSS به راحتی Import میشود
|
||
}
|
||
};
|
||
export function formatDurationPersian(seconds:string) {
|
||
const sec = Math.abs(parseFloat(seconds));
|
||
|
||
if (sec < 1) return "بلافاصله"; // برای مقادیر بسیار ناچیز
|
||
if (sec < 60) return `${Math.floor(sec)} ثانیه`;
|
||
|
||
const minutes = Math.floor(sec / 60);
|
||
const remainingSeconds = Math.floor(sec % 60);
|
||
|
||
if (minutes < 60) {
|
||
return `${minutes} دقیقه و ${remainingSeconds} ثانیه`;
|
||
}
|
||
|
||
const hours = Math.floor(minutes / 60);
|
||
const remainingMinutes = minutes % 60;
|
||
return `${hours} ساعت و ${remainingMinutes} دقیقه`;
|
||
}
|
||
|
||
// مثال برای مقدار شما:
|
||
console.log(formatDurationPersian("-0.00100000000000000000"));
|
||
// خروجی: "بلافاصله" (یا ۰ ثانیه)
|