diff --git a/src/Views/SheetView.ts b/src/Views/SheetView.ts index eedee5e..edb7d1a 100644 --- a/src/Views/SheetView.ts +++ b/src/Views/SheetView.ts @@ -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; diff --git a/src/Views/spreadSheetWrapper.ts b/src/Views/spreadSheetWrapper.ts index fa66d9f..607041e 100644 --- a/src/Views/spreadSheetWrapper.ts +++ b/src/Views/spreadSheetWrapper.ts @@ -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(); + 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; +}