Commit 02688d85 by Lin Wang

fix: enable send empty data to google sheet

parent 900dd84d
......@@ -36,26 +36,27 @@ async function uploadAllToSheets() {
const raw = readFileSync(join(dir, file), 'utf-8');
const data = JSON.parse(raw);
if (!data.length) continue;
const headers = Object.keys(data[0]);
const rows = data.map((row: any) =>
headers.map((h: string) => {
const cell = row[h];
return cell != null && typeof cell === 'object'
? JSON.stringify(cell)
: cell;
})
);
// Clear existing values
// Always clear existing values, even for empty data, to overwrite the sheet
await sheets.spreadsheets.values.clear({ spreadsheetId, range: `'${sheetName}'` });
// Write headers and data
await sheets.spreadsheets.values.append({
spreadsheetId,
range: `'${sheetName}'!A1`,
valueInputOption: 'RAW',
requestBody: { values: [headers, ...rows] }
});
if (data.length) {
const headers = Object.keys(data[0]);
const rows = data.map((row: any) =>
headers.map((h: string) => {
const cell = row[h];
return cell != null && typeof cell === 'object'
? JSON.stringify(cell)
: cell;
})
);
// Write headers and data
await sheets.spreadsheets.values.append({
spreadsheetId,
range: `'${sheetName}'!A1`,
valueInputOption: 'RAW',
requestBody: { values: [headers, ...rows] }
});
}
console.log(`✅ Uploaded ${file} to tab ${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