Commit fef17669 by Lin Wang

feat: skip empty JSON

parent 9ae6ee9e
...@@ -71,6 +71,13 @@ async function uploadAllToSheets() { ...@@ -71,6 +71,13 @@ async function uploadAllToSheets() {
} }
for (const { subdir, file, path } of files) { 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;
}
// Use file name (without extension) as sheetName // Use file name (without extension) as sheetName
const sheetName = file.replace(/\.json$/, ''); const sheetName = file.replace(/\.json$/, '');
if (!existing.includes(sheetName)) { if (!existing.includes(sheetName)) {
...@@ -81,15 +88,14 @@ async function uploadAllToSheets() { ...@@ -81,15 +88,14 @@ async function uploadAllToSheets() {
requests: [{ addSheet: { properties: { title: sheetName } } }] requests: [{ addSheet: { properties: { title: sheetName } } }]
} }
}); });
existing.push(sheetName);
} }
const raw = readFileSync(path, 'utf-8');
const data = JSON.parse(raw);
// Always clear existing values, even for empty data, to overwrite the sheet // Always clear existing values, even for empty data, to overwrite the sheet
await sheets.spreadsheets.values.clear({ spreadsheetId, range: `'${sheetName}'` }); await sheets.spreadsheets.values.clear({ spreadsheetId, range: `'${sheetName}'` });
if (data.length) { if (fileData.length) {
const headers = Object.keys(data[0]); const headers = Object.keys(fileData[0]);
const rows = data.map((row: any) => const rows = fileData.map((row: any) =>
headers.map((h: string) => { headers.map((h: string) => {
const cell = row[h]; const cell = row[h];
return cell != null && typeof cell === 'object' return cell != null && typeof cell === 'object'
......
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