Skip to content

Commit

Permalink
feat: add the min rowCount & columnCount of the default workbookData (#…
Browse files Browse the repository at this point in the history
…59)

* chore: remove createFilt to filt.ts

* feat: add the min row & column count for default sheet

* chore: update pnpm-lock.yml
  • Loading branch information
karlsbeard authored Apr 19, 2024
1 parent ba1a8a2 commit 8483b69
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@univerjs/engine-render": "^0.1.7",
"@univerjs/facade": "^0.1.7",
"@univerjs/find-replace": "^0.1.7",
"@univerjs/protocol": "^0.1.17",
"@univerjs/protocol": "^0.1.18",
"@univerjs/rpc": "^0.1.7",
"@univerjs/sheets": "^0.1.7",
"@univerjs/sheets-conditional-formatting-ui": "^0.1.7",
Expand Down
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/modals/chooseType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { App } from 'obsidian'
import { Modal } from 'obsidian'
import type { UniverPluginSettings } from '@/types/setting'
import { createNewFile } from '@/utils/createFile'
import { createNewFile } from '@/utils/file'

interface ModalText {
title: string
Expand Down
33 changes: 0 additions & 33 deletions src/utils/createFile.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
import JSZip from 'jszip'
import { type App, Notice } from 'obsidian'
import { Type as DocType } from '@/views/udoc'
import { Type as SheetType } from '@/views/usheet'

export async function createNewFile(app: App, suffix: string, folderPath?: string, fileNum?: number): Promise<void> {
if (folderPath) {
try {
await app.vault.createFolder(folderPath)
}
catch (err) {
console.error(err)
}
}
const fileName = `Untitled${fileNum !== undefined ? `-${fileNum}` : ''}.${suffix}`
const filePath = folderPath !== undefined ? `${folderPath}/${fileName}` : fileName
try {
await app.vault.create(filePath, '')
await app.workspace.getLeaf(true).setViewState({
type: suffix === 'udoc' ? DocType : SheetType,
active: true,
state: {
file: filePath,
},
})

new Notice(`Created new ${suffix} file: ${filePath}`)
}
catch (err) {
const error = err
if (error.message.includes('File already exists'))
return await createNewFile(app, suffix, folderPath, (fileNum || 0) + 1)
}
}

export function transformToExcelBuffer(data: Record<string, any>): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
Expand Down
17 changes: 14 additions & 3 deletions src/views/xlsx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IWorkbookData, Univer, Workbook } from '@univerjs/core'
import { Tools } from '@univerjs/core'
import type { TFile, WorkspaceLeaf } from 'obsidian'
import { TextFileView } from 'obsidian'
import { FUniver } from '@univerjs/facade'
Expand Down Expand Up @@ -60,8 +59,20 @@ export class XlsxTypeView extends TextFileView {
const transformData = await window.univerProExchangeImport(raw)
const jsonData = JSON.parse(transformData)
let workbookData = transformSnapshotJsonToWorkbookData(jsonData.snapshot, jsonData.sheetBlocks)
if (!workbookData)
workbookData = Tools.deepClone({}) as IWorkbookData

workbookData = workbookData || {} as IWorkbookData

if (workbookData.sheets) {
const sheets = workbookData.sheets
Object.keys(sheets).forEach((sheetId) => {
const sheet = sheets[sheetId]
if (sheet.columnCount)
sheet.columnCount = Math.max(36, sheet.columnCount)

if (sheet.rowCount)
sheet.rowCount = Math.max(99, sheet.rowCount)
})
}

this.workbook = this.univer.createUniverSheet(workbookData)
}
Expand Down

0 comments on commit 8483b69

Please sign in to comment.