-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleSheets.ts
80 lines (70 loc) · 2.41 KB
/
googleSheets.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ProcessedData } from '../types';
const GOOGLE_API_KEY = import.meta.env.VITE_GOOGLE_API_KEY;
const GOOGLE_CLIENT_ID = import.meta.env.VITE_GOOGLE_CLIENT_ID;
export class GoogleSheetsService {
private accessToken: string | null = null;
async initialize() {
// Load the Google API client library
await this.loadGoogleAPI();
}
private loadGoogleAPI(): Promise<void> {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = 'https://apis.google.com/js/api.js';
script.onload = () => {
gapi.load('client:auth2', async () => {
try {
await gapi.client.init({
apiKey: GOOGLE_API_KEY,
clientId: GOOGLE_CLIENT_ID,
scope: 'https://www.googleapis.com/auth/spreadsheets.readonly',
discoveryDocs: ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
});
resolve();
} catch (error) {
reject(error);
}
});
};
script.onerror = () => reject(new Error('Failed to load Google API'));
document.body.appendChild(script);
});
}
async authenticate(): Promise<void> {
try {
const googleAuth = gapi.auth2.getAuthInstance();
const user = await googleAuth.signIn();
this.accessToken = user.getAuthResponse().access_token;
} catch (error) {
console.error('Authentication failed:', error);
throw new Error('Failed to authenticate with Google');
}
}
async getSheetData(spreadsheetId: string, range: string): Promise<ProcessedData> {
if (!this.accessToken) {
throw new Error('Not authenticated');
}
try {
const response = await gapi.client.sheets.spreadsheets.values.get({
spreadsheetId,
range,
});
const values = response.result.values;
if (!values || values.length === 0) {
throw new Error('No data found in sheet');
}
const columns = values[0] as string[];
const rows = values.slice(1).map(row => {
const rowData: { [key: string]: string } = {};
columns.forEach((col, index) => {
rowData[col] = row[index] as string || '';
});
return rowData;
});
return { columns, rows };
} catch (error) {
console.error('Failed to fetch sheet data:', error);
throw new Error('Failed to fetch sheet data');
}
}
}