表单里面包括116500命名样式,某些命名样式的名称不可读。
由于命名样式太多,很难检查是否使用了这些样式。
因此,这边给您提供一个加载json后编的逻辑,删除了所有未使用的名称样式。
- var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
- window.onload = function () {
- spread.fromJSON(this.eee);
- removeUnusedNamedStyle(spread);
- };
- function removeUnusedNamedStyle(spread) {
- spread.suspendPaint();
- console.time("Remove Style");
- for (var i = 0, len = spread.getSheetCount(); i < len; i++) {
- var sheet = spread.getSheet(i);
- var styleNameCache = {};
- sheet.getNamedStyles().forEach(function (style) {
- styleNameCache[style.name] = true;
- });
- console.log(sheet.name(), "Original:", Object.keys(styleNameCache).length);
- for (var r = 0; r < sheet.getRowCount(); r++) {
- for (var c = 0; c < sheet.getColumnCount(); c++) {
- var name = sheet.getStyleName(r, c), style = sheet.getStyle(r, c);
- if (typeof name === "string" && styleNameCache[name]) {
- delete styleNameCache[name];
- } else if (style.parentName && styleNameCache[style.parentName]) {
- delete styleNameCache[style.parentName];
- }
- }
- }
- console.log(sheet.name(), "Should Remove:", Object.keys(styleNameCache).length);
- for (var nonUsedName in styleNameCache) {
- sheet.removeNamedStyle(nonUsedName);
- }
- }
- console.timeEnd("Remove Style");
- spread.resumePaint();
- }
- function aaa() {
- const excelIo = new GC.Spread.Excel.IO();
- const json = spread.toJSON();
- excelIo.save(json, function (blob) {
- saveAs(blob, "aaa.xlsx");
- }, function (err) {
- console.log(err);
- });
- }
-
复制代码
|