Optimized Saving

This commit is contained in:
Gabriele Cannata 2023-08-05 22:22:54 +02:00
parent 8f36e725a3
commit adeb94d2be
2 changed files with 41 additions and 3 deletions

View file

@ -18,7 +18,9 @@ import * as XLSX from "xlsx";
import * as ExcelJS from "exceljs";
import { stox } from "../utils/xlsxpread";
import { toSpreadsheet } from "src/utils/excelConverter";
import { createSpreadSheet, saveToFile } from "./spreadSheetWrapper";
import { createSpreadSheet, prepareDataForSaving, saveToFile } from "./spreadSheetWrapper";
import Spreadsheet from "x-data-spreadsheet";
import { SheetData } from "x-data-spreadsheet";
const DEFAULT_OPTIONS = {
@ -199,13 +201,15 @@ async function parseFileContent(filename:string, fileContent:ArrayBuffer){
}
function saveDataIntoBlock(
data: any,
sheet: any,
ctx: MarkdownPostProcessorContext
) {
const s = (ctx as any).spreadsheet;
const dts = s.getData();
const s = (ctx as any).spreadsheet as Spreadsheet;
const dts = prepareDataForSaving( s.getData() as SheetData[] );
const view = app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;

View file

@ -4,6 +4,7 @@ import Spreadsheet from "x-data-spreadsheet";
import * as XLSX from "xlsx";
import { xtos } from "../utils/xlsxpread";
import { toExcelJS } from "src/utils/excelConverter";
import { SheetData } from "x-data-spreadsheet";
function resolve_book_type(fileName: string): XLSX.BookType {
@ -129,3 +130,36 @@ function styleSS2WB(ssstyle: any) {
}
return style;
}
export function prepareDataForSaving(data: SheetData[]): SheetData[] {
for(const sheet of data){
const actualStyles = [];
const usedStyles = new Map<number, number>();
if(sheet.styles !== undefined) {
for(const rowId in sheet.rows) {
const rowNum = Number(rowId)
if(!isNaN(rowNum)) {
const row = sheet.rows[rowNum];
for(const cellId in row.cells) {
const cellNum = Number(cellId);
const cell = row.cells[cellNum];
if(cell.style !== undefined){
if(usedStyles.has(cell.style)){
cell.style = usedStyles.get(cell.style)
} else {
actualStyles.push(sheet.styles[cell.style])
const index = actualStyles.length-1;
usedStyles.set(cell.style, index)
cell.style = index;
}
}
}
}
}
}
sheet.styles = actualStyles;
}
return data;
}