Commit e93da218 by Lin Wang

fix: avoid empty tables

parent fef17669
......@@ -42,6 +42,21 @@ async function uploadAllToSheets() {
.filter(f => f.endsWith('.json'))
.map(f => ({ subdir, file: f, path: join(d, f) }));
});
// Filter out empty JSON files
const uploadFiles: Array<{subdir: string; file: string; path: string; data: any[]}> = [];
for (const fObj of files) {
const raw = readFileSync(fObj.path, 'utf-8');
const data = JSON.parse(raw);
if (Array.isArray(data) && data.length === 0) {
console.log(`🚫 Skipping empty file ${fObj.file}`);
} else {
uploadFiles.push({ ...fObj, data });
}
}
if (!uploadFiles.length) {
console.log('No non-empty JSON files to upload');
return;
}
// Fetch existing sheet names
const { data: spreadsheet } = await sheets.spreadsheets.get({ spreadsheetId });
......@@ -55,9 +70,9 @@ async function uploadAllToSheets() {
// Track existing titles (only first sheet remains)
const existing: string[] = [sheetsArr[0]?.properties?.title || ''];
// Rename the remaining sheet to match the first file (if any)
if (files.length > 0) {
const firstName = files[0].file.replace(/\.json$/, '');
// Rename the remaining sheet to match the first non-empty file
if (uploadFiles.length > 0) {
const firstName = uploadFiles[0].file.replace(/\.json$/, '');
const firstSheetId = sheetsArr[0].properties?.sheetId!;
if (existing[0] !== firstName) {
await sheets.spreadsheets.batchUpdate({
......@@ -70,14 +85,7 @@ async function uploadAllToSheets() {
existing[0] = firstName;
}
for (const { subdir, file, path } of files) {
// Read and skip empty JSON
const fileRaw = readFileSync(path, 'utf-8');
const fileData = JSON.parse(fileRaw);
if (Array.isArray(fileData) && fileData.length === 0) {
console.log(`🚫 Skipping empty file ${file}`);
continue;
}
for (const { subdir, file, path, data: fileData } of uploadFiles) {
// Use file name (without extension) as sheetName
const sheetName = file.replace(/\.json$/, '');
if (!existing.includes(sheetName)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment