-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateSheet.ts
More file actions
65 lines (55 loc) · 1.68 KB
/
updateSheet.ts
File metadata and controls
65 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { google } from 'googleapis';
const auth = new google.auth.GoogleAuth({
keyFile: 'credentials.json',
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
const sheets = google.sheets({ version: 'v4', auth });
const spreadsheetId = '**sheetID';
interface TestResult {
id: string;
status: string;
sheetName: string;
}
export async function updateStatus(results: TestResult[]): Promise<void> {
try {
const resultsBySheet = results.reduce((acc, result) => {
if (!acc[result.sheetName]) {
acc[result.sheetName] = [];
}
acc[result.sheetName].push(result);
return acc;
}, {} as Record<string, TestResult[]>);
await Promise.all(
Object.entries(resultsBySheet).map(async ([sheetName, sheetResults]) => {
const response = await sheets.spreadsheets.values.get({
spreadsheetId,
range: `${sheetName}!B9:K`,
});
const rows = response.data.values || [];
let updated = false;
const idToRowIndex = new Map(
rows.map((row, index) => [row[1], index])
);
sheetResults.forEach(({ id, status }) => {
const rowIndex = idToRowIndex.get(id);
if (rowIndex !== undefined) {
rows[rowIndex][8] = status;
updated = true;
}
});
if (updated) {
return sheets.spreadsheets.values.update({
spreadsheetId,
range: `${sheetName}!B9:K`,
valueInputOption: 'RAW',
requestBody: { values: rows },
});
}
})
);
console.log('Updated successfully');
} catch (err) {
console.error('Update failed:', err);
throw err;
}
}