to-spreadsheet
Version:
npm package to create spreadsheet in node environment and in browser
185 lines (184 loc) • 7.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCenteredCell = exports.createAlignedCell = exports.createVerticallyAlignedCell = exports.createHorizontallyAlignedCell = exports.createBackgroundDateCell = exports.createColoredCell = exports.createForegroundCell = exports.createBackgroundCell = exports.createBorderedDateCell = exports.createDateCell = exports.dateToExcelSerial = exports.createBorderedCell = exports.createStyledCell = exports.getStyleKey = exports.getBorderKey = exports.createRightBorder = exports.createLeftBorder = exports.createBottomBorder = exports.createTopBorder = exports.createAllBorders = exports.createBorder = exports.writeEquation = exports.Equation = exports.skipCell = exports.SkipCell = exports.calculateExtant = exports.rowColumnToVbPosition = exports.indexToRowIndex = exports.indexToVbRelationIndex = exports.indexToVbIndex = void 0;
const _1 = require(".");
const indexToVbIndex = (index) => index + 1;
exports.indexToVbIndex = indexToVbIndex;
const indexToVbRelationIndex = (index) => indexToVbIndex(index) + 2;
exports.indexToVbRelationIndex = indexToVbRelationIndex;
const indexToRowIndex = (index) => {
const base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
let result = "";
do {
const remainder = index % 26;
result = base[(remainder || 26) - 1] + result;
index = Math.floor(index / 26);
} while (index > 0);
return result;
};
exports.indexToRowIndex = indexToRowIndex;
const rowColumnToVbPosition = (row, col) => indexToRowIndex(indexToVbIndex(row)) + indexToVbIndex(col);
exports.rowColumnToVbPosition = rowColumnToVbPosition;
const calculateExtant = (rows) => rowColumnToVbPosition(Math.max(...rows.map(row => row.cells.length)) - 1, rows.length - 1);
exports.calculateExtant = calculateExtant;
class SkipCell {
constructor(skipCell) {
this.getSkipCell = () => this.skipCell;
this.skipCell = skipCell;
}
}
exports.SkipCell = SkipCell;
const skipCell = (skipCell) => new SkipCell(skipCell);
exports.skipCell = skipCell;
class Equation {
constructor(equation) {
this.getEquation = () => this.equation;
this.equation = equation;
}
}
exports.Equation = Equation;
const writeEquation = (equation) => new Equation(equation);
exports.writeEquation = writeEquation;
const createBorder = (border) => border;
exports.createBorder = createBorder;
const createAllBorders = (style = _1.BorderStyle.thin, color = "#000000") => ({
top: style,
right: style,
bottom: style,
left: style,
color
});
exports.createAllBorders = createAllBorders;
const createTopBorder = (style = _1.BorderStyle.thin, color = "#000000") => ({
top: style,
color
});
exports.createTopBorder = createTopBorder;
const createBottomBorder = (style = _1.BorderStyle.thin, color = "#000000") => ({
bottom: style,
color
});
exports.createBottomBorder = createBottomBorder;
const createLeftBorder = (style = _1.BorderStyle.thin, color = "#000000") => ({
left: style,
color
});
exports.createLeftBorder = createLeftBorder;
const createRightBorder = (style = _1.BorderStyle.thin, color = "#000000") => ({
right: style,
color
});
exports.createRightBorder = createRightBorder;
const getBorderKey = (border) => {
if (!border)
return "none";
const parts = [
border.top || "none",
border.right || "none",
border.bottom || "none",
border.left || "none",
border.color || "#000000"
];
return parts.join("-");
};
exports.getBorderKey = getBorderKey;
const getStyleKey = (style) => {
if (!style)
return "default";
const parts = [
getBorderKey(style.border),
style.backgroundColor || "no-bg",
style.foregroundColor || "no-fg",
style.horizontalAlignment || "no-halign",
style.verticalAlignment || "no-valign"
];
return parts.join("|");
};
exports.getStyleKey = getStyleKey;
const createStyledCell = (value, style) => {
if (typeof value === 'string') {
return {
type: _1.ICellType.string,
value: value,
style
};
}
else {
return {
type: _1.ICellType.number,
value,
style
};
}
};
exports.createStyledCell = createStyledCell;
const createBorderedCell = (value, border) => {
return createStyledCell(value, { border });
};
exports.createBorderedCell = createBorderedCell;
const dateToExcelSerial = (date) => {
const excelEpoch = new Date(1900, 0, 1);
const diffTime = date.getTime() - excelEpoch.getTime();
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
return diffDays + (date >= new Date(1900, 1, 29) ? 2 : 1);
};
exports.dateToExcelSerial = dateToExcelSerial;
const createDateCell = (date, style) => {
return {
type: _1.ICellType.date,
value: date,
style
};
};
exports.createDateCell = createDateCell;
const createBorderedDateCell = (date, border) => {
return createDateCell(date, { border });
};
exports.createBorderedDateCell = createBorderedDateCell;
const createBackgroundCell = (value, backgroundColor) => {
return createStyledCell(value, { backgroundColor });
};
exports.createBackgroundCell = createBackgroundCell;
const createForegroundCell = (value, foregroundColor) => {
return createStyledCell(value, { foregroundColor });
};
exports.createForegroundCell = createForegroundCell;
const createColoredCell = (value, backgroundColor, foregroundColor) => {
return createStyledCell(value, { backgroundColor, foregroundColor });
};
exports.createColoredCell = createColoredCell;
const createBackgroundDateCell = (date, backgroundColor) => {
return createDateCell(date, { backgroundColor });
};
exports.createBackgroundDateCell = createBackgroundDateCell;
const createHorizontallyAlignedCell = (value, alignment) => {
if (value instanceof Date) {
return createDateCell(value, { horizontalAlignment: alignment });
}
return createStyledCell(value, { horizontalAlignment: alignment });
};
exports.createHorizontallyAlignedCell = createHorizontallyAlignedCell;
const createVerticallyAlignedCell = (value, alignment) => {
if (value instanceof Date) {
return createDateCell(value, { verticalAlignment: alignment });
}
return createStyledCell(value, { verticalAlignment: alignment });
};
exports.createVerticallyAlignedCell = createVerticallyAlignedCell;
const createAlignedCell = (value, horizontal, vertical) => {
if (value instanceof Date) {
return createDateCell(value, {
horizontalAlignment: horizontal,
verticalAlignment: vertical
});
}
return createStyledCell(value, {
horizontalAlignment: horizontal,
verticalAlignment: vertical
});
};
exports.createAlignedCell = createAlignedCell;
const createCenteredCell = (value) => {
return createAlignedCell(value, _1.HorizontalAlignment.center, _1.VerticalAlignment.center);
};
exports.createCenteredCell = createCenteredCell;