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
|
||||
* @param refreshToken - Google refresh token
|
||||
* @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
|
||||
*/
|
||||
export async function getSpreadsheetData(
|
||||
refreshToken: string,
|
||||
spreadsheetId: string,
|
||||
range: string = 'A1:Z10'
|
||||
refreshToken: string,
|
||||
spreadsheetId: string,
|
||||
range?: string
|
||||
): Promise<SheetData> {
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const sheets = google.sheets({ version: 'v4', auth: oauth });
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const sheets = google.sheets({ version: 'v4', auth: oauth });
|
||||
|
||||
const response = await sheets.spreadsheets.values.get({
|
||||
spreadsheetId,
|
||||
range
|
||||
});
|
||||
let effectiveRange = range;
|
||||
|
||||
return {
|
||||
values: response.data.values || []
|
||||
};
|
||||
// If no range is provided, get the name of the first sheet and use that as the range
|
||||
// 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 { googleSheetsServer } from '$lib/google/sheets/server.js';
|
||||
|
||||
export const GET: RequestHandler = async ({ params, request }) => {
|
||||
export const GET: RequestHandler = async ({ params, request, url }) => {
|
||||
try {
|
||||
const { sheetId } = params;
|
||||
const authHeader = request.headers.get('authorization');
|
||||
|
||||
const range = url.searchParams.get('range') || undefined;
|
||||
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
return json({ error: 'Missing or invalid authorization header' }, { status: 401 });
|
||||
}
|
||||
|
||||
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);
|
||||
} catch (error) {
|
||||
|
||||
@@ -172,8 +172,8 @@
|
||||
}
|
||||
|
||||
try {
|
||||
// Use the new unified API endpoint
|
||||
const response = await fetch(`/private/api/google/sheets/${sheet.id}/data`, {
|
||||
// Use the new unified API endpoint, requesting only a preview range
|
||||
const response = await fetch(`/private/api/google/sheets/${sheet.id}/data?range=A1:Z10`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${localStorage.getItem('google_refresh_token')}`
|
||||
|
||||
@@ -115,6 +115,8 @@
|
||||
}
|
||||
|
||||
syncingParticipants = true;
|
||||
const previousCount = participants.length; // Capture count before sync
|
||||
|
||||
try {
|
||||
// Fetch sheet data
|
||||
const response = await fetch(`/private/api/google/sheets/${event.sheet_id}/data`, {
|
||||
@@ -177,16 +179,23 @@
|
||||
|
||||
// Reload participants
|
||||
await loadParticipants();
|
||||
|
||||
// Show success message with count of synced participants
|
||||
const previousCount = participants.length;
|
||||
const newCount = names.length;
|
||||
const addedCount = Math.max(0, participants.length - previousCount);
|
||||
|
||||
toast.success(
|
||||
`Successfully synced participants. ${newCount} entries processed, ${addedCount} new participants added.`,
|
||||
5000
|
||||
);
|
||||
|
||||
// Show success message with accurate count of changes
|
||||
const newCount = participants.length;
|
||||
const diff = newCount - previousCount;
|
||||
const processedCount = names.length;
|
||||
|
||||
let message = `Sync complete. ${processedCount} confirmed entries processed from the sheet.`;
|
||||
|
||||
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) {
|
||||
console.error('Error syncing participants:', err);
|
||||
toast.error(`Failed to sync participants: ${err instanceof Error ? err.message : 'Unknown error'}`);
|
||||
|
||||
Reference in New Issue
Block a user