Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(import-xlsx): 1.support import xlsx 2. render supprt inline style

Dushusirdb33bbaafe61
diff --git a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
index 750e4e833b..b7d0dbd09a 100644
--- a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
+++ b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
@@ -29,6 +29,7 @@ import {
Nullable,
getColorStyle,
IDocumentRenderConfig,
+ getStyle,
} from '@univer/core';
import { BORDER_TYPE, COLOR_BLACK_RGB } from '../../Basics/Const';
import { IStylesCache, BorderCache } from './Interfaces';
@@ -660,7 +661,10 @@ export class SpreadsheetSkeleton extends Skeleton {
if (!cell) {
return true;
}
- const style = styles && styles.get(cell.s);
+
+ // style supports inline styles
+ // const style = styles && styles.get(cell.s);
+ const style = getStyle(styles, cell);
if (!skipBackgroundAndBorder && style && style.bg && style.bg.rgb) {
const rgb = style.bg.rgb;
diff --git a/packages/core/src/Shared/Common.ts b/packages/core/src/Shared/Common.ts
index 78099e4673..f934051a6d 100644
--- a/packages/core/src/Shared/Common.ts
+++ b/packages/core/src/Shared/Common.ts
@@ -1,7 +1,14 @@
import { Tools } from './Tools';
import { Nullable } from '.';
-import { ICellInfo, ISelection, ICellData, IColorStyle } from '../Interfaces';
-import { ColorBuilder } from '../Sheets/Domain';
+import {
+ ICellInfo,
+ ISelection,
+ ICellData,
+ IColorStyle,
+ IStyleData,
+} from '../Interfaces';
+import { ColorBuilder, Styles } from '../Sheets/Domain';
+import { DEFAULT_STYLES } from '../Const';
export function makeCellToSelection(
cellInfo: Nullable<ICellInfo>
@@ -78,3 +85,14 @@ export function getColorStyle(color: Nullable<IColorStyle>): Nullable<string> {
export function isFormulaString(value: any): boolean {
return Tools.isString(value) && value.indexOf('=') === 0 && value.length > 1;
}
+
+export function getStyle(styles: Styles, cell: Nullable<ICellData>): IStyleData {
+ let style;
+ if (cell && Tools.isObject(cell.s)) {
+ style = cell.s as IStyleData;
+ } else {
+ style = cell && cell.s && styles.get(cell.s);
+ }
+
+ return style || DEFAULT_STYLES;
+}
diff --git a/packages/core/src/Sheets/Domain/Range.ts b/packages/core/src/Sheets/Domain/Range.ts
index f84fe25c17..6b09f5ed65 100644
--- a/packages/core/src/Sheets/Domain/Range.ts
+++ b/packages/core/src/Sheets/Domain/Range.ts
@@ -45,6 +45,7 @@ import {
ITextDecoration,
} from '../../Interfaces';
import {
+ getStyle,
Nullable,
ObjectArrayPrimitiveType,
ObjectMatrix,
@@ -446,16 +447,16 @@ export class Range {
/**
*
- * @param arge Shorthand for the style that gets
+ * @param arg Shorthand for the style that gets
* @returns style value
*/
- private _getStyles(arge: string) {
+ private _getStyles(arg: string) {
return this.getValues().map((row) =>
row.map((cell: Nullable<ICellData>) => {
const styles = this._context.getWorkBook().getStyles();
- return (
- (cell?.s && styles.get(cell.s)![arge]) || DEFAULT_STYLES[arge]
- );
+
+ const style = getStyle(styles, cell);
+ return style[arg];
})
);
}
diff --git a/packages/sheets-plugin-import-xlsx/index.html b/packages/sheets-plugin-import-xlsx/index.html
index 45b8be6bf6..1fa3bb8e2e 100644
--- a/packages/sheets-plugin-import-xlsx/index.html
+++ b/packages/sheets-plugin-import-xlsx/index.html
@@ -17,7 +17,7 @@
<body>
<div id="app" style="position:absolute;z-index: 99999;"></div>
- <div id="universheet-demo" style="height: 100%;"></div>
+ <div id="universheet" style="height: 100%;"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
diff --git a/packages/sheets-plugin-import-xlsx/package.json b/packages/sheets-plugin-import-xlsx/package.json
index ca7745be2e..7afb7174c3 100644
--- a/packages/sheets-plugin-import-xlsx/package.json
+++ b/packages/sheets-plugin-import-xlsx/package.json
@@ -1,67 +1,68 @@
-{
- "name": "@univer/sheets-plugin-import-xlsx",
- "version": "0.0.1",
- "description": "UniverSheet normal sheets-plugin-import-xlsx",
- "keywords": [],
- "author": "DreamNum <[email protected]>",
- "license": "MIT",
- "main": "./lib/univer-sheets-plugin-import-xlsx.cjs.js",
- "module": "./lib/univer-sheets-plugin-import-xlsx.es.js",
- "browser": {
- "./lib/univer-sheets-plugin-import-xlsx.cjs.js": "./src/index.ts",
- "./lib/univer-sheets-plugin-import-xlsx.es.js": "./src/index.ts"
- },
- "types": "./src/Types/index.d.ts",
- "exports": {
- ".": {
- "import": {
- "node": "./lib/univer-sheets-plugin-import-xlsx.cjs.js",
- "default": "./lib/univer-sheets-plugin-import-xlsx.es.js"
- },
- "require": "./lib/univer-sheets-plugin-import-xlsx.cjs.js",
- "types": "./lib/univer-sheets-plugin-import-xlsx.d.ts"
- },
- "./*": "./src/*"
- },
- "directories": {
- "lib": "lib"
- },
- "scripts": {
- "test": "jest",
- "dev": "vite",
- "build": "tsc && vite build",
- "watch": "vite build",
- "link": "pnpm link ./",
- "serve": "vite preview"
- },
- "devDependencies": {
- "@preact/preset-vite": "2.0.0",
- "@testing-library/preact": "^2.0.1",
- "@types/jest": "^27.0.1",
- "@types/node": "^16.4.6",
- "@vitejs/plugin-legacy": "^1.5.1",
- "eslint": "^8.22.0",
- "eslint-config-airbnb-typescript": "^17.0.0",
- "eslint-config-prettier": "^8.3.0",
- "eslint-plugin-import": "^2.23.4",
- "eslint-plugin-prettier": "^3.4.0",
- "eslint-plugin-jsx-a11y": "^6.5.1",
- "eslint-plugin-react": "^7.28.0",
- "eslint-plugin-react-hooks": "^4.3.0",
- "less": "^4.1.1",
- "prettier": "^2.3.2",
- "ts-jest": "29.0.1",
- "ts-node": "^10.2.1",
- "typescript": "^4.4.1-rc",
- "vite": "^2.4.4"
- },
- "dependencies": {
- "@univer/common-plugin-data": "workspace:^0.0.1",
- "@univer/base-component": "workspace:^0.0.1",
- "@univer/base-render": "workspace:^0.0.1",
- "@univer/core": "workspace:^0.0.1",
- "@univer/base-sheets": "workspace:^0.0.1",
- "@univer/style-universheet": "workspace:^0.0.1",
- "preact": "^10.5.14"
- }
-}
+{
+ "name": "@univer/sheets-plugin-import-xlsx",
+ "version": "0.0.1",
+ "description": "UniverSheet normal sheets-plugin-import-xlsx",
+ "keywords": [],
+ "author": "DreamNum <[email protected]>",
+ "license": "MIT",
+ "main": "./lib/univer-sheets-plugin-import-xlsx.cjs.js",
+ "module": "./lib/univer-sheets-plugin-import-xlsx.es.js",
+ "browser": {
+ "./lib/univer-sheets-plugin-import-xlsx.cjs.js": "./src/index.ts",
+ "./lib/univer-sheets-plugin-import-xlsx.es.js": "./src/index.ts"
+ },
+ "types": "./src/Types/index.d.ts",
+ "exports": {
+ ".": {
+ "import": {
+ "node": "./lib/univer-sheets-plugin-import-xlsx.cjs.js",
+ "default": "./lib/univer-sheets-plugin-import-xlsx.es.js"
+ },
+ "require": "./lib/univer-sheets-plugin-import-xlsx.cjs.js",
+ "types": "./lib/univer-sheets-plugin-import-xlsx.d.ts"
+ },
+ "./*": "./src/*"
+ },
+ "directories": {
+ "lib": "lib"
+ },
+ "scripts": {
+ "test": "jest",
+ "dev": "vite",
+ "build": "tsc && vite build",
+ "watch": "vite build",
+ "link": "pnpm link ./",
+ "serve": "vite preview"
+ },
+ "devDependencies": {
+ "@preact/preset-vite": "2.0.0",
+ "@testing-library/preact": "^2.0.1",
+ "@types/jest": "^27.0.1",
+ "@types/node": "^16.4.6",
+ "@vitejs/plugin-legacy": "^1.5.1",
+ "eslint": "^8.22.0",
+ "eslint-config-airbnb-typescript": "^17.0.0",
+ "eslint-config-prettier": "^8.3.0",
+ "eslint-plugin-import": "^2.23.4",
+ "eslint-plugin-jsx-a11y": "^6.5.1",
+ "eslint-plugin-prettier": "^3.4.0",
+ "eslint-plugin-react": "^7.28.0",
+ "eslint-plugin-react-hooks": "^4.3.0",
+ "less": "^4.1.1",
+ "prettier": "^2.3.2",
+ "ts-jest": "29.0.1",
+ "ts-node": "^10.2.1",
+ "typescript": "^4.4.1-rc",
+ "vite": "^2.4.4"
+ },
+ "dependencies": {
+ "@univer/base-component": "workspace:^0.0.1",
+ "@univer/base-render": "workspace:^0.0.1",
+ "@univer/base-sheets": "workspace:^0.0.1",
+ "@univer/common-plugin-data": "workspace:^0.0.1",
+ "@univer/core": "workspace:^0.0.1",
+ "@univer/style-universheet": "workspace:^0.0.1",
+ "luckyexcel": "^1.0.1",
+ "preact": "^10.5.14"
+ }
+}
diff --git a/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts b/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts
index 7fe7331406..ed2766dc49 100644
--- a/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts
+++ b/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts
@@ -1,7 +1,8 @@
-import { PLUGIN_NAMES } from '@univer/core';
+import { migrate, PLUGIN_NAMES, Tools } from '@univer/core';
import { BaseComponentRender } from '@univer/base-component';
import { IToolBarItemProps, SelectTypes, SheetPlugin } from '@univer/base-sheets';
+import * as LuckyExcel from 'luckyexcel';
import { IMPORT_XLSX_PLUGIN_NAME } from '../Basic/Const';
import { ImportXlsxPlugin } from '../ImportXlsxPlugin';
@@ -27,25 +28,73 @@ export class ImportXlsxController {
hideSelectedIcon: true,
children: [
{
- locale: 'importXlsx.one',
+ locale: 'importXlsx.upload',
onClick: () => {
- console.log('one');
- },
- },
- {
- locale: 'importXlsx.two',
- onClick: () => {
- console.log('two');
- },
- },
- {
- locale: 'importXlsx.three',
- onClick: () => {
- console.log('three');
+ this.upload();
},
},
],
};
this._sheetPlugin.addToolButton(this._toolButton);
}
+
+ upload() {
+ const input = document.createElement('input');
+ input.type = 'file';
+ input.accept = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
+
+ input.addEventListener('change', (evt) => {
+ const files = evt.target.files;
+ if (files == null || files.length == 0) {
+ alert('No files wait for import');
+ return;
+ }
+
+ let name = files[0].name;
+ let suffixArr = name.split('.');
+ let suffix = suffixArr[suffixArr.length - 1];
+ if (suffix != 'xlsx') {
+ alert('Currently only supports the import of xlsx files');
+ return;
+ }
+ const transformExcelToLucky = LuckyExcel.default.transformExcelToLucky;
+
+ transformExcelToLucky(files[0], (exportJson, luckysheetfile) => {
+ if (exportJson.sheets == null || exportJson.sheets.length == 0) {
+ alert('Failed to read the content of the excel file, currently does not support xls files!');
+ return;
+ }
+
+ const luckysheetConfig = {
+ container: 'universheet',
+ data: exportJson.sheets,
+ title: exportJson.info.name,
+ };
+ const univerWorkbookConfig = migrate(luckysheetConfig);
+
+ const sheets = univerWorkbookConfig.sheets;
+
+ if (!sheets) {
+ return alert('No content');
+ }
+
+ const workbook = this._plugin.getContext().getWorkBook();
+
+ const order = Tools.deepClone(workbook.getConfig().sheetOrder);
+
+ // add new sheets
+ Object.keys(sheets).forEach((sheetId) => {
+ const sheetData = sheets[sheetId];
+ workbook.insertSheet(sheetData);
+ });
+
+ // remove other sheets
+ // order.forEach((sheetId: string) => {
+ // workbook.removeSheetBySheetId(sheetId);
+ // });
+ });
+ });
+
+ input.click();
+ }
}
diff --git a/packages/sheets-plugin-import-xlsx/src/Locale/en.ts b/packages/sheets-plugin-import-xlsx/src/Locale/en.ts
index 09e9cda2db..93226ae12d 100644
--- a/packages/sheets-plugin-import-xlsx/src/Locale/en.ts
+++ b/packages/sheets-plugin-import-xlsx/src/Locale/en.ts
@@ -1,8 +1,6 @@
export default {
importXlsx: {
- tooltip: 'ImportXlsx',
- one:'one',
- two:'two',
- three:'three',
+ tooltip: 'Import',
+ upload: 'Upload XLSX',
},
};
diff --git a/packages/sheets-plugin-import-xlsx/src/Locale/zh.ts b/packages/sheets-plugin-import-xlsx/src/Locale/zh.ts
index ec095153fb..46a3bf3489 100644
--- a/packages/sheets-plugin-import-xlsx/src/Locale/zh.ts
+++ b/packages/sheets-plugin-import-xlsx/src/Locale/zh.ts
@@ -1,8 +1,6 @@
export default {
importXlsx: {
- tooltip: 'ImportXlsx',
- one:'一',
- two:'二',
- three:'三',
+ tooltip: '导入',
+ upload: '上传XLSX',
},
};
diff --git a/packages/sheets-plugin-import-xlsx/src/main.tsx b/packages/sheets-plugin-import-xlsx/src/main.tsx
index 34787fd6a1..5693b29beb 100644
--- a/packages/sheets-plugin-import-xlsx/src/main.tsx
+++ b/packages/sheets-plugin-import-xlsx/src/main.tsx
@@ -6,9 +6,8 @@ import { DEFAULT_WORKBOOK_DATA } from '@univer/common-plugin-data';
import { ImportXlsxPlugin } from './ImportXlsxPlugin';
-
const uiDefaultConfigUp = {
- container: 'universheet-demo',
+ container: 'universheet',
layout: 'auto',
};