mirror of
https://github.com/canna71/obsidian-sheets.git
synced 2026-07-22 08:30:27 +00:00
width and height
This commit is contained in:
parent
a3baa9e72e
commit
548e8a2171
2 changed files with 139 additions and 35 deletions
|
|
@ -285,10 +285,6 @@ function applyStyles(ssdata: any, wb: XLSX.WorkBook) {
|
|||
c: Number(cellId),
|
||||
});
|
||||
wb.Sheets[name][cellRef].s = wbStyle;
|
||||
console.log(
|
||||
`Cell (${cellId},${rowId})=>${cellRef} has style `,
|
||||
wbStyle
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,50 +1,73 @@
|
|||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { Workbook } from "exceljs";
|
||||
import { convertThemeColorToRGB, rgbToHex } from "./excelColors";
|
||||
import { CellData, CellStyle, RowData, SheetData } from "x-data-spreadsheet";
|
||||
|
||||
declare module "x-data-spreadsheet" {
|
||||
interface RowData {
|
||||
height?: number;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
export function toSpreadsheet(wb: Workbook) {
|
||||
|
||||
|
||||
console.log(wb);
|
||||
|
||||
const out = wb.worksheets.map((ws) => {
|
||||
const ows = {
|
||||
const ows : SheetData = {
|
||||
name: ws.name,
|
||||
rows: {} as any,
|
||||
merges: [] as string[],
|
||||
styles: [] as any[],
|
||||
rows: {},
|
||||
cols: {},
|
||||
merges: [] ,
|
||||
|
||||
// freezes:
|
||||
};
|
||||
|
||||
ows.styles = [];
|
||||
|
||||
for(const col of ws.columns){
|
||||
if(col.width && ows.cols && col.number) {
|
||||
ows.cols[col.number-1] = {width: width2px(col.width)}
|
||||
}
|
||||
}
|
||||
|
||||
ws.eachRow((row, rowNumber) => {
|
||||
const rowId = String(row.number - 1);
|
||||
const rowob: any = {
|
||||
cells: {},
|
||||
const rowId = (row.number - 1);
|
||||
const rowob: RowData = {
|
||||
cells: {}
|
||||
};
|
||||
ows.rows[rowId] = rowob;
|
||||
|
||||
if(ows.rows){
|
||||
ows.rows[rowId] = rowob;
|
||||
}
|
||||
if(row.height){
|
||||
// px = pt * ( 72pt / 96 )
|
||||
|
||||
rowob.height = pt2px(row.height);
|
||||
}
|
||||
if(row.hidden) {
|
||||
rowob.hidden = true;
|
||||
// TODO: implementit!
|
||||
}
|
||||
|
||||
row.eachCell((cell, cellNumber) => {
|
||||
const cellOb = {
|
||||
const cellOb : CellData = {
|
||||
text: cell.text
|
||||
} as any;
|
||||
} ;
|
||||
if (cell.formula) cellOb.text = "=" + cell.formula;
|
||||
|
||||
// style
|
||||
const oStyle: any = {};
|
||||
const oStyle: CellStyle = {};
|
||||
if (cell.style.fill) {
|
||||
//
|
||||
// console.log(cell.style.fill);
|
||||
if (cell.style.fill.type === "pattern") {
|
||||
const fgColor = cell.style.fill.fgColor;
|
||||
if (fgColor) {
|
||||
if (fgColor.argb) {
|
||||
//
|
||||
} else if (fgColor.theme !== undefined) {
|
||||
const theme = fgColor?.theme;
|
||||
const tint = (fgColor as any).tint || 0;
|
||||
const rgb = convertThemeColorToRGB(theme, tint);
|
||||
const hex = rgbToHex(rgb[0],rgb[1],rgb[2]);
|
||||
oStyle.bgcolor = "#" + hex;
|
||||
|
||||
const hexColor = getColor(fgColor);
|
||||
if(hexColor) {
|
||||
oStyle.bgcolor = "#" + hexColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -55,27 +78,89 @@ export function toSpreadsheet(wb: Workbook) {
|
|||
//
|
||||
}
|
||||
if (cell.style.font) {
|
||||
//
|
||||
const font = cell.style.font;
|
||||
|
||||
if(font.bold) {
|
||||
oStyle.font = oStyle.font || {}
|
||||
oStyle.font.bold = true;
|
||||
}
|
||||
if(font.color) {
|
||||
const hexColor = getColor(font.color);
|
||||
if(hexColor) {
|
||||
oStyle.color = "#" + hexColor;
|
||||
}
|
||||
}
|
||||
if(font.italic) {
|
||||
oStyle.font = oStyle.font || {};
|
||||
// oStyle.font
|
||||
(oStyle.font).italic = true;
|
||||
}
|
||||
if(font.strike) {
|
||||
(oStyle).strike = true;
|
||||
}
|
||||
if(font.underline) {
|
||||
(oStyle).underline = true;
|
||||
}
|
||||
if(font.name) {
|
||||
oStyle.font = oStyle.font || {};
|
||||
(oStyle.font).name = font.name;
|
||||
}
|
||||
if(font.family){
|
||||
oStyle.font = oStyle.font || {};
|
||||
oStyle.font.family = font.family;
|
||||
}
|
||||
if(font.size) {
|
||||
oStyle.font = oStyle.font || {};
|
||||
(oStyle.font).size = font.size;
|
||||
}
|
||||
// if(font.vertAlign) {
|
||||
// console.log("here");
|
||||
// }
|
||||
}
|
||||
if (cell.style.alignment) {
|
||||
//
|
||||
const alig = cell.style.alignment
|
||||
if(alig.vertical){
|
||||
(oStyle as any).valign = alig.vertical;
|
||||
}
|
||||
if(alig.horizontal){
|
||||
(oStyle as any).align = alig.horizontal;
|
||||
}
|
||||
if(cell.style.alignment.wrapText) {
|
||||
oStyle.textwrap = true;
|
||||
}
|
||||
|
||||
}
|
||||
if (cell.style.numFmt) {
|
||||
//
|
||||
const numFmt = cell.style.numFmt;
|
||||
if(numFmt.endsWith('%')){
|
||||
(oStyle as any).format = 'percent';
|
||||
// TODO: 0.00%
|
||||
} else if (numFmt.contains('€')) {
|
||||
(oStyle as any).format = 'eur';
|
||||
} else if (numFmt.contains('$')) {
|
||||
(oStyle as any).format = 'usd';
|
||||
} else if (numFmt === "@") {
|
||||
(oStyle as any).format = 'text';
|
||||
}
|
||||
// TODO: dates mm-dd-yy
|
||||
// TODO: [$-F800]dddd, mmmm dd, yyyy
|
||||
// TODO: 0.00
|
||||
// TODO: [$-F400]h:mm:ss AM/PM
|
||||
// TODO: 0.00E+00
|
||||
console.log(numFmt)
|
||||
}
|
||||
if (cell.style.protection) {
|
||||
//
|
||||
//TODO:
|
||||
}
|
||||
|
||||
if(Object.keys(oStyle).length>0){
|
||||
console.log(oStyle);
|
||||
// TODO: index it and put index in cell
|
||||
if(Object.keys(oStyle).length>0 && ows.styles){
|
||||
const j = JSON.stringify(oStyle)
|
||||
let styleIndex = ows.styles.findIndex(s =>
|
||||
JSON.stringify(s) == j
|
||||
)
|
||||
if(styleIndex<0) {
|
||||
ows.styles.push(oStyle);
|
||||
ows.styles?.push(oStyle);
|
||||
styleIndex = ows.styles.length-1;
|
||||
}
|
||||
cellOb.style = styleIndex;
|
||||
|
|
@ -106,6 +191,29 @@ export function toSpreadsheet(wb: Workbook) {
|
|||
ows.merges = merges;
|
||||
return ows;
|
||||
});
|
||||
|
||||
console.log(out);
|
||||
return out;
|
||||
}
|
||||
function pt2px(pt: number) {
|
||||
return Math.round(pt * 1.33333);
|
||||
}
|
||||
|
||||
function width2px(w: number) {
|
||||
// TODO: get actual character width
|
||||
// 10 units = 64px
|
||||
return Math.round(w * 6.4);
|
||||
}
|
||||
|
||||
function getColor(fgColor: any) {
|
||||
if (fgColor.argb) {
|
||||
const hex = fgColor.argb.substring(2);
|
||||
return hex;
|
||||
} else if (fgColor.theme !== undefined) {
|
||||
const theme = fgColor?.theme;
|
||||
const tint = (fgColor as any).tint || 0;
|
||||
const rgb = convertThemeColorToRGB(theme, tint);
|
||||
const hex = rgbToHex(rgb[0], rgb[1], rgb[2]);
|
||||
return hex;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue