rapid-spreadjs 1.0.120 → 1.0.121
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +39 -34
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +39 -34
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -710,57 +710,81 @@ const SheetUtils = {
|
|
|
710
710
|
return;
|
|
711
711
|
}
|
|
712
712
|
/**
|
|
713
|
-
* 解析从Excel复制的TSV
|
|
713
|
+
* 解析从Excel复制的TSV格式数据,智能处理合并单元格导致的空列
|
|
714
714
|
* @param data 剪贴板中的原始TSV数据
|
|
715
|
-
* @returns
|
|
715
|
+
* @returns 二维数组,代表行和列,已压缩合并单元格
|
|
716
716
|
*/
|
|
717
717
|
function parseExcelData(data) {
|
|
718
718
|
const result = [];
|
|
719
719
|
let currentRow = [];
|
|
720
720
|
let currentCell = '';
|
|
721
|
-
let insideQuotes = false;
|
|
721
|
+
let insideQuotes = false;
|
|
722
722
|
for (let i = 0; i < data.length; i++) {
|
|
723
723
|
const char = data[i];
|
|
724
724
|
if (char === '"') {
|
|
725
|
-
// 遇到引号,切换状态
|
|
726
725
|
if (insideQuotes && data[i + 1] === '"') {
|
|
727
|
-
// 处理转义的双引号 ("")
|
|
728
726
|
currentCell += '"';
|
|
729
|
-
i++;
|
|
727
|
+
i++;
|
|
730
728
|
}
|
|
731
729
|
else {
|
|
732
730
|
insideQuotes = !insideQuotes;
|
|
733
731
|
}
|
|
734
732
|
}
|
|
735
733
|
else if (char === '\t' && !insideQuotes) {
|
|
736
|
-
//
|
|
734
|
+
// 遇到制表符,结束当前单元格
|
|
737
735
|
currentRow.push(currentCell);
|
|
738
736
|
currentCell = '';
|
|
739
737
|
}
|
|
740
738
|
else if ((char === '\n' || char === '\r') && !insideQuotes) {
|
|
741
|
-
//
|
|
742
|
-
// 处理可能是 \r\n 的情况
|
|
739
|
+
// 遇到换行符,结束当前行
|
|
743
740
|
if (char === '\r' && data[i + 1] === '\n') {
|
|
744
|
-
i++;
|
|
741
|
+
i++;
|
|
745
742
|
}
|
|
746
743
|
currentRow.push(currentCell);
|
|
747
|
-
|
|
744
|
+
// 关键改进:压缩当前行,合并连续的空单元格
|
|
745
|
+
const compressedRow = compressRow(currentRow);
|
|
746
|
+
if (compressedRow.length > 0) {
|
|
747
|
+
result.push(compressedRow);
|
|
748
|
+
}
|
|
748
749
|
currentRow = [];
|
|
749
750
|
currentCell = '';
|
|
750
751
|
}
|
|
751
752
|
else {
|
|
752
|
-
// 普通字符,添加到当前单元格
|
|
753
753
|
currentCell += char;
|
|
754
754
|
}
|
|
755
755
|
}
|
|
756
|
-
//
|
|
756
|
+
// 处理最后一行
|
|
757
757
|
if (currentCell !== '' || currentRow.length > 0) {
|
|
758
758
|
currentRow.push(currentCell);
|
|
759
|
-
|
|
759
|
+
const compressedRow = compressRow(currentRow);
|
|
760
|
+
if (compressedRow.length > 0) {
|
|
761
|
+
result.push(compressedRow);
|
|
762
|
+
}
|
|
760
763
|
}
|
|
761
|
-
// 过滤掉可能存在的空行(例如复制内容末尾的空白行)
|
|
762
764
|
return result.filter((row) => row.some((cell) => cell.trim() !== ''));
|
|
763
765
|
}
|
|
766
|
+
/**
|
|
767
|
+
* 压缩行数据:将连续的空单元格合并,只保留有内容的单元格
|
|
768
|
+
* 例如:['','','测试'] 变为 ['测试']
|
|
769
|
+
* ['A','','B','','C'] 变为 ['A','B','C']
|
|
770
|
+
*/
|
|
771
|
+
function compressRow(row) {
|
|
772
|
+
const compressed = [];
|
|
773
|
+
let emptyCount = 0;
|
|
774
|
+
for (let i = 0; i < row.length; i++) {
|
|
775
|
+
const cell = row[i].trim();
|
|
776
|
+
if (cell === '') {
|
|
777
|
+
emptyCount++;
|
|
778
|
+
}
|
|
779
|
+
else {
|
|
780
|
+
// 如果之前有空单元格,且当前单元格有内容,且这不是行首
|
|
781
|
+
if (emptyCount > 0 && compressed.length > 0) ;
|
|
782
|
+
compressed.push(cell);
|
|
783
|
+
emptyCount = 0;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return compressed;
|
|
787
|
+
}
|
|
764
788
|
const allCells = SheetUtils.getSheetSelectCellObjs(sheet);
|
|
765
789
|
const allCellsGroup = rapidUtils.groupByJson(allCells, 'row');
|
|
766
790
|
const allCellsEw = [];
|
|
@@ -771,25 +795,6 @@ const SheetUtils = {
|
|
|
771
795
|
const clipboardText = yield navigator.clipboard.readText();
|
|
772
796
|
// 得到剪贴板中的二维数组值
|
|
773
797
|
const clipboardData = parseExcelData(clipboardText);
|
|
774
|
-
// 验证剪贴板的二维数组内容和框选的单元格二维数组是否一致
|
|
775
|
-
let isRight = true;
|
|
776
|
-
if (allCellsEw.length != clipboardData.length) {
|
|
777
|
-
isRight = false;
|
|
778
|
-
}
|
|
779
|
-
else {
|
|
780
|
-
for (let i = 0; i < allCellsEw.length; i++) {
|
|
781
|
-
const row = allCellsEw[i];
|
|
782
|
-
const clipboardRow = clipboardData[i];
|
|
783
|
-
if (row.length != clipboardRow.length) {
|
|
784
|
-
isRight = false;
|
|
785
|
-
break;
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
if (!isRight && callFn != null && callFn != undefined && typeof callFn == 'function') {
|
|
790
|
-
callFn(false, '复制的内容区域和框选的表格区域不一致!');
|
|
791
|
-
return;
|
|
792
|
-
}
|
|
793
798
|
sheet.suspendPaint();
|
|
794
799
|
for (let i = 0; i < allCellsEw.length; i++) {
|
|
795
800
|
const row = allCellsEw[i];
|