diff --git a/plugins/google-sheets/src/App.tsx b/plugins/google-sheets/src/App.tsx index e703be8ba..b160db1fb 100644 --- a/plugins/google-sheets/src/App.tsx +++ b/plugins/google-sheets/src/App.tsx @@ -1,6 +1,7 @@ import { framer } from "framer-plugin" import { useEffect, useLayoutEffect, useState } from "react" import { + CollectionFieldType, getPluginContext, PluginContext, PluginContextUpdate, @@ -166,6 +167,14 @@ export function App({ pluginContext }: AppProps) { } = context const [headerRow] = sheet.values + const colFieldTypes: Record = {} + + // Determine if the field type is already configured, otherwise default to "string" + for (const colName of headerRow) { + const field = fields.find(field => field?.name === colName) + colFieldTypes[colName] = field?.type ?? "string" + } + syncSheet({ ignoredColumns, slugColumn, @@ -174,11 +183,7 @@ export function App({ pluginContext }: AppProps) { spreadsheetId, sheetTitle, fields, - // Determine if the field type is already configured, otherwise default to "string" - colFieldTypes: headerRow.map(colName => { - const field = fields.find(field => field?.name === colName) - return field?.type ?? "string" - }), + colFieldTypes, }).then(() => framer.closePlugin()) }, [context, shouldSyncOnly]) diff --git a/plugins/google-sheets/src/pages/MapSheetFields.tsx b/plugins/google-sheets/src/pages/MapSheetFields.tsx index c72abd8a6..4d94894a6 100644 --- a/plugins/google-sheets/src/pages/MapSheetFields.tsx +++ b/plugins/google-sheets/src/pages/MapSheetFields.tsx @@ -222,11 +222,17 @@ export function MapSheetFieldsPage({ return field }) + const colFieldTypes: Record = {} + + for (const field of allFields) { + colFieldTypes[field.id] = field.type + } + onSubmit({ fields: allFields, spreadsheetId, sheetTitle, - colFieldTypes: fieldConfig.map(field => field.type ?? "string"), + colFieldTypes, ignoredColumns: Array.from(disabledColumns), slugColumn, lastSyncedTime: getLastSyncedTime(pluginContext, slugColumn), diff --git a/plugins/google-sheets/src/sheets.ts b/plugins/google-sheets/src/sheets.ts index dcac31807..d301dda90 100644 --- a/plugins/google-sheets/src/sheets.ts +++ b/plugins/google-sheets/src/sheets.ts @@ -258,7 +258,7 @@ export interface SyncResult extends SyncStatus { } interface ProcessSheetRowParams { - fieldTypes: CollectionFieldType[] + fieldTypes: Record row: Row rowIndex: number uniqueHeaderRowNames: string[] @@ -275,7 +275,7 @@ export interface SyncMutationOptions { fields: ManagedCollectionField[] slugColumn: string | null ignoredColumns: string[] - colFieldTypes: CollectionFieldType[] + colFieldTypes: Record lastSyncedTime: string | null } @@ -331,7 +331,11 @@ function getFieldValue(fieldType: CollectionFieldType, cellValue: CellValue) { case "formattedText": case "color": case "string": { - return String(cellValue) + if (typeof cellValue === "string") { + return cellValue + } + + return "" } default: return null @@ -355,18 +359,7 @@ function processSheetRow({ for (const [colIndex, cell] of row.entries()) { if (ignoredFieldColumnIndexes.includes(colIndex)) continue - // +1 as zero-indexed, another +1 to account for header row - const location = columnToLetter(colIndex + 1) + (rowIndex + 2) - - const fieldValue = getFieldValue(fieldTypes[colIndex], cell) - - if (fieldValue === null) { - status.warnings.push({ - rowIndex, - message: `Invalid cell value at ${location}.`, - }) - continue - } + const fieldValue = getFieldValue(fieldTypes[uniqueHeaderRowNames[colIndex]], cell) if (colIndex === slugFieldColumnIndex) { if (typeof fieldValue !== "string") { @@ -392,6 +385,16 @@ function processSheetRow({ return null } + for (const headerRowName of uniqueHeaderRowNames) { + if (!(headerRowName in fieldData)) { + if (["string", "formattedText"].includes(fieldTypes[headerRowName])) { + fieldData[headerRowName] = "" + } else { + fieldData[headerRowName] = null + } + } + } + return { id: itemId, slug: slugValue,