mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.9 KiB
93 lines
2.9 KiB
2 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import type { RequestParams } from 'nocodb-sdk'
|
||
2 years ago
|
import { ExportTypes } from 'nocodb-sdk'
|
||
|
import FileSaver from 'file-saver'
|
||
|
import * as XLSX from 'xlsx'
|
||
|
import { message } from 'ant-design-vue'
|
||
2 years ago
|
import { useI18n } from 'vue-i18n'
|
||
|
|
||
|
const { t } = useI18n()
|
||
2 years ago
|
|
||
|
const isPublicView = inject(IsPublicInj, ref(false))
|
||
2 years ago
|
|
||
2 years ago
|
const fields = inject(FieldsInj, ref([]))
|
||
|
|
||
|
const { project } = useProject()
|
||
|
|
||
|
const { $api } = useNuxtApp()
|
||
|
|
||
2 years ago
|
const meta = inject(MetaInj, ref())
|
||
2 years ago
|
|
||
|
const selectedView = inject(ActiveViewInj)
|
||
|
|
||
2 years ago
|
const { sorts, nestedFilters } = useSmartsheetStoreOrThrow()
|
||
|
|
||
2 years ago
|
const exportFile = async (exportType: ExportTypes) => {
|
||
|
let offset = 0
|
||
|
let c = 1
|
||
|
const responseType = exportType === ExportTypes.EXCEL ? 'base64' : 'blob'
|
||
|
|
||
|
try {
|
||
|
while (!isNaN(offset) && offset > -1) {
|
||
|
let res
|
||
|
if (isPublicView.value) {
|
||
|
const { exportFile: sharedViewExportFile } = useSharedView()
|
||
|
res = await sharedViewExportFile(fields.value, offset, exportType, responseType)
|
||
|
} else {
|
||
|
res = await $api.dbViewRow.export(
|
||
|
'noco',
|
||
2 years ago
|
project.value?.title as string,
|
||
|
meta.value?.title as string,
|
||
2 years ago
|
selectedView?.value.title as string,
|
||
|
exportType,
|
||
|
{
|
||
|
responseType,
|
||
|
query: {
|
||
2 years ago
|
fields: fields.value.map((field) => field.title),
|
||
2 years ago
|
offset,
|
||
2 years ago
|
sortArrJson: JSON.stringify(sorts.value),
|
||
|
filterArrJson: JSON.stringify(nestedFilters.value),
|
||
2 years ago
|
},
|
||
2 years ago
|
} as RequestParams,
|
||
2 years ago
|
)
|
||
|
}
|
||
|
const { data, headers } = res
|
||
|
if (exportType === ExportTypes.EXCEL) {
|
||
|
const workbook = XLSX.read(data, { type: 'base64' })
|
||
2 years ago
|
XLSX.writeFile(workbook, `${meta.value?.title}_exported_${c++}.xlsx`)
|
||
2 years ago
|
} else if (exportType === ExportTypes.CSV) {
|
||
|
const blob = new Blob([data], { type: 'text/plain;charset=utf-8' })
|
||
2 years ago
|
FileSaver.saveAs(blob, `${meta.value?.title}_exported_${c++}.csv`)
|
||
2 years ago
|
}
|
||
|
offset = +headers['nc-export-offset']
|
||
|
if (offset > -1) {
|
||
2 years ago
|
// Downloading more files
|
||
|
message.info(t('msg.info.downloadingMoreFiles'))
|
||
2 years ago
|
} else {
|
||
2 years ago
|
// Successfully exported all table data
|
||
|
message.success(t('msg.success.tableDataExported'))
|
||
2 years ago
|
}
|
||
|
}
|
||
|
} catch (e: any) {
|
||
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
|
}
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<a-menu-item>
|
||
2 years ago
|
<div v-e="['a:actions:download-csv']" class="nc-project-menu-item" @click="exportFile(ExportTypes.CSV)">
|
||
2 years ago
|
<MdiDownloadOutline class="text-gray-500" />
|
||
|
<!-- Download as CSV -->
|
||
|
{{ $t('activity.downloadCSV') }}
|
||
|
</div>
|
||
|
</a-menu-item>
|
||
|
<a-menu-item>
|
||
2 years ago
|
<div v-e="['a:actions:download-excel']" class="nc-project-menu-item" @click="exportFile(ExportTypes.EXCEL)">
|
||
2 years ago
|
<MdiDownloadOutline class="text-gray-500" />
|
||
|
<!-- Download as XLSX -->
|
||
|
{{ $t('activity.downloadExcel') }}
|
||
|
</div>
|
||
|
</a-menu-item>
|
||
|
</template>
|