@@ -2,8 +2,10 @@ import { Injector } from '@wendellhu/redi';
2
2
import type { IMessage , PushStorageMessage , SetStorageMessage } from '@univer-clipsheet-core/shared' ;
3
3
import { ClipsheetMessageTypeEnum , IframeViewTypeEnum , listenPingSignal , PingSignalKeyEnum , promisifyMessage , requestDataSource , sendSetIframeViewMessage } from '@univer-clipsheet-core/shared' ;
4
4
import { ClientController , ClientViewService , CoverService , DetectTablesService , ElementInspectService , IframeViewController , RemountObserver , ScraperClientService , TableScrapingShadowComponent } from '@univer-clipsheet-core/ui' ;
5
- import type { IGetTableRecordsParams , IPreviewSheetStorageValue , ITableRecordsResponse } from '@univer-clipsheet-core/table' ;
6
- import { PreviewSheetFromEnum , TableDataSourceKeyEnum , TableStorageKeyEnum } from '@univer-clipsheet-core/table' ;
5
+ import type { IGetTableRecordsParams , IInitialSheet , IPreviewSheetStorageValue , ITableRecord , ITableRecordsResponse } from '@univer-clipsheet-core/table' ;
6
+ import { getCellValue , PreviewSheetFromEnum , TableDataSourceKeyEnum , TableStorageKeyEnum } from '@univer-clipsheet-core/table' ;
7
+ import { saveAs } from 'file-saver' ;
8
+ import Papa from 'papaparse' ;
7
9
8
10
// You can use startAjaxIntercept to intercept AJAX requests
9
11
// startAjaxIntercept(chrome.runtime.getURL('ajax-interceptor/index.iife.js'), (res) => {
@@ -103,3 +105,32 @@ listenPingSignal(PingSignalKeyEnum.ScraperFormShowed, () => {
103
105
} ) ;
104
106
} ) ;
105
107
108
+ chrome . runtime . onMessage . addListener ( async ( req , _sender , _sendResponse ) => {
109
+ if ( req . type === 'EXPORT_CSV' ) {
110
+ const tableRecord = req . payload as ITableRecord ;
111
+ const key = TableStorageKeyEnum . TableSheetsPrefix + tableRecord . id ;
112
+ chrome . runtime . sendMessage ( {
113
+ type : ClipsheetMessageTypeEnum . GetStorage ,
114
+ payload : key ,
115
+ } ) ;
116
+ const res = await promisifyMessage < PushStorageMessage > ( ( msg ) => msg . type === ClipsheetMessageTypeEnum . PushStorage && msg . payload . key === key ) ;
117
+
118
+ if ( ! Array . isArray ( res . payload . value ) ) {
119
+ return ;
120
+ }
121
+ const sheet = res . payload . value [ 0 ] as IInitialSheet ;
122
+ if ( ! sheet ) {
123
+ return ;
124
+ }
125
+ const rows = sheet . rows ;
126
+
127
+ const rowValues = rows . map ( ( row ) => row . cells . map ( getCellValue ) ) ;
128
+ const matrix = [ sheet . columnName ] . concat ( rowValues ) ;
129
+ const csv = Papa . unparse ( matrix ) ;
130
+
131
+ const blob = new Blob ( [ `\uFEFF${ csv } ` ] , { type : 'text/csv;charset=utf-8' } ) ;
132
+
133
+ saveAs ( blob , `${ tableRecord ?. title } .csv` ) ;
134
+ }
135
+ } ) ;
136
+
0 commit comments