-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheets-data.html
More file actions
455 lines (423 loc) · 12.9 KB
/
Copy pathsheets-data.html
File metadata and controls
455 lines (423 loc) · 12.9 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Sheets Integration - ShareOut Demo</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
min-height: 100vh;
padding: 40px 20px;
}
.container {
max-width: 900px;
margin: 0 auto;
}
.card {
background: white;
border-radius: 16px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
padding: 32px;
margin-bottom: 24px;
}
h1 {
font-size: 28px;
margin-bottom: 8px;
color: #1a1a2e;
}
.subtitle {
color: #666;
margin-bottom: 24px;
}
.step {
display: flex;
align-items: flex-start;
gap: 16px;
padding: 20px 0;
border-bottom: 1px solid #eee;
}
.step:last-child { border-bottom: none; }
.step-number {
width: 32px;
height: 32px;
border-radius: 50%;
background: #e8e8e8;
display: flex;
align-items: center;
justify-content: center;
font-weight: 600;
color: #666;
flex-shrink: 0;
}
.step.active .step-number {
background: #4f46e5;
color: white;
}
.step.completed .step-number {
background: #22c55e;
color: white;
}
.step-content { flex: 1; }
.step-title {
font-weight: 600;
margin-bottom: 8px;
color: #333;
}
.step-description {
color: #666;
font-size: 14px;
margin-bottom: 12px;
}
.btn {
padding: 12px 24px;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
}
.btn-primary {
background: #4f46e5;
color: white;
}
.btn-primary:hover { background: #4338ca; }
.btn-primary:disabled {
background: #ccc;
cursor: not-allowed;
}
.btn-secondary {
background: #f3f4f6;
color: #374151;
}
.btn-secondary:hover { background: #e5e7eb; }
.input-group {
display: flex;
gap: 8px;
margin-top: 12px;
}
.input-group input {
flex: 1;
padding: 12px 16px;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 14px;
}
.input-group input:focus {
outline: none;
border-color: #4f46e5;
}
.status-badge {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
border-radius: 20px;
font-size: 13px;
font-weight: 500;
}
.status-badge.connected {
background: #d1fae5;
color: #065f46;
}
.status-badge.disconnected {
background: #fee2e2;
color: #991b1b;
}
.data-table {
width: 100%;
border-collapse: collapse;
margin-top: 16px;
}
.data-table th, .data-table td {
padding: 12px 16px;
text-align: left;
border-bottom: 1px solid #eee;
}
.data-table th {
background: #f9fafb;
font-weight: 600;
font-size: 13px;
color: #6b7280;
text-transform: uppercase;
}
.data-table tr:hover {
background: #f9fafb;
}
.empty-state {
text-align: center;
padding: 40px;
color: #666;
}
.empty-state svg {
width: 64px;
height: 64px;
margin-bottom: 16px;
opacity: 0.5;
}
.loading {
display: flex;
align-items: center;
gap: 8px;
color: #666;
}
.spinner {
width: 20px;
height: 20px;
border: 2px solid #e5e7eb;
border-top-color: #4f46e5;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error {
background: #fef2f2;
border: 1px solid #fecaca;
color: #991b1b;
padding: 12px 16px;
border-radius: 8px;
margin-top: 12px;
}
.hint {
font-size: 13px;
color: #9ca3af;
margin-top: 8px;
}
</style>
</head>
<body>
<div class="container">
<div class="card">
<h1>Google Sheets Integration</h1>
<p class="subtitle">Connect your Google account and import data from any spreadsheet</p>
<!-- Step 1: Connect Google -->
<div class="step" id="step1">
<div class="step-number">1</div>
<div class="step-content">
<div class="step-title">Connect Google Account</div>
<div class="step-description">
Authorize ShareOut to access your Google Sheets. We only request read access.
</div>
<div id="connectionStatus">
<span class="status-badge disconnected">Not Connected</span>
</div>
<div style="margin-top: 12px;">
<button class="btn btn-primary" id="connectBtn">Connect Google Sheets</button>
</div>
</div>
</div>
<!-- Step 2: Enter Sheet URL -->
<div class="step" id="step2">
<div class="step-number">2</div>
<div class="step-content">
<div class="step-title">Enter Spreadsheet URL</div>
<div class="step-description">
Paste the URL of the Google Sheet you want to import. Make sure it's shared with your connected Google account.
</div>
<div class="input-group">
<input type="text" id="sheetUrl" placeholder="https://docs.google.com/spreadsheets/d/..." disabled>
<button class="btn btn-primary" id="fetchBtn" disabled>Fetch Data</button>
</div>
<p class="hint">Tip: You can also specify a range, e.g., Sheet1!A1:D100</p>
</div>
</div>
<!-- Step 3: View Data -->
<div class="step" id="step3">
<div class="step-number">3</div>
<div class="step-content">
<div class="step-title">Your Data</div>
<div class="step-description" id="dataDescription">
Fetched data will appear here.
</div>
<div id="dataContainer">
<div class="empty-state">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
<p>No data loaded yet</p>
</div>
</div>
</div>
</div>
</div>
<!-- Code Example -->
<div class="card">
<h2 style="font-size: 18px; margin-bottom: 16px;">Code Example</h2>
<pre style="background: #1a1a2e; color: #e5e5e5; padding: 20px; border-radius: 8px; overflow-x: auto; font-size: 13px;"><code>// Initialize ShareOut SDK
const shareout = new ShareOut();
// Step 1: Check connection or authorize
if (!await shareout.sheets.isConnected()) {
await shareout.sheets.authorize();
}
// Step 2: Fetch data from any Google Sheet
const { data, headers, rowCount } = await shareout.sheets.fetch({
spreadsheetUrl: 'https://docs.google.com/spreadsheets/d/...',
range: 'Sheet1', // optional
headers: true // first row as headers
});
// Step 3: Use the data
console.log(`Loaded ${rowCount} rows with columns: ${headers.join(', ')}`);
data.forEach(row => console.log(row));</code></pre>
</div>
</div>
<script src="https://shareout.site/sdk/shareout.js"></script>
<script>
let shareout = null;
let isConnected = false;
// Initialize
async function init() {
try {
shareout = new ShareOut();
await checkConnection();
} catch (e) {
console.warn('ShareOut SDK not available:', e);
}
}
async function checkConnection() {
if (!shareout) return;
try {
isConnected = await shareout.sheets.isConnected();
updateConnectionUI();
} catch (e) {
console.error('Failed to check connection:', e);
}
}
function updateConnectionUI() {
const statusEl = document.getElementById('connectionStatus');
const connectBtn = document.getElementById('connectBtn');
const sheetUrl = document.getElementById('sheetUrl');
const fetchBtn = document.getElementById('fetchBtn');
const step1 = document.getElementById('step1');
const step2 = document.getElementById('step2');
if (isConnected) {
statusEl.innerHTML = '<span class="status-badge connected">Connected</span>';
connectBtn.textContent = 'Reconnect';
sheetUrl.disabled = false;
fetchBtn.disabled = false;
step1.classList.add('completed');
step1.classList.remove('active');
step2.classList.add('active');
} else {
statusEl.innerHTML = '<span class="status-badge disconnected">Not Connected</span>';
connectBtn.textContent = 'Connect Google Sheets';
sheetUrl.disabled = true;
fetchBtn.disabled = true;
step1.classList.add('active');
step1.classList.remove('completed');
step2.classList.remove('active');
}
}
// Connect button
document.getElementById('connectBtn').addEventListener('click', async () => {
if (!shareout) {
alert('ShareOut SDK not loaded');
return;
}
const btn = document.getElementById('connectBtn');
btn.disabled = true;
btn.innerHTML = '<span class="loading"><span class="spinner"></span> Connecting...</span>';
try {
const success = await shareout.sheets.authorize();
if (success) {
isConnected = true;
updateConnectionUI();
}
} catch (e) {
console.error('Connection failed:', e);
alert('Failed to connect: ' + e.message);
} finally {
btn.disabled = false;
btn.textContent = isConnected ? 'Reconnect' : 'Connect Google Sheets';
}
});
// Fetch button
document.getElementById('fetchBtn').addEventListener('click', async () => {
const urlInput = document.getElementById('sheetUrl');
const url = urlInput.value.trim();
if (!url) {
alert('Please enter a spreadsheet URL');
return;
}
const btn = document.getElementById('fetchBtn');
const container = document.getElementById('dataContainer');
const description = document.getElementById('dataDescription');
const step3 = document.getElementById('step3');
btn.disabled = true;
btn.innerHTML = '<span class="loading"><span class="spinner"></span></span>';
container.innerHTML = '<div class="loading"><span class="spinner"></span> Fetching data...</div>';
try {
const result = await shareout.sheets.fetch({
spreadsheetUrl: url,
headers: true
});
description.textContent = `Loaded ${result.rowCount} rows`;
step3.classList.add('active');
if (result.data.length === 0) {
container.innerHTML = `
<div class="empty-state">
<p>The spreadsheet is empty</p>
</div>
`;
} else {
renderTable(result.data, result.headers);
}
} catch (e) {
console.error('Fetch failed:', e);
container.innerHTML = `<div class="error">Failed to fetch: ${e.message}</div>`;
} finally {
btn.disabled = false;
btn.textContent = 'Fetch Data';
}
});
function renderTable(data, headers) {
const container = document.getElementById('dataContainer');
if (!headers || headers.length === 0) {
// Raw array mode
const firstRow = data[0];
if (Array.isArray(firstRow)) {
headers = firstRow.map((_, i) => `Column ${i + 1}`);
} else {
headers = Object.keys(data[0] || {});
}
}
let html = '<table class="data-table"><thead><tr>';
headers.forEach(h => {
html += `<th>${escapeHtml(h)}</th>`;
});
html += '</tr></thead><tbody>';
data.forEach(row => {
html += '<tr>';
headers.forEach(h => {
const value = row[h] ?? '';
html += `<td>${escapeHtml(String(value))}</td>`;
});
html += '</tr>';
});
html += '</tbody></table>';
container.innerHTML = html;
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Handle OAuth callback messages
window.addEventListener('message', (event) => {
if (event.data?.type === 'shareout_sheets_connected') {
isConnected = true;
updateConnectionUI();
}
});
// Check URL params for successful connection redirect
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get('sheets_connected') === 'true') {
// Remove param from URL
window.history.replaceState({}, '', window.location.pathname);
}
init();
</script>
</body>
</html>