Skip to content

Commit 9065dc0

Browse files
author
m.r
committed
RelativeFormulaType
1 parent 1889d02 commit 9065dc0

File tree

6 files changed

+176
-45
lines changed

6 files changed

+176
-45
lines changed

src/Themes/theme.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
ExcelTable,
44
Header,
55
Sheet,
6-
Theme,
76
} from "../data-model/excel-table";
87
import { generateContrastTextColor, hexToRgbNegative } from "../utils/color";
98
import colorHunt from "./colorHunt.json";

src/data-model/excel-table.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ export interface DataOptions {
9696
multiStyleValue?: MapMultiStyleValue;
9797
comment?: MapComment;
9898
}
99+
export interface RowMap {
100+
[rowNumber: number]: {
101+
startTag: string;
102+
endTag: string;
103+
details: string;
104+
};
105+
}
99106
export type ProtectionOption = {
100107
[key in ProtectionOptionKey]: "0" | "1" | 0 | 1;
101108
};
@@ -205,7 +212,6 @@ export interface Comment {
205212
author?: string;
206213
}
207214

208-
209215
export interface MergeRowConditionMap {
210216
[columnKey: string]: {
211217
inProgress: boolean;
@@ -273,6 +279,7 @@ export interface Checkbox {
273279
endStr?: string;
274280
}
275281
export type FormulaType = "AVERAGE" | "SUM" | "COUNT" | "MAX" | "MIN";
282+
export type RelativeFormulaType = "LEN";
276283
export interface FormatMap {
277284
[format: string]: {
278285
key: number;
@@ -289,6 +296,11 @@ export interface FormulaSetting {
289296
end: string;
290297
styleId?: string;
291298
}
299+
export interface RelativeFormulaSetting {
300+
type: RelativeFormulaType;
301+
refrenceCell: string;
302+
styleId?: string;
303+
}
292304
export interface StyleMapper {
293305
conditinalFormating: {
294306
count: number;
@@ -323,4 +335,4 @@ export interface StyleMapper {
323335

324336
export interface MapComment {
325337
[key: string]: Comment | string;
326-
}
338+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ThemeOption, themeGenerator } from "./Themes/theme";
2-
import { Data, ExcelTable, SideBySide, Theme } from "./data-model/excel-table";
2+
import { Data, ExcelTable, SideBySide } from "./data-model/excel-table";
33
import {
44
ColWidthScaleFunction,
55
RowHeightScaleFunction,

src/utils/content-generator/content-types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export function contentTypeGenerator(
55
commentId: number[],
66
arrTypes: string[],
77
sheetDrawers: string[],
8-
checkboxForm: string[]
8+
checkboxForm: string[],
9+
needCalcChain:boolean
910
) {
1011
return (
1112
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' +
@@ -55,6 +56,9 @@ export function contentTypeGenerator(
5556
"<Override" +
5657
' ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"' +
5758
' PartName="/xl/sharedStrings.xml" />' +
59+
(needCalcChain
60+
? `<Override PartName="/xl/calcChain.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml"/>`
61+
: "") +
5862
' <Override PartName="/docProps/core.xml" ' +
5963
' ContentType="application/vnd.openxmlformats-package.core-properties+xml" />' +
6064
sheetDrawers.reduce((res, cu) => {

src/utils/generate-excel.ts

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Formula,
77
MergeRowConditionMap,
88
ProtectionOptionKey,
9+
RowMap,
910
StyleMapper,
1011
} from "../data-model/excel-table";
1112
import { generateColumnName } from "../utils/generate-column-name";
@@ -362,8 +363,12 @@ export async function generateExcel(data: ExcelTable) {
362363
};
363364

364365
let checkboxForm: string[] = [];
366+
let calcChainValue = "";
367+
let needCalcChain = false;
365368
for (let index = 0; index < sheetLength; index++) {
366369
const sheetData = data.sheet[index];
370+
const sheetDataId = index + 1;
371+
let rowMap: RowMap = {};
367372
let rowCount = sheetData.shiftTop ? sheetData.shiftTop : 1;
368373
let sheetDataString = "";
369374
let sheetSizeString = "";
@@ -667,6 +672,27 @@ export async function generateExcel(data: ExcelTable) {
667672
);
668673
}
669674
if (typeof title.text == "string") {
675+
rowMap[rowCount + top] = {
676+
startTag:
677+
'<row r="' +
678+
(rowCount + top) +
679+
'" ' +
680+
height +
681+
' spans="1:' +
682+
(left + consommeCol - 1) +
683+
'">',
684+
details:
685+
'<c r="' +
686+
refString +
687+
'" ' +
688+
(data.styles[tStyle]
689+
? ' s="' + data.styles[tStyle].index + '" '
690+
: "") +
691+
' t="s"><v>' +
692+
sharedStringIndex +
693+
"</v></c>",
694+
endTag: "</row>",
695+
};
670696
titleRow +=
671697
'<row r="' +
672698
(rowCount + top) +
@@ -811,11 +837,17 @@ export async function generateExcel(data: ExcelTable) {
811837
}
812838
const formula = formulaSheetObj && formulaSheetObj[refString];
813839
if (formula) {
814-
sheetDataString += generateCellRowCol(
840+
const f = generateCellRowCol(
815841
refString,
816842
formula,
843+
sheetDataId,
817844
data.styles
818-
).cell;
845+
);
846+
if (f.needCalcChain) {
847+
needCalcChain = true;
848+
calcChainValue += f.chainCell;
849+
}
850+
sheetDataString += f.cell;
819851
delete formulaSheetObj![refString];
820852
} else {
821853
sheetDataString +=
@@ -860,8 +892,7 @@ export async function generateExcel(data: ExcelTable) {
860892
}
861893
});
862894
if (!sheetData.withoutHeader) {
863-
sheetDataString =
864-
titleRow +
895+
const rowTag =
865896
'<row r="' +
866897
rowCount +
867898
'" spans="1:' +
@@ -882,9 +913,13 @@ export async function generateExcel(data: ExcelTable) {
882913
);
883914
}, " ")
884915
: "") +
885-
">" +
886-
sheetDataString +
887-
"</row>";
916+
">";
917+
rowMap[rowCount] = {
918+
startTag: rowTag,
919+
endTag: "</row>",
920+
details: sheetDataString,
921+
};
922+
sheetDataString = titleRow + rowTag + sheetDataString + "</row>";
888923
rowCount++;
889924
} else {
890925
sheetDataString += titleRow;
@@ -945,7 +980,7 @@ export async function generateExcel(data: ExcelTable) {
945980
}
946981
}
947982
const rowStyle = mData.rowStyle;
948-
sheetDataString +=
983+
const rowTagStart =
949984
'<row r="' +
950985
rowCount +
951986
'" spans="1:' +
@@ -959,6 +994,8 @@ export async function generateExcel(data: ExcelTable) {
959994
: "") +
960995
(keyHidden in mData ? ' hidden="' + mData[keyHidden] + '"' : "") +
961996
" >";
997+
sheetDataString += rowTagStart;
998+
let rowDataString = "";
962999
objKey.forEach((key, keyIndex) => {
9631000
if (shiftCount) {
9641001
keyIndex += shiftCount;
@@ -1077,11 +1114,17 @@ export async function generateExcel(data: ExcelTable) {
10771114
}
10781115
const formula = formulaSheetObj && formulaSheetObj[refString];
10791116
if (formula) {
1080-
sheetDataString += generateCellRowCol(refString, formula).cell;
1117+
const f = generateCellRowCol(refString, formula, sheetDataId);
1118+
if (f.needCalcChain) {
1119+
needCalcChain = true;
1120+
calcChainValue += f.chainCell;
1121+
}
1122+
sheetDataString += f.cell;
1123+
rowDataString += f.cell;
10811124
delete formulaSheetObj![refString];
10821125
} else {
10831126
if (typeof dataEl == "string") {
1084-
sheetDataString +=
1127+
const localcell =
10851128
'<c r="' +
10861129
cols[keyIndex] +
10871130
rowCount +
@@ -1092,6 +1135,8 @@ export async function generateExcel(data: ExcelTable) {
10921135
"><v>" +
10931136
sharedStringIndex +
10941137
"</v></c>";
1138+
rowDataString += localcell;
1139+
sheetDataString += localcell;
10951140
if (typeof sheetData.multiStyleConditin == "function") {
10961141
const multi = sheetData.multiStyleConditin(
10971142
dataEl,
@@ -1129,7 +1174,7 @@ export async function generateExcel(data: ExcelTable) {
11291174
sharedStringMap[dataEl] = dataEl;
11301175
sharedStringIndex++;
11311176
} else {
1132-
sheetDataString +=
1177+
const localcell =
11331178
'<c r="' +
11341179
cols[keyIndex] +
11351180
rowCount +
@@ -1140,6 +1185,8 @@ export async function generateExcel(data: ExcelTable) {
11401185
"><v>" +
11411186
dataEl +
11421187
"</v></c>";
1188+
sheetDataString += localcell;
1189+
rowDataString += localcell;
11431190
}
11441191
}
11451192
});
@@ -1152,7 +1199,11 @@ export async function generateExcel(data: ExcelTable) {
11521199
}
11531200
});
11541201
}
1155-
1202+
rowMap[rowCount] = {
1203+
startTag: rowTagStart,
1204+
endTag: "</row>",
1205+
details: rowDataString,
1206+
};
11561207
rowCount++;
11571208
sheetDataString += "</row>";
11581209
});
@@ -1208,7 +1259,16 @@ export async function generateExcel(data: ExcelTable) {
12081259
[row: number]: string;
12091260
} = {};
12101261
remindFormulaKey.forEach((v) => {
1211-
const f = generateCellRowCol(v, formulaSheetObj![v], data.styles);
1262+
const f = generateCellRowCol(
1263+
v,
1264+
formulaSheetObj![v],
1265+
sheetDataId,
1266+
data.styles
1267+
);
1268+
if (f.needCalcChain) {
1269+
needCalcChain = true;
1270+
calcChainValue += f.chainCell;
1271+
}
12121272
if (!rF[f.row]) {
12131273
rF[f.row] = f.cell;
12141274
} else {
@@ -1217,14 +1277,27 @@ export async function generateExcel(data: ExcelTable) {
12171277
});
12181278
Object.keys(rF).forEach((v) => {
12191279
const l = rF[v as keyof object];
1220-
sheetDataString +=
1221-
'<row r="' +
1222-
v +
1223-
'" spans="1:' +
1224-
colsLength +
1225-
'" >' +
1226-
l +
1227-
"</row>";
1280+
debugger
1281+
let rowDataMap = rowMap[v as keyof object];
1282+
if (rowDataMap) {
1283+
const body =
1284+
rowDataMap.startTag +
1285+
rowDataMap.details +
1286+
l +
1287+
rowDataMap.endTag;
1288+
let reg = new RegExp(rowDataMap.startTag + "[\\n\\s\\S]*?</row>");
1289+
sheetDataString = sheetDataString.replace(reg, body);
1290+
console.log(sheetDataString);
1291+
} else {
1292+
sheetDataString +=
1293+
'<row r="' +
1294+
v +
1295+
'" spans="1:' +
1296+
colsLength +
1297+
'" >' +
1298+
l +
1299+
"</row>";
1300+
}
12281301
});
12291302
}
12301303
}
@@ -1706,7 +1779,20 @@ export async function generateExcel(data: ExcelTable) {
17061779
};
17071780
indexId++;
17081781
}
1782+
if (needCalcChain) {
1783+
indexId++;
1784+
workbookRelString +=
1785+
'<Relationship Id="rId' +
1786+
indexId +
1787+
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain" Target="calcChain.xml"/>';
1788+
}
17091789

1790+
xlFolder?.file(
1791+
"calcChain.xml",
1792+
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<calcChain xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">' +
1793+
calcChainValue +
1794+
"</calcChain>"
1795+
);
17101796
let sheetKeys = Object.keys(mapData);
17111797
// in _rels
17121798
let relsFolder = zip.folder("_rels");
@@ -2042,7 +2128,8 @@ ${sh.checkboxDrawingContent}
20422128
commentId,
20432129
[...new Set(arrTypes)],
20442130
sheetDrawers,
2045-
checkboxForm
2131+
checkboxForm,
2132+
needCalcChain
20462133
)
20472134
);
20482135
if (data.backend) {

0 commit comments

Comments
 (0)