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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(clipboard): copy will not use filtered-out rows content (#3292)
YuHongec601c33545e
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-filter.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-filter.spec.tsnew file mode 100644index 0000000000..513fabef23--- /dev/null+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-filter.spec.ts@@ -0,0 +1,172 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { ICellData, Injector, Nullable, Univer } from '@univerjs/core';+import { ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE, ThemeService } from '@univerjs/core';+import type {+ ISetSelectionsOperationParams } from '@univerjs/sheets';+import {+ AddWorksheetMergeMutation,+ RemoveWorksheetMergeMutation,+ SetRangeValuesMutation,+ SetSelectionsOperation,+ SetWorksheetColWidthMutation,+ SetWorksheetRowHeightMutation,+} from '@univerjs/sheets';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';++import { ISheetClipboardService } from '../clipboard.service';+import { SheetCopyCommand, SheetPasteCommand } from '../../../commands/commands/clipboard.command';+import { clipboardTestBed } from './clipboard-test-bed';++const theme = {+ colorBlack: '#35322b',+};+describe('Test clipboard', () => {+ let univer: Univer;+ let get: Injector['get'];+ let commandService: ICommandService;+ let sheetClipboardService: ISheetClipboardService;+ let themeService: ThemeService;+ let getValues: (+ startRow: number,+ startColumn: number,+ endRow: number,+ endColumn: number+ ) => Array<Array<Nullable<ICellData>>> | undefined;++ beforeEach(async () => {+ const testBed = clipboardTestBed({+ id: 'test',+ appVersion: '3.0.0-alpha',+ sheets: {+ sheet1: {+ id: 'sheet1',+ cellData: {+ 0: {+ 0: {+ v: 1,+ },+ },+ 1: {+ 0: {+ v: 2,+ },+ },+ 2: {+ 0: {+ v: 3,+ },+ },+ 3: {+ 0: {+ v: 4,+ },+ },+ },+ },+ },+ locale: LocaleType.ZH_CN,+ name: '',+ sheetOrder: [],+ styles: {},+ });++ univer = testBed.univer;+ get = testBed.get;++ commandService = get(ICommandService);+ commandService.registerCommand(SetRangeValuesMutation);+ commandService.registerCommand(SetWorksheetRowHeightMutation);+ commandService.registerCommand(SetWorksheetColWidthMutation);+ commandService.registerCommand(AddWorksheetMergeMutation);+ commandService.registerCommand(RemoveWorksheetMergeMutation);+ commandService.registerCommand(SetSelectionsOperation);++ sheetClipboardService = get(ISheetClipboardService);++ themeService = get(ThemeService);+ themeService.setTheme(theme);+ getValues = (+ startRow: number,+ startColumn: number,+ endRow: number,+ endColumn: number+ ): Array<Array<Nullable<ICellData>>> | undefined =>+ get(IUniverInstanceService)+ .getUniverSheetInstance('test')+ ?.getSheetBySheetId('sheet1')+ ?.getRange(startRow, startColumn, endRow, endColumn)+ .getValues();+ });++ afterEach(() => {+ univer?.dispose();+ });++ describe('Copy area where have filtered out rows', () => {+ it('Paste content should not have filtered out rows', async () => {+ const worksheet = get(IUniverInstanceService).getUniverSheetInstance('test')!.getSheetBySheetId('sheet1')!;+ worksheet.__interceptViewModel((viewModel) => {+ viewModel.registerRowFilteredInterceptor({+ getRowFiltered(row: number): boolean {+ return row === 2;+ },+ });+ });+ expect(await commandService.executeCommand(SetSelectionsOperation.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ selections: [+ {+ range: { startRow: 0, startColumn: 0, endRow: 3, endColumn: 0, rangeType: RANGE_TYPE.NORMAL },+ primary: { actualRow: 0, actualColumn: 0, isMerged: false, isMergedMainCell: false },+ style: null,+ },+ ],+ } as ISetSelectionsOperationParams)).toBeTruthy();++ expect(await commandService.executeCommand(SheetCopyCommand.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ })).toBeTruthy();++ expect(await commandService.executeCommand(SetSelectionsOperation.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ selections: [+ {+ range: { startRow: 10, startColumn: 0, endRow: 10, endColumn: 0, rangeType: RANGE_TYPE.NORMAL },+ primary: { actualRow: 0, actualColumn: 0, isMerged: false, isMergedMainCell: false },+ style: null,+ },+ ],+ })).toBeTruthy();++ // clear cache will use html content+ sheetClipboardService.copyContentCache().clear();++ expect(await commandService.executeCommand(SheetPasteCommand.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ })).toBeTruthy();++ expect(getValues(10, 0, 10, 0)?.[0]?.[0]?.v).toEqual(1);+ expect(getValues(11, 0, 11, 0)?.[0]?.[0]?.v).toEqual(2);+ expect(getValues(12, 0, 12, 0)?.[0]?.[0]?.v).toEqual(4);+ });+ });+});diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.tsindex b2bd2d25a2..80e34daa0b 100644--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts@@ -328,7 +328,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard}// convert matrix to html- let html = this._usmToHtml.convert(matrix, range, hooks);+ let html = this._usmToHtml.convert(matrix, discreteRange, hooks);const plain = getMatrixPlainText(matrixFragment);const copyId = genId();diff --git a/packages/sheets-ui/src/services/clipboard/usm-to-html/convertor.ts b/packages/sheets-ui/src/services/clipboard/usm-to-html/convertor.tsindex 1778fcfc43..43d1e711c9 100644--- a/packages/sheets-ui/src/services/clipboard/usm-to-html/convertor.ts+++ b/packages/sheets-ui/src/services/clipboard/usm-to-html/convertor.ts@@ -14,9 +14,10 @@* limitations under the License.*/-import type { ICellData, IRange, ObjectMatrix } from '@univerjs/core';+import type { ICellData, ObjectMatrix } from '@univerjs/core';import type { ICellDataWithSpanInfo, IClipboardPropertyItem, ISheetClipboardHook } from '../type';+import type { IDiscreteRange } from '../../../controllers/utils/range-tools';/***@@ -130,28 +131,20 @@ export class USMToHtmlService {colSpan?: number | undefined;}>,- range: IRange,+ range: IDiscreteRange,hooks: ISheetClipboardHook[]): string {- const { startColumn, endColumn } = range;- const colStyles = getColStyle(getArrayFromTo(startColumn, endColumn), hooks);+ const { cols, rows } = range;+ if (!cols.length) {+ return '';+ }+ const colStyles = getColStyle(cols, hooks);// row styles and table contentsconst rowContents: string[] = [];const mergeSet = new Set<string>();-- const {- startRow: dataStartRow,- endRow: dataEndRow,- startColumn: dataStartColumn,- endColumn: dataEndColumn,- } = matrix.getDataRange();- for (let row = dataStartRow; row <= dataEndRow; row++) {- const cols = Array.from(- { length: dataEndColumn - dataStartColumn + 1 },- (_, index) => dataStartColumn + index- );+ rows.forEach((row) => {rowContents.push(getRowContent(row, cols, hooks, matrix, mergeSet));- }+ });const html = `<google-sheets-html-origin><table xmlns="http://www.w3.org/1999/xhtml" cellspacing="0" cellpadding="0" dir="ltr" style="table-layout:fixed;font-size:10pt;font-family:Arial;width:0px;border-collapse:collapse;border:none">${colStyles}<tbody>${rowContents.join('')}</tbody></table>`;