diff --git a/lib/onboarding.js b/lib/onboarding.js index 86b6e93..7e0e2c7 100644 --- a/lib/onboarding.js +++ b/lib/onboarding.js @@ -78,6 +78,33 @@ async function askOptionalPositiveInteger(ask, question, defaultValue) { } } +function extractSpreadsheetId(input) { + const value = (input || '').trim(); + if (!value) { + return ''; + } + + const urlMatch = value.match(/\/spreadsheets\/d\/([a-zA-Z0-9-_]+)/i); + if (urlMatch && urlMatch[1]) { + return urlMatch[1]; + } + + return value; +} + +async function askSpreadsheetIdOrUrl(ask, question) { + while (true) { + const answer = await ask(question); + const documentId = extractSpreadsheetId(answer); + + if (documentId) { + return documentId; + } + + console.log('Please enter a Google Sheets URL or spreadsheet ID.'); + } +} + async function pauseForEnter(ask) { await ask('Press Enter to close this window...'); } @@ -176,7 +203,10 @@ async function runOnboarding() { const documentCount = await askPositiveInteger(ask, 'How many documents do you want to pull from? '); for (let documentIndex = 0; documentIndex < documentCount; documentIndex += 1) { - const documentId = await askNonEmpty(ask, `Enter document ID ${documentIndex + 1}: `); + const documentId = await askSpreadsheetIdOrUrl( + ask, + `Enter Google Sheets URL or document ID ${documentIndex + 1}: ` + ); let documentTitle = documentId; try {