Added search to sheets

This commit is contained in:
Roman Krček
2025-07-08 13:07:24 +02:00
parent bd7e3f9720
commit 4d71bf5410
5 changed files with 266 additions and 19 deletions

View File

@@ -1,6 +1,19 @@
import { google } from 'googleapis';
import { getAuthenticatedClient } from '../auth/server.js';
import { GoogleSheet } from './types.ts'
import { GoogleSheet } from './types.ts';
// Type for sheet data
export interface SheetData {
values: string[][];
}
// Server-side Google Sheets API handler
export const googleSheetsServer = {
getRecentSpreadsheets,
getSpreadsheetData,
getSpreadsheetInfo,
searchSheets
};
/**
* Get a list of recent Google Sheets
@@ -77,3 +90,38 @@ export async function getSpreadsheetInfo(
return response.data;
}
/**
* Search for Google Sheets by name
* @param refreshToken - Google refresh token
* @param query - Search query
* @param limit - Maximum number of sheets to return
* @returns List of Google Sheets matching the query
*/
export async function searchSheets(
refreshToken: string,
query: string,
limit: number = 20
): Promise<GoogleSheet[]> {
const oauth = getAuthenticatedClient(refreshToken);
const drive = google.drive({ version: 'v3', auth: oauth });
// Create a query to search for spreadsheets with names containing the search term
const q = `mimeType='application/vnd.google-apps.spreadsheet' and name contains '${query}'`;
const response = await drive.files.list({
q,
orderBy: 'modifiedTime desc',
pageSize: limit,
fields: 'files(id,name,modifiedTime,webViewLink)'
});
return (
response.data.files?.map(file => ({
id: file.id!, // eslint-disable-line @typescript-eslint/no-non-null-assertion
name: file.name!,
modifiedTime: file.modifiedTime!,
webViewLink: file.webViewLink!
})) || []
);
}