test-2d-structure.jsโข5.33 kB
#!/usr/bin/env node
/**
* Test script to verify the new 2D array structure
* This tests the updated format with metadata and cells array
*/
import { GoogleSheetsService } from '../server/sheets.js';
import { config as dotenvConfig } from 'dotenv';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readFileSync } from 'fs';
// Get the directory of this script
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Load environment variables
dotenvConfig({ path: join(__dirname, '../.env') });
// Test Google Sheets URL (public Google Sheets template)
const TEST_SHEET_URL = 'https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit';
async function test2DStructure() {
console.log('๐งช Testing new 2D array structure...\n');
try {
// Load credentials from JSON file
const credentialsPath = process.env.GOOGLE_CREDENTIALS_JSON_FILE;
if (!credentialsPath) {
throw new Error('Missing GOOGLE_CREDENTIALS_JSON_FILE environment variable. Please set it to the path of your service account JSON file.');
}
console.log('๐ Loading credentials from JSON file...');
const credentialsContent = readFileSync(credentialsPath, 'utf8');
const credentials = JSON.parse(credentialsContent);
const serviceAccountEmail = credentials.client_email;
const privateKey = credentials.private_key;
const projectId = credentials.project_id;
if (!serviceAccountEmail || !privateKey || !projectId) {
throw new Error('Invalid credentials JSON file. Missing required fields: client_email, private_key, or project_id.');
}
// Initialize service
const sheetsService = new GoogleSheetsService({
serviceAccountEmail,
privateKey,
projectId,
});
await sheetsService.initialize();
console.log('โ
Google Sheets service initialized');
// Get spreadsheet summary first
console.log('\n๐ Getting spreadsheet summary...');
const summary = await sheetsService.getSpreadsheetSummary(TEST_SHEET_URL);
const firstSheetName = summary.sheetNames[0].name;
console.log(`๐ Testing with sheet: "${firstSheetName}"`);
// Get data in new 2D format
console.log('\n๐ Getting sheet data in new 2D format...');
const sheetData = await sheetsService.getSheetData(TEST_SHEET_URL, firstSheetName);
console.log('โ
Successfully retrieved data in new format!');
// Verify structure
console.log('\n๐ Verifying structure...');
console.log(`๐ Spreadsheet: ${sheetData.spreadsheetTitle}`);
console.log(`๐ Sheet: ${sheetData.metadata.title}`);
console.log(`๐ Dimensions: ${sheetData.metadata.dimensions.rows} rows ร ${sheetData.metadata.dimensions.columns} columns`);
console.log(`๐ Cells with data: ${sheetData.cells.length}`);
// Show sample cells in new format
console.log('\n๐ Sample cells in new 2D format:');
const sampleCells = sheetData.cells.slice(0, 10);
sampleCells.forEach((cell, index) => {
const posStr = `[${cell.pos[0]},${cell.pos[1]}]`;
const valStr = typeof cell.val === 'string' ? `"${cell.val}"` : cell.val;
const fmtStr = cell.fmt ? ` (formatted: "${cell.fmt}")` : '';
console.log(` ${index + 1}. pos: ${posStr}, val: ${valStr}, type: ${cell.type}${fmtStr}`);
});
// Test type detection
console.log('\n๐ Type distribution:');
const typeCounts = {};
sheetData.cells.forEach(cell => {
typeCounts[cell.type] = (typeCounts[cell.type] || 0) + 1;
});
Object.entries(typeCounts).forEach(([type, count]) => {
console.log(` ${type}: ${count} cells`);
});
// Show examples of different types
console.log('\n๐ Examples by type:');
const typeExamples = {};
sheetData.cells.forEach(cell => {
if (!typeExamples[cell.type]) {
typeExamples[cell.type] = cell;
}
});
Object.entries(typeExamples).forEach(([type, cell]) => {
const posStr = `[${cell.pos[0]},${cell.pos[1]}]`;
const valStr = typeof cell.val === 'string' ? `"${cell.val}"` : cell.val;
const fmtStr = cell.fmt ? ` โ "${cell.fmt}"` : '';
console.log(` ${type}: ${valStr}${fmtStr} at ${posStr}`);
});
// Verify JSON serialization
console.log('\n๐ Testing JSON serialization...');
try {
const jsonString = JSON.stringify(sheetData, null, 2);
console.log('โ
Data is JSON-serializable');
console.log(`๐ JSON size: ${Math.round(jsonString.length / 1024)}KB`);
} catch (error) {
console.log('โ Data is not JSON-serializable:', error.message);
}
console.log('\n๐ 2D array structure test completed successfully!');
console.log('\n๐ก New structure benefits:');
console.log(' โ
More compact representation');
console.log(' โ
Preserves exact cell positions');
console.log(' โ
Automatic type detection');
console.log(' โ
Separates raw values from formatting');
console.log(' โ
Easy to process for AI analysis');
} catch (error) {
console.error('โ Test failed:', error.message);
console.error('Stack trace:', error.stack);
process.exit(1);
}
}
// Run the test
test2DStructure();