Compare commits
2 Commits
03eeef5c69
...
7b4a179428
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b4a179428 | ||
|
|
5ef9735ea5 |
@@ -51,25 +51,48 @@ export async function getRecentSpreadsheets(
|
|||||||
* Get data from a Google Sheet
|
* Get data from a Google Sheet
|
||||||
* @param refreshToken - Google refresh token
|
* @param refreshToken - Google refresh token
|
||||||
* @param spreadsheetId - ID of the spreadsheet
|
* @param spreadsheetId - ID of the spreadsheet
|
||||||
* @param range - Cell range to retrieve (default: A1:Z10)
|
* @param range - Optional cell range. If not provided, it will fetch the entire first sheet.
|
||||||
* @returns Sheet data as a 2D array
|
* @returns Sheet data as a 2D array
|
||||||
*/
|
*/
|
||||||
export async function getSpreadsheetData(
|
export async function getSpreadsheetData(
|
||||||
refreshToken: string,
|
refreshToken: string,
|
||||||
spreadsheetId: string,
|
spreadsheetId: string,
|
||||||
range: string = 'A1:Z10'
|
range?: string
|
||||||
): Promise<SheetData> {
|
): Promise<SheetData> {
|
||||||
const oauth = getAuthenticatedClient(refreshToken);
|
const oauth = getAuthenticatedClient(refreshToken);
|
||||||
const sheets = google.sheets({ version: 'v4', auth: oauth });
|
const sheets = google.sheets({ version: 'v4', auth: oauth });
|
||||||
|
|
||||||
const response = await sheets.spreadsheets.values.get({
|
let effectiveRange = range;
|
||||||
spreadsheetId,
|
|
||||||
range
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
// If no range is provided, get the name of the first sheet and use that as the range
|
||||||
values: response.data.values || []
|
// to fetch all its content.
|
||||||
};
|
if (!effectiveRange) {
|
||||||
|
try {
|
||||||
|
const info = await getSpreadsheetInfo(refreshToken, spreadsheetId);
|
||||||
|
const firstSheetName = info.sheets?.[0]?.properties?.title;
|
||||||
|
|
||||||
|
if (firstSheetName) {
|
||||||
|
// To use a sheet name as a range, it must be quoted if it contains spaces or special characters.
|
||||||
|
effectiveRange = `'${firstSheetName}'`;
|
||||||
|
} else {
|
||||||
|
// Fallback if sheet name can't be determined.
|
||||||
|
effectiveRange = 'A1:Z1000'; // A sensible default for a large preview
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to get sheet info for spreadsheet ${spreadsheetId}`, error);
|
||||||
|
// Fallback if the info call fails
|
||||||
|
effectiveRange = 'A1:Z1000';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await sheets.spreadsheets.values.get({
|
||||||
|
spreadsheetId,
|
||||||
|
range: effectiveRange
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
values: response.data.values || []
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2,17 +2,18 @@ import { json } from '@sveltejs/kit';
|
|||||||
import type { RequestHandler } from './$types';
|
import type { RequestHandler } from './$types';
|
||||||
import { googleSheetsServer } from '$lib/google/sheets/server.js';
|
import { googleSheetsServer } from '$lib/google/sheets/server.js';
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ params, request }) => {
|
export const GET: RequestHandler = async ({ params, request, url }) => {
|
||||||
try {
|
try {
|
||||||
const { sheetId } = params;
|
const { sheetId } = params;
|
||||||
const authHeader = request.headers.get('authorization');
|
const authHeader = request.headers.get('authorization');
|
||||||
|
const range = url.searchParams.get('range') || undefined;
|
||||||
|
|
||||||
if (!authHeader?.startsWith('Bearer ')) {
|
if (!authHeader?.startsWith('Bearer ')) {
|
||||||
return json({ error: 'Missing or invalid authorization header' }, { status: 401 });
|
return json({ error: 'Missing or invalid authorization header' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const refreshToken = authHeader.slice(7);
|
const refreshToken = authHeader.slice(7);
|
||||||
const sheetData = await googleSheetsServer.getSpreadsheetData(refreshToken, sheetId, 'A1:Z10');
|
const sheetData = await googleSheetsServer.getSpreadsheetData(refreshToken, sheetId, range);
|
||||||
|
|
||||||
return json(sheetData);
|
return json(sheetData);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -172,8 +172,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Use the new unified API endpoint
|
// Use the new unified API endpoint, requesting only a preview range
|
||||||
const response = await fetch(`/private/api/google/sheets/${sheet.id}/data`, {
|
const response = await fetch(`/private/api/google/sheets/${sheet.id}/data?range=A1:Z10`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${localStorage.getItem('google_refresh_token')}`
|
Authorization: `Bearer ${localStorage.getItem('google_refresh_token')}`
|
||||||
|
|||||||
@@ -115,6 +115,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
syncingParticipants = true;
|
syncingParticipants = true;
|
||||||
|
const previousCount = participants.length; // Capture count before sync
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Fetch sheet data
|
// Fetch sheet data
|
||||||
const response = await fetch(`/private/api/google/sheets/${event.sheet_id}/data`, {
|
const response = await fetch(`/private/api/google/sheets/${event.sheet_id}/data`, {
|
||||||
@@ -178,15 +180,22 @@
|
|||||||
// Reload participants
|
// Reload participants
|
||||||
await loadParticipants();
|
await loadParticipants();
|
||||||
|
|
||||||
// Show success message with count of synced participants
|
// Show success message with accurate count of changes
|
||||||
const previousCount = participants.length;
|
const newCount = participants.length;
|
||||||
const newCount = names.length;
|
const diff = newCount - previousCount;
|
||||||
const addedCount = Math.max(0, participants.length - previousCount);
|
const processedCount = names.length;
|
||||||
|
|
||||||
toast.success(
|
let message = `Sync complete. ${processedCount} confirmed entries processed from the sheet.`;
|
||||||
`Successfully synced participants. ${newCount} entries processed, ${addedCount} new participants added.`,
|
|
||||||
5000
|
if (diff > 0) {
|
||||||
);
|
message += ` ${diff} new participants added.`;
|
||||||
|
} else if (diff < 0) {
|
||||||
|
message += ` ${-diff} participants removed.`;
|
||||||
|
} else {
|
||||||
|
message += ` No changes to the participant list.`;
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success(message, 6000);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error syncing participants:', err);
|
console.error('Error syncing participants:', err);
|
||||||
toast.error(`Failed to sync participants: ${err instanceof Error ? err.message : 'Unknown error'}`);
|
toast.error(`Failed to sync participants: ${err instanceof Error ? err.message : 'Unknown error'}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user