import { JnpfPrintDirectionEnum, JnpfPrintPaperSizeForType } from './define';
|
|
/**
|
* 检查值是否是 null 或 undefined
|
* @param value 要检查的值
|
* @returns 如果值是 null 或 undefined,返回 true,否则返回 false
|
*/
|
export const isNullOrUndefined = (value: any) => {
|
return value === null || value === undefined;
|
};
|
|
/**
|
* 判断一个值是否为空对象
|
* @param obj - 待检查的值
|
* @returns 如果是空对象,返回 true;否则返回 false
|
*/
|
export const isEmptyObject = (obj: unknown): boolean => {
|
return obj !== null && typeof obj === 'object' && !Array.isArray(obj) && Object.keys(obj).length === 0;
|
};
|
|
/**
|
* 将指定值从数组中移除并将其添加到数组的末尾。
|
* @param array - 要操作的数组。
|
* @param value - 要移动到数组末尾的值。
|
* @returns - 操作后的数组(原数组被修改)。
|
*/
|
export const moveArrayValueToEnd = (array: any[], value: number | string) => {
|
const index = array?.indexOf(value); // 找到目标值的索引
|
|
if (index !== -1) {
|
// 如果值存在于数组中
|
const [item] = array.splice(index, 1); // 删除目标值
|
array.push(item); // 添加到数组末尾
|
}
|
|
return array;
|
};
|
|
/**
|
* 根据字母规则计算输入字符串的索引值。
|
* @param input {string} - 输入的字母字符串(如 "A", "B", "AA", "AB" 等)。
|
* @returns {number} - 对应的索引值。
|
*/
|
export const getIndexFromAlphabetRule = (input: string): null | number => {
|
if (isNullOrUndefined(input)) {
|
return null;
|
}
|
|
const base = 26; // 字母表的长度
|
const charCodeA = 'A'.charCodeAt(0);
|
|
let index = 0;
|
for (let i = 0; i < input.length; i++) {
|
index = index * base + (input.charCodeAt(i) - charCodeA + 1);
|
}
|
|
return index - 1; // 数组索引从 0 开始
|
};
|
|
/**
|
* 根据索引值获取字母字符串。
|
* @param index {number} - 输入的索引值(如 0, 1, 26, 27 等)。
|
* @returns {string} - 对应的字母字符串(如 "A", "B", "AA", "AB" 等)。
|
*/
|
export const getAlphabetFromIndexRule = (index: number): string => {
|
const base = 26; // 字母表的长度
|
const charCodeA = 'A'.charCodeAt(0);
|
|
let result = '';
|
index += 1; // 转为从 1 开始的规则
|
|
while (index > 0) {
|
const remainder = (index - 1) % base;
|
result = String.fromCharCode(charCodeA + remainder) + result;
|
index = Math.floor((index - 1) / base);
|
}
|
|
return result;
|
};
|
|
/**
|
* 根据类型和坐标获取箭头单元格数组
|
* @param parentCellType - 父单元格类型
|
* @param colName - 列名
|
* @param rowName - 行名
|
* @returns 单元格坐标数组
|
*/
|
// export const getSheetRelationCell = (parentCellType: string, colName?: string, rowName?: string): { row: number; col: number }[] => {
|
// if (parentCellType === 'none' || !colName || !rowName) return [];
|
//
|
// const col = getIndexFromAlphabetRule(colName);
|
// const row = Number(rowName) - 1;
|
//
|
// return [{ row, col }];
|
// };
|
|
/**
|
* 修正工作表中的单元格数据。
|
* 根据传入的单元格数据 (`cellData`) 和指定的行、列数量,判断是否需要修正数据。
|
* 如果所有单元格数据异常(如未定义、空字符串、或无效值),则修正为默认配置;
|
* 如果存在正常单元格,则返回原始数据。
|
*
|
*/
|
export const correctSheetCellData = (cellData: any = {}, rowCount: number, columnCount: number, isFloatDom: boolean = false) => {
|
// 单元格纠正配置
|
const correctCellDataConfig = {
|
t: 1,
|
v: ' ',
|
};
|
|
// 如果 cellData 是空对象,直接返回默认初始化的单元格数据
|
if (isEmptyObject(cellData)) {
|
return {
|
0: {
|
0: correctCellDataConfig,
|
},
|
};
|
}
|
|
let totalCells = 0;
|
let abnormalCells = 0;
|
// 统计单元格数据中正常与异常的数量
|
for (const rowKey in cellData) {
|
const rowValue = cellData[rowKey] ?? {};
|
for (const colKey in rowValue) {
|
const { s, t, v, p } = rowValue[colKey] ?? {};
|
|
// 判断异常单元格条件
|
if (isNullOrUndefined(s) && !p && (v === undefined || (v === '' && t === 1) || (v === 0 && (t === 2 || t === 3)))) {
|
abnormalCells++;
|
}
|
|
totalCells++;
|
}
|
}
|
|
// 如果存在正常单元格,直接返回原始数据
|
if (totalCells > abnormalCells) {
|
return cellData;
|
}
|
|
// 否则需要修正数据
|
for (let i = 0; i < rowCount; i++) {
|
for (let j = 0; j < columnCount; j++) {
|
const { custom, t, v } = cellData?.[i]?.[j] ?? {};
|
|
// 判断是否需要修正(根据 isFloatDom 区分)
|
const needsCorrection = isFloatDom
|
? custom === undefined && (v === undefined || (v === '' && t === 1))
|
: custom === undefined && (v === undefined || (v === '' && t === 1) || (v === 0 && (t === 2 || t === 3)));
|
|
// 如果发现第一个需要修正的单元格,修正并结束所有循环
|
if (needsCorrection) {
|
if (!cellData[i]) {
|
cellData[i] = {};
|
}
|
cellData[i][j] = correctCellDataConfig;
|
|
return cellData; // 修正完成后直接返回
|
}
|
}
|
}
|
|
// 排查不出来问题,只能返回了
|
return cellData;
|
};
|
|
/**
|
* 将 Base64 编码的字符串转换为 File 对象
|
* @param base64String Base64 字符串(必须以 `data:` 开头)
|
* @param fileName 生成的文件名
|
* @param mimeType 文件 MIME 类型(如 "image/png", "application/pdf")
|
* @returns 返回一个 Promise,解析后得到 File 对象
|
*/
|
export function base64ToFile(base64String: string, fileName: string, mimeType: string): Promise<File> {
|
return fetch(base64String)
|
.then(res => res.blob()) // 将 Base64 转换为 Blob
|
.then(blob => new File([blob], fileName, { type: mimeType })); // 生成 File 对象
|
}
|
|
/**
|
* 计算旋转后的边界框尺寸
|
* @param {number} width 原始宽度
|
* @param {number} height 原始高度
|
* @param {number} angleDegrees 旋转角度(0-360,单位:度)
|
* @returns {{ rotatedWidth: number; rotatedHeight: number }} 旋转后的宽度和高度
|
*/
|
export function rotatedBoundingBox(width: number, height: number, angleDegrees: number): { rotatedHeight: number; rotatedWidth: number } {
|
const angle = (angleDegrees * Math.PI) / 180; // 角度转换为弧度
|
const rotatedWidth = Math.abs(width * Math.cos(angle)) + Math.abs(height * Math.sin(angle));
|
const rotatedHeight = Math.abs(width * Math.sin(angle)) + Math.abs(height * Math.cos(angle));
|
return { rotatedHeight, rotatedWidth };
|
}
|
|
/**
|
* 计算插入行和列后父格的位置
|
* @param range - 选中的区域,包含起始行、列(`startRow`、`startColumn`)以及结束行、列(`endRow`、`endColumn`)
|
* @param axis - 操作方向,`'row'` 表示插入行,`'col'` 表示插入列
|
* @param offset - 插入的行数或列数,决定偏移量
|
* @param custom - 当前单元格的数据,包含自定义父格信息(如 `topParentCellType` 和 `leftParentCellType`)
|
*/
|
export function getParentCellPosWhenInsert(range: any, axis: 'col' | 'row', offset: number, custom: any) {
|
const { startColumn = 0, startRow = 0 } = range ?? {};
|
|
let {
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
leftParentCellType,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
topParentCellType,
|
} = custom ?? {};
|
|
// 上父格自定义
|
if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
|
// 行操作
|
if (axis === 'row') {
|
const targetTopColIndex = Number(topParentCellCustomColName) - 1;
|
if (startRow <= targetTopColIndex) {
|
topParentCellCustomColName = (targetTopColIndex + offset + 1).toString();
|
}
|
}
|
|
// 列操作
|
if (axis === 'col') {
|
const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
|
if (targetTopRowIndex !== null && startColumn <= targetTopRowIndex) {
|
topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex + offset);
|
}
|
}
|
}
|
|
// 左父格自定义
|
if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
|
// 行操作
|
if (axis === 'row') {
|
const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
|
if (startRow <= targetLeftColIndex) {
|
leftParentCellCustomColName = (targetLeftColIndex + offset + 1).toString();
|
}
|
}
|
|
// 列操作
|
if (axis === 'col') {
|
const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
|
if (targetLeftRowIndex !== null && startColumn <= targetLeftRowIndex) {
|
leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex + offset);
|
}
|
}
|
}
|
|
return {
|
...custom,
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
};
|
}
|
|
/**
|
* 计算删除行和列后父格的位置
|
* @param range - 选中的区域,包含起始行、列(`startRow`、`startColumn`)以及结束行、列(`endRow`、`endColumn`)
|
* @param axis - 操作方向,`'row'` 表示插入行,`'col'` 表示插入列
|
* @param offset - 插入的行数或列数,决定偏移量
|
* @param custom - 当前单元格的数据,包含自定义父格信息(如 `topParentCellType` 和 `leftParentCellType`)
|
*/
|
export function getParentCellPosWhenDelete(range: any, axis: 'col' | 'row', offset: number, custom: any) {
|
const { endColumn = 0, endRow = 0, startColumn = 0, startRow = 0 } = range ?? {};
|
|
let {
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
leftParentCellType,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
topParentCellType,
|
} = custom ?? {};
|
|
// 上父格自定义
|
if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
|
// 行操作
|
if (axis === 'row') {
|
const targetTopColIndex = Number(topParentCellCustomColName) - 1;
|
|
if (startRow <= targetTopColIndex && endRow >= targetTopColIndex) {
|
topParentCellCustomColName = null;
|
topParentCellCustomRowName = null;
|
} else if (startRow <= targetTopColIndex) {
|
topParentCellCustomColName = (targetTopColIndex - offset + 1).toString();
|
}
|
}
|
|
// 列操作
|
if (axis === 'col') {
|
const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
|
if (targetTopRowIndex !== null) {
|
if (startColumn <= targetTopRowIndex && endColumn >= targetTopRowIndex) {
|
topParentCellCustomColName = null;
|
topParentCellCustomRowName = null;
|
} else if (startColumn <= targetTopRowIndex) {
|
topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex - offset);
|
}
|
}
|
}
|
}
|
|
// 左父格自定义
|
if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
|
// 行操作
|
if (axis === 'row') {
|
const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
|
|
if (startRow <= targetLeftColIndex && endRow >= targetLeftColIndex) {
|
leftParentCellCustomColName = null;
|
leftParentCellCustomRowName = null;
|
} else if (startRow <= targetLeftColIndex) {
|
leftParentCellCustomColName = (targetLeftColIndex - offset + 1).toString();
|
}
|
}
|
|
// 列操作
|
if (axis === 'col') {
|
const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
|
if (targetLeftRowIndex !== null) {
|
if (startColumn <= targetLeftRowIndex && endColumn >= targetLeftRowIndex) {
|
leftParentCellCustomColName = null;
|
leftParentCellCustomRowName = null;
|
} else if (startColumn <= targetLeftRowIndex) {
|
leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex - offset);
|
}
|
}
|
}
|
}
|
|
return {
|
...custom,
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
};
|
}
|
|
/**
|
* 计算移动行和列后父格的位置
|
* @param sourceRange - 原选中的区域,包含起始行、列(`startRow`、`startColumn`)以及结束行、列(`endRow`、`endColumn`)
|
* @param targetRange - 后影响的区域,包含起始行、列(`startRow`、`startColumn`)以及结束行、列(`endRow`、`endColumn`)
|
* @param axis - 操作方向,`'row'` 表示插入行,`'col'` 表示插入列
|
* @param offset - 插入的行数或列数,决定偏移量
|
* @param involved - 涉及的行或列的数量
|
* @param custom - 当前单元格的数据,包含自定义父格信息(如 `topParentCellType` 和 `leftParentCellType`)
|
*/
|
export function getParentCellPosWhenMove(sourceRange: any, targetRange: any, axis: 'col' | 'row', offset: number, involved: number, custom: any) {
|
let {
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
leftParentCellType,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
topParentCellType,
|
} = custom ?? {};
|
|
// 上父格自定义
|
if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
|
// 行操作
|
if (axis === 'row') {
|
const targetTopColIndex = Number(topParentCellCustomColName) - 1;
|
|
if (sourceRange.startRow <= targetTopColIndex && targetTopColIndex <= sourceRange.endRow) {
|
topParentCellCustomColName = (targetTopColIndex + offset + 1).toString();
|
} else if (
|
(sourceRange.startRow < targetTopColIndex && targetTopColIndex < targetRange.startRow) ||
|
(sourceRange.startRow > targetTopColIndex && targetTopColIndex >= targetRange.startRow)
|
) {
|
const moveValue = offset > 0 ? involved : -involved;
|
topParentCellCustomColName = (targetTopColIndex - moveValue + 1).toString();
|
}
|
}
|
|
// 列操作
|
if (axis === 'col') {
|
const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
|
if (targetTopRowIndex !== null) {
|
if (sourceRange.startColumn <= targetTopRowIndex && targetTopRowIndex <= sourceRange.endColumn) {
|
topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex + offset);
|
} else if (
|
(sourceRange.startColumn < targetTopRowIndex && targetTopRowIndex < targetRange.startColumn) ||
|
(sourceRange.startColumn > targetTopRowIndex && targetTopRowIndex >= targetRange.startColumn)
|
) {
|
const moveValue = offset > 0 ? involved : -involved;
|
topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex - moveValue);
|
}
|
}
|
}
|
}
|
|
// 左父格自定义
|
if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
|
// 行操作
|
if (axis === 'row') {
|
const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
|
|
if (sourceRange.startRow <= targetLeftColIndex && targetLeftColIndex <= sourceRange.endRow) {
|
leftParentCellCustomColName = (targetLeftColIndex + offset + 1).toString();
|
} else if (
|
(sourceRange.startRow < targetLeftColIndex && targetLeftColIndex < targetRange.startRow) ||
|
(sourceRange.startRow > targetLeftColIndex && targetLeftColIndex >= targetRange.startRow)
|
) {
|
const moveValue = offset > 0 ? involved : -involved;
|
leftParentCellCustomColName = (targetLeftColIndex - moveValue + 1).toString();
|
}
|
}
|
|
// 列操作
|
if (axis === 'col') {
|
const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
|
if (targetLeftRowIndex !== null) {
|
if (sourceRange.startColumn <= targetLeftRowIndex && targetLeftRowIndex <= sourceRange.endColumn) {
|
leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex + offset);
|
} else if (
|
(sourceRange.startColumn < targetLeftRowIndex && targetLeftRowIndex < targetRange.startColumn) ||
|
(sourceRange.startColumn > targetLeftRowIndex && targetLeftRowIndex >= targetRange.startColumn)
|
) {
|
const moveValue = offset > 0 ? involved : -involved;
|
leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex - moveValue);
|
}
|
}
|
}
|
}
|
|
return {
|
...custom,
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
};
|
}
|
|
/**
|
* 计算移动单元格后父格的位置
|
* @param {object} fromRange - 被移动的单元格的范围,包含起始和结束行列坐标
|
* @param {object} offset - 行列偏移量,包含纵向(offsetRow)和横向(offsetCol)的偏移
|
* @param {object} custom - 当前单元格的自定义信息,包含上父格和左父格的信息
|
*/
|
export function getParentCellPosWhenMoveCell(fromRange: any, offset: any, custom: any) {
|
const { offsetCol, offsetRow } = offset ?? {};
|
let {
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
leftParentCellType,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
topParentCellType,
|
} = custom ?? {};
|
|
// 上父格自定义
|
if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
|
const targetTopColIndex = Number(topParentCellCustomColName) - 1;
|
const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
|
|
if (targetTopRowIndex !== null) {
|
const isLinchpin =
|
fromRange.startRow <= targetTopColIndex &&
|
targetTopColIndex <= fromRange.endRow &&
|
fromRange.startColumn <= targetTopRowIndex &&
|
targetTopRowIndex <= fromRange.endColumn; // 当事单元格
|
|
if (
|
offsetRow !== 0 && // 纵向操作
|
isLinchpin
|
) {
|
topParentCellCustomColName = (targetTopColIndex + offsetRow + 1).toString();
|
}
|
|
if (
|
offsetCol !== 0 && // 横向操作
|
isLinchpin
|
) {
|
topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex + offsetCol);
|
}
|
}
|
}
|
|
// 左父格自定义
|
if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
|
const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
|
const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
|
|
if (targetLeftRowIndex !== null) {
|
const isLinchpin =
|
fromRange.startRow <= targetLeftColIndex &&
|
targetLeftColIndex <= fromRange.endRow &&
|
fromRange.startColumn <= targetLeftRowIndex &&
|
targetLeftRowIndex <= fromRange.endColumn; // 当事单元格
|
|
if (
|
offsetRow !== 0 && // 纵向操作
|
isLinchpin
|
) {
|
leftParentCellCustomColName = (targetLeftColIndex + offsetRow + 1).toString();
|
}
|
|
if (
|
offsetCol !== 0 && // 横向操作
|
isLinchpin
|
) {
|
leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex + offsetCol);
|
}
|
}
|
}
|
|
return {
|
...custom,
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
};
|
}
|
|
/**
|
* 计算清楚单元格后父格的位置
|
* @param ranges - 当前选择的单元格范围列表,每个范围包含起始和结束行列
|
* @param custom - 父格的自定义信息,包括行列名称和类型
|
*/
|
export function getParentCellPosWhenClearCell(ranges: any, custom: any) {
|
let {
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
leftParentCellType,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
topParentCellType,
|
} = custom ?? {};
|
|
// 上父格自定义
|
if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
|
const targetTopColIndex = Number(topParentCellCustomColName) - 1;
|
const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
|
|
if (targetTopRowIndex !== null) {
|
const isLinchpin = ranges.some(({ endColumn, endRow, startColumn, startRow }: any) => {
|
return startRow <= targetTopColIndex && targetTopColIndex <= endRow && startColumn <= targetTopRowIndex && targetTopRowIndex <= endColumn;
|
});
|
|
if (isLinchpin) {
|
topParentCellCustomColName = null;
|
topParentCellCustomRowName = null;
|
}
|
}
|
}
|
|
// 左父格自定义
|
if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
|
const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
|
const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
|
|
if (targetLeftRowIndex !== null) {
|
const isLinchpin = ranges.some(({ endColumn, endRow, startColumn, startRow }: any) => {
|
return startRow <= targetLeftColIndex && targetLeftColIndex <= endRow && startColumn <= targetLeftRowIndex && targetLeftRowIndex <= endColumn;
|
});
|
|
if (isLinchpin) {
|
leftParentCellCustomColName = null;
|
leftParentCellCustomRowName = null;
|
}
|
}
|
}
|
|
return {
|
...custom,
|
leftParentCellCustomColName,
|
leftParentCellCustomRowName,
|
topParentCellCustomColName,
|
topParentCellCustomRowName,
|
};
|
}
|
|
/**
|
* 翻译小时
|
* @param hour
|
*/
|
function to12HourFormat(hour: any) {
|
const h = hour % 12 || 12; // 0 => 12, 13 => 1, 14 => 2 ...
|
return h.toString().padStart(2, '0'); // 补零,例如 2 => '02'
|
}
|
|
/**
|
* 翻译页眉页脚文本
|
*/
|
/**
|
* 翻译页眉页脚文本
|
*/
|
export function translateHeaderFooterText(orientation: string, printConfig: any): string {
|
if (!printConfig?.customHeaderFooterValue?.[orientation]) return '';
|
|
let targetValue = printConfig?.customHeaderFooterValue[orientation];
|
|
// 日期和时间的预计算,减少 slice 调用
|
const dateTime = printConfig?.currentDateTime || '';
|
const dateA = dateTime.slice(0, 10); // YYYY-MM-DD
|
const dateB = dateTime.slice(5, 10); // YYYY-MM
|
const dateC = `${dateTime.slice(5, 7)}/${dateTime.slice(8, 10)}/${dateTime.slice(0, 4)}`; // MM/DD/YYYY
|
const dateD = `${dateTime.slice(5, 7)}/${dateTime.slice(8, 10)}`; // MM/DD
|
const timeA = dateTime.slice(11, 19); // HH:mm:ss
|
const timeB = dateTime.slice(11, 16); // HH:mm
|
|
const hourFormat = to12HourFormat(dateTime.slice(11, 13));
|
const hour = Number(dateTime.slice(11, 13));
|
|
const timeC = `${hour < 13 ? 'AM' : 'PM'} ${hourFormat}:${dateTime.slice(14, 19)}`; // HH:mm:ss
|
const timeD = `${hour < 13 ? 'AM' : 'PM'} ${hourFormat}:${dateTime.slice(14, 16)}`; // HH:mm:ss
|
|
// 依次替换占位符,保持原有业务逻辑
|
targetValue = targetValue
|
.replace('@ReportName', printConfig.workbookTitleText)
|
.replace('@SheetName', printConfig.workSheetTitleText)
|
.replace('@TotalPages', printConfig.bookTotalPage)
|
.replace('@PageNumbers', printConfig.bookPageNumber)
|
.replace('@SheetTotalPages', printConfig.sheetTotalPage)
|
.replace('@SheetPageNumbers', printConfig.sheetPageNumber)
|
.replace('@DateA', dateA)
|
.replace('@DateB', dateB)
|
.replace('@DateC', dateC)
|
.replace('@DateD', dateD)
|
.replace('@TimeA', timeA)
|
.replace('@TimeB', timeB)
|
.replace('@TimeC', timeC)
|
.replace('@TimeD', timeD);
|
|
return targetValue;
|
}
|
|
/**
|
* 获取打印页面的样式
|
* @param paperType - 纸张类型,对应 `JnpfPrintPaperSizeForType` 中的键值
|
* @param direction - 打印方向,取值为 `JnpfPrintDirectionEnum.portrait`(纵向)或 `JnpfPrintDirectionEnum.landscape`(横向)
|
* @returns 一个包含打印样式的 `<style>` 元素
|
*/
|
export function getPrintPageStyle(paperType: string, direction: string) {
|
const { h, w } = JnpfPrintPaperSizeForType[paperType as keyof typeof JnpfPrintPaperSizeForType] ?? {};
|
const width = direction === JnpfPrintDirectionEnum?.portrait ? w : h;
|
const height = direction === JnpfPrintDirectionEnum?.portrait ? h : w;
|
const style = `
|
@page {
|
size: ${width}px ${height}px;
|
}
|
@page {
|
margin: 0;
|
visibility: hidden;
|
}
|
@media print {
|
body > * {
|
display: none!important;
|
}
|
#jnpfReportPrint, #jnpfReportPrint * {
|
display: block!important;
|
height: fit-content;
|
overflow: visible;
|
top: 0;
|
width: fit-content;
|
}
|
#jnpfReportPrint .printContainer {
|
page-break-after: always!important;
|
height: ${height}px;
|
width: ${width}px;
|
position: relative;
|
}
|
}`;
|
const $style = document.createElement('style');
|
$style.innerHTML = style;
|
$style.className = 'jnpfPrintCss';
|
return $style;
|
}
|