mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-28 03:00:29 -04:00
feat(turbo): restructured repo to be a standard turborepo monorepo (#341)
* added turborepo * finished turbo migration * updated gitignore * use dotenv & run format * fixed error in docs * remove standalone deployment in prod * fix ts error, remove ignore ts errors during build * added formatter to the end of the docs generator
This commit is contained in:
216
apps/sim/tools/google_sheets/append.ts
Normal file
216
apps/sim/tools/google_sheets/append.ts
Normal file
@@ -0,0 +1,216 @@
|
||||
import { ToolConfig } from '../types'
|
||||
import { GoogleSheetsAppendResponse, GoogleSheetsToolParams } from './types'
|
||||
|
||||
export const appendTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsAppendResponse> = {
|
||||
id: 'google_sheets_append',
|
||||
name: 'Append to Google Sheets',
|
||||
description: 'Append data to the end of a Google Sheets spreadsheet',
|
||||
version: '1.0',
|
||||
oauth: {
|
||||
required: true,
|
||||
provider: 'google-sheets',
|
||||
additionalScopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
||||
},
|
||||
params: {
|
||||
accessToken: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The access token for the Google Sheets API',
|
||||
},
|
||||
spreadsheetId: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The ID of the spreadsheet to append to',
|
||||
},
|
||||
range: { type: 'string', required: false, description: 'The range of cells to append after' },
|
||||
values: { type: 'array', required: true, description: 'The data to append to the spreadsheet' },
|
||||
valueInputOption: {
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'The format of the data to append',
|
||||
},
|
||||
insertDataOption: {
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'How to insert the data (OVERWRITE or INSERT_ROWS)',
|
||||
},
|
||||
includeValuesInResponse: {
|
||||
type: 'boolean',
|
||||
required: false,
|
||||
description: 'Whether to include the appended values in the response',
|
||||
},
|
||||
},
|
||||
request: {
|
||||
url: (params) => {
|
||||
// If range is not provided, use a default range for the first sheet
|
||||
const range = params.range || 'Sheet1'
|
||||
|
||||
const url = new URL(
|
||||
`https://sheets.googleapis.com/v4/spreadsheets/${params.spreadsheetId}/values/${encodeURIComponent(range)}:append`
|
||||
)
|
||||
|
||||
// Default to USER_ENTERED if not specified
|
||||
const valueInputOption = params.valueInputOption || 'USER_ENTERED'
|
||||
url.searchParams.append('valueInputOption', valueInputOption)
|
||||
|
||||
// Default to INSERT_ROWS if not specified
|
||||
if (params.insertDataOption) {
|
||||
url.searchParams.append('insertDataOption', params.insertDataOption)
|
||||
}
|
||||
|
||||
if (params.includeValuesInResponse) {
|
||||
url.searchParams.append('includeValuesInResponse', 'true')
|
||||
}
|
||||
|
||||
return url.toString()
|
||||
},
|
||||
method: 'POST',
|
||||
headers: (params) => ({
|
||||
Authorization: `Bearer ${params.accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
body: (params) => {
|
||||
let processedValues: any = params.values || []
|
||||
|
||||
// Handle case where values might be a string (potentially JSON string)
|
||||
if (typeof processedValues === 'string') {
|
||||
try {
|
||||
// Try to parse it as JSON
|
||||
processedValues = JSON.parse(processedValues)
|
||||
} catch (error) {
|
||||
// If the input contains literal newlines causing JSON parse to fail,
|
||||
// try a more robust approach
|
||||
try {
|
||||
// Replace literal newlines with escaped newlines for JSON parsing
|
||||
const sanitizedInput = (processedValues as string)
|
||||
.replace(/\n/g, '\\n')
|
||||
.replace(/\r/g, '\\r')
|
||||
.replace(/\t/g, '\\t')
|
||||
// Fix any double backslashes that might occur
|
||||
.replace(/\\\\/g, '\\')
|
||||
|
||||
// Try to parse again with sanitized input
|
||||
processedValues = JSON.parse(sanitizedInput)
|
||||
} catch (secondError) {
|
||||
// If all parsing attempts fail, wrap as a single cell value
|
||||
processedValues = [[processedValues]]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// New logic to handle array of objects
|
||||
if (
|
||||
Array.isArray(processedValues) &&
|
||||
processedValues.length > 0 &&
|
||||
typeof processedValues[0] === 'object' &&
|
||||
!Array.isArray(processedValues[0])
|
||||
) {
|
||||
// It's an array of objects
|
||||
|
||||
// First, extract all unique keys from all objects to create headers
|
||||
const allKeys = new Set<string>()
|
||||
processedValues.forEach((obj: any) => {
|
||||
if (obj && typeof obj === 'object') {
|
||||
Object.keys(obj).forEach((key) => allKeys.add(key))
|
||||
}
|
||||
})
|
||||
const headers = Array.from(allKeys)
|
||||
|
||||
// Then create rows with object values in the order of headers
|
||||
const rows = processedValues.map((obj: any) => {
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
// Handle non-object items by creating an array with empty values
|
||||
return Array(headers.length).fill('')
|
||||
}
|
||||
return headers.map((key) => {
|
||||
const value = obj[key]
|
||||
// Handle nested objects/arrays by converting to JSON string
|
||||
if (value !== null && typeof value === 'object') {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
return value === undefined ? '' : value
|
||||
})
|
||||
})
|
||||
|
||||
// Add headers as the first row, then add data rows
|
||||
processedValues = [headers, ...rows]
|
||||
}
|
||||
// Continue with existing logic for other array types
|
||||
else if (!Array.isArray(processedValues)) {
|
||||
processedValues = [[String(processedValues)]]
|
||||
} else if (!processedValues.every((item: any) => Array.isArray(item))) {
|
||||
// If it's an array but not all elements are arrays, wrap each element
|
||||
processedValues = (processedValues as any[]).map((row: any) =>
|
||||
Array.isArray(row) ? row : [String(row)]
|
||||
)
|
||||
}
|
||||
|
||||
const body: Record<string, any> = {
|
||||
majorDimension: params.majorDimension || 'ROWS',
|
||||
values: processedValues,
|
||||
}
|
||||
|
||||
// Only include range if it's provided
|
||||
if (params.range) {
|
||||
body.range = params.range
|
||||
}
|
||||
|
||||
return body
|
||||
},
|
||||
},
|
||||
transformResponse: async (response: Response) => {
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text()
|
||||
throw new Error(`Failed to append data to Google Sheets: ${errorText}`)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
// Extract spreadsheet ID from the URL
|
||||
const urlParts = response.url.split('/spreadsheets/')
|
||||
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
|
||||
|
||||
// Create a simple metadata object with just the ID and URL
|
||||
const metadata = {
|
||||
spreadsheetId,
|
||||
properties: {},
|
||||
spreadsheetUrl: `https://docs.google.com/spreadsheets/d/${spreadsheetId}`,
|
||||
}
|
||||
|
||||
const result = {
|
||||
success: true,
|
||||
output: {
|
||||
tableRange: data.tableRange || '',
|
||||
updatedRange: data.updates?.updatedRange || '',
|
||||
updatedRows: data.updates?.updatedRows || 0,
|
||||
updatedColumns: data.updates?.updatedColumns || 0,
|
||||
updatedCells: data.updates?.updatedCells || 0,
|
||||
metadata: {
|
||||
spreadsheetId: metadata.spreadsheetId,
|
||||
spreadsheetUrl: metadata.spreadsheetUrl,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return result
|
||||
},
|
||||
transformError: (error) => {
|
||||
// If it's an Error instance with a message, use that
|
||||
if (error instanceof Error) {
|
||||
return error.message
|
||||
}
|
||||
|
||||
// If it's an object with an error or message property
|
||||
if (typeof error === 'object' && error !== null) {
|
||||
if (error.error) {
|
||||
return typeof error.error === 'string' ? error.error : JSON.stringify(error.error)
|
||||
}
|
||||
if (error.message) {
|
||||
return error.message
|
||||
}
|
||||
}
|
||||
|
||||
// Default fallback message
|
||||
return 'An error occurred while appending to Google Sheets'
|
||||
},
|
||||
}
|
||||
9
apps/sim/tools/google_sheets/index.ts
Normal file
9
apps/sim/tools/google_sheets/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { appendTool } from './append'
|
||||
import { readTool } from './read'
|
||||
import { updateTool } from './update'
|
||||
import { writeTool } from './write'
|
||||
|
||||
export const sheetsReadTool = readTool
|
||||
export const sheetsWriteTool = writeTool
|
||||
export const sheetsUpdateTool = updateTool
|
||||
export const sheetsAppendTool = appendTool
|
||||
132
apps/sim/tools/google_sheets/read.ts
Normal file
132
apps/sim/tools/google_sheets/read.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
import { ToolConfig } from '../types'
|
||||
import { GoogleSheetsReadResponse, GoogleSheetsToolParams } from './types'
|
||||
|
||||
export const readTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsReadResponse> = {
|
||||
id: 'google_sheets_read',
|
||||
name: 'Read from Google Sheets',
|
||||
description: 'Read data from a Google Sheets spreadsheet',
|
||||
version: '1.0',
|
||||
oauth: {
|
||||
required: true,
|
||||
provider: 'google-sheets',
|
||||
additionalScopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
||||
},
|
||||
params: {
|
||||
accessToken: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The access token for the Google Sheets API',
|
||||
},
|
||||
spreadsheetId: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The ID of the spreadsheet to read from',
|
||||
},
|
||||
range: { type: 'string', required: false, description: 'The range of cells to read from' },
|
||||
},
|
||||
request: {
|
||||
url: (params) => {
|
||||
// Ensure spreadsheetId is valid
|
||||
const spreadsheetId = params.spreadsheetId?.trim()
|
||||
if (!spreadsheetId) {
|
||||
throw new Error('Spreadsheet ID is required')
|
||||
}
|
||||
|
||||
// If no range is provided, get all values from the first sheet
|
||||
if (!params.range) {
|
||||
return `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/Sheet1!A1:Z1000`
|
||||
}
|
||||
|
||||
// Otherwise, get values from the specified range
|
||||
return `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${encodeURIComponent(params.range)}`
|
||||
},
|
||||
method: 'GET',
|
||||
headers: (params) => {
|
||||
// Validate access token
|
||||
if (!params.accessToken) {
|
||||
throw new Error('Access token is required')
|
||||
}
|
||||
|
||||
return {
|
||||
Authorization: `Bearer ${params.accessToken}`,
|
||||
}
|
||||
},
|
||||
},
|
||||
transformResponse: async (response: Response) => {
|
||||
if (!response.ok) {
|
||||
const errorJson = await response.json().catch(() => ({ error: response.statusText }))
|
||||
const errorText =
|
||||
errorJson.error && typeof errorJson.error === 'object'
|
||||
? errorJson.error.message || JSON.stringify(errorJson.error)
|
||||
: errorJson.error || response.statusText
|
||||
throw new Error(`Failed to read Google Sheets data: ${errorText}`)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
// Extract spreadsheet ID from the URL
|
||||
const urlParts = response.url.split('/spreadsheets/')
|
||||
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
|
||||
|
||||
// Create a simple metadata object with just the ID and URL
|
||||
const metadata = {
|
||||
spreadsheetId,
|
||||
properties: {},
|
||||
spreadsheetUrl: `https://docs.google.com/spreadsheets/d/${spreadsheetId}`,
|
||||
}
|
||||
|
||||
// Process the values response
|
||||
const result: GoogleSheetsReadResponse = {
|
||||
success: true,
|
||||
output: {
|
||||
data: {
|
||||
range: data.range || '',
|
||||
values: data.values || [],
|
||||
},
|
||||
metadata: {
|
||||
spreadsheetId: metadata.spreadsheetId,
|
||||
spreadsheetUrl: metadata.spreadsheetUrl,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return result
|
||||
},
|
||||
transformError: (error) => {
|
||||
// If it's an Error instance with a message, use that
|
||||
if (error instanceof Error) {
|
||||
return error.message
|
||||
}
|
||||
|
||||
// If it's an object with an error or message property
|
||||
if (typeof error === 'object' && error !== null) {
|
||||
// Handle Google API error response format
|
||||
if (error.error) {
|
||||
if (typeof error.error === 'string') {
|
||||
return error.error
|
||||
}
|
||||
// Google API often returns error objects with error.error.message
|
||||
if (typeof error.error === 'object' && error.error.message) {
|
||||
return error.error.message
|
||||
}
|
||||
// If error.error is an object but doesn't have a message property
|
||||
return JSON.stringify(error.error)
|
||||
}
|
||||
|
||||
if (error.message) {
|
||||
return error.message
|
||||
}
|
||||
|
||||
// If we have a complex object, stringify it for debugging
|
||||
try {
|
||||
return `Google Sheets API error: ${JSON.stringify(error)}`
|
||||
} catch (e) {
|
||||
// In case the error object can't be stringified (e.g., circular references)
|
||||
return 'Google Sheets API error: Unable to parse error details'
|
||||
}
|
||||
}
|
||||
|
||||
// Default fallback message
|
||||
return 'An error occurred while reading from Google Sheets'
|
||||
},
|
||||
}
|
||||
71
apps/sim/tools/google_sheets/types.ts
Normal file
71
apps/sim/tools/google_sheets/types.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { ToolResponse } from '../types'
|
||||
|
||||
export interface GoogleSheetsRange {
|
||||
sheetId?: number
|
||||
sheetName?: string
|
||||
range: string
|
||||
values: any[][]
|
||||
}
|
||||
|
||||
export interface GoogleSheetsMetadata {
|
||||
spreadsheetId: string
|
||||
spreadsheetUrl?: string
|
||||
title?: string
|
||||
sheets?: {
|
||||
sheetId: number
|
||||
title: string
|
||||
index: number
|
||||
rowCount?: number
|
||||
columnCount?: number
|
||||
}[]
|
||||
}
|
||||
|
||||
export interface GoogleSheetsReadResponse extends ToolResponse {
|
||||
output: {
|
||||
data: GoogleSheetsRange
|
||||
metadata: GoogleSheetsMetadata
|
||||
}
|
||||
}
|
||||
|
||||
export interface GoogleSheetsWriteResponse extends ToolResponse {
|
||||
output: {
|
||||
updatedRange: string
|
||||
updatedRows: number
|
||||
updatedColumns: number
|
||||
updatedCells: number
|
||||
metadata: GoogleSheetsMetadata
|
||||
}
|
||||
}
|
||||
|
||||
export interface GoogleSheetsUpdateResponse extends ToolResponse {
|
||||
output: {
|
||||
updatedRange: string
|
||||
updatedRows: number
|
||||
updatedColumns: number
|
||||
updatedCells: number
|
||||
metadata: GoogleSheetsMetadata
|
||||
}
|
||||
}
|
||||
|
||||
export interface GoogleSheetsAppendResponse extends ToolResponse {
|
||||
output: {
|
||||
tableRange: string
|
||||
updatedRange: string
|
||||
updatedRows: number
|
||||
updatedColumns: number
|
||||
updatedCells: number
|
||||
metadata: GoogleSheetsMetadata
|
||||
}
|
||||
}
|
||||
|
||||
export interface GoogleSheetsToolParams {
|
||||
accessToken: string
|
||||
spreadsheetId: string
|
||||
range?: string
|
||||
values?: any[][]
|
||||
valueInputOption?: 'RAW' | 'USER_ENTERED'
|
||||
insertDataOption?: 'OVERWRITE' | 'INSERT_ROWS'
|
||||
includeValuesInResponse?: boolean
|
||||
responseValueRenderOption?: 'FORMATTED_VALUE' | 'UNFORMATTED_VALUE' | 'FORMULA'
|
||||
majorDimension?: 'ROWS' | 'COLUMNS'
|
||||
}
|
||||
170
apps/sim/tools/google_sheets/update.ts
Normal file
170
apps/sim/tools/google_sheets/update.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import { ToolConfig } from '../types'
|
||||
import { GoogleSheetsToolParams, GoogleSheetsUpdateResponse } from './types'
|
||||
|
||||
export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateResponse> = {
|
||||
id: 'google_sheets_update',
|
||||
name: 'Update Google Sheets',
|
||||
description: 'Update data in a Google Sheets spreadsheet',
|
||||
version: '1.0',
|
||||
oauth: {
|
||||
required: true,
|
||||
provider: 'google-sheets',
|
||||
additionalScopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
||||
},
|
||||
params: {
|
||||
accessToken: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The access token for the Google Sheets API',
|
||||
},
|
||||
spreadsheetId: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The ID of the spreadsheet to update',
|
||||
},
|
||||
range: { type: 'string', required: false, description: 'The range of cells to update' },
|
||||
values: { type: 'array', required: true, description: 'The data to update in the spreadsheet' },
|
||||
valueInputOption: {
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'The format of the data to update',
|
||||
},
|
||||
includeValuesInResponse: {
|
||||
type: 'boolean',
|
||||
required: false,
|
||||
description: 'Whether to include the updated values in the response',
|
||||
},
|
||||
},
|
||||
request: {
|
||||
url: (params) => {
|
||||
// If range is not provided, use a default range for the first sheet, second row to preserve headers
|
||||
const range = params.range || 'Sheet1!A2'
|
||||
|
||||
const url = new URL(
|
||||
`https://sheets.googleapis.com/v4/spreadsheets/${params.spreadsheetId}/values/${encodeURIComponent(range)}`
|
||||
)
|
||||
|
||||
// Default to USER_ENTERED if not specified
|
||||
const valueInputOption = params.valueInputOption || 'USER_ENTERED'
|
||||
url.searchParams.append('valueInputOption', valueInputOption)
|
||||
|
||||
if (params.includeValuesInResponse) {
|
||||
url.searchParams.append('includeValuesInResponse', 'true')
|
||||
}
|
||||
|
||||
return url.toString()
|
||||
},
|
||||
method: 'PUT',
|
||||
headers: (params) => ({
|
||||
Authorization: `Bearer ${params.accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
body: (params) => {
|
||||
let processedValues: any = params.values || []
|
||||
|
||||
// Handle array of objects
|
||||
if (
|
||||
Array.isArray(processedValues) &&
|
||||
processedValues.length > 0 &&
|
||||
typeof processedValues[0] === 'object' &&
|
||||
!Array.isArray(processedValues[0])
|
||||
) {
|
||||
// It's an array of objects
|
||||
|
||||
// First, extract all unique keys from all objects to create headers
|
||||
const allKeys = new Set<string>()
|
||||
processedValues.forEach((obj: any) => {
|
||||
if (obj && typeof obj === 'object') {
|
||||
Object.keys(obj).forEach((key) => allKeys.add(key))
|
||||
}
|
||||
})
|
||||
const headers = Array.from(allKeys)
|
||||
|
||||
// Then create rows with object values in the order of headers
|
||||
const rows = processedValues.map((obj: any) => {
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
// Handle non-object items by creating an array with empty values
|
||||
return Array(headers.length).fill('')
|
||||
}
|
||||
return headers.map((key) => {
|
||||
const value = obj[key]
|
||||
// Handle nested objects/arrays by converting to JSON string
|
||||
if (value !== null && typeof value === 'object') {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
return value === undefined ? '' : value
|
||||
})
|
||||
})
|
||||
|
||||
// Add headers as the first row, then add data rows
|
||||
processedValues = [headers, ...rows]
|
||||
}
|
||||
|
||||
const body: Record<string, any> = {
|
||||
majorDimension: params.majorDimension || 'ROWS',
|
||||
values: processedValues,
|
||||
}
|
||||
|
||||
// Only include range if it's provided
|
||||
if (params.range) {
|
||||
body.range = params.range
|
||||
}
|
||||
|
||||
return body
|
||||
},
|
||||
},
|
||||
transformResponse: async (response: Response) => {
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text()
|
||||
throw new Error(`Failed to update data in Google Sheets: ${errorText}`)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
// Extract spreadsheet ID from the URL
|
||||
const urlParts = response.url.split('/spreadsheets/')
|
||||
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
|
||||
|
||||
// Create a simple metadata object with just the ID and URL
|
||||
const metadata = {
|
||||
spreadsheetId,
|
||||
properties: {},
|
||||
spreadsheetUrl: `https://docs.google.com/spreadsheets/d/${spreadsheetId}`,
|
||||
}
|
||||
|
||||
const result = {
|
||||
success: true,
|
||||
output: {
|
||||
updatedRange: data.updatedRange,
|
||||
updatedRows: data.updatedRows,
|
||||
updatedColumns: data.updatedColumns,
|
||||
updatedCells: data.updatedCells,
|
||||
metadata: {
|
||||
spreadsheetId: metadata.spreadsheetId,
|
||||
spreadsheetUrl: metadata.spreadsheetUrl,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return result
|
||||
},
|
||||
transformError: (error) => {
|
||||
// If it's an Error instance with a message, use that
|
||||
if (error instanceof Error) {
|
||||
return error.message
|
||||
}
|
||||
|
||||
// If it's an object with an error or message property
|
||||
if (typeof error === 'object' && error !== null) {
|
||||
if (error.error) {
|
||||
return typeof error.error === 'string' ? error.error : JSON.stringify(error.error)
|
||||
}
|
||||
if (error.message) {
|
||||
return error.message
|
||||
}
|
||||
}
|
||||
|
||||
// Default fallback message
|
||||
return 'An error occurred while updating Google Sheets'
|
||||
},
|
||||
}
|
||||
170
apps/sim/tools/google_sheets/write.ts
Normal file
170
apps/sim/tools/google_sheets/write.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import { ToolConfig } from '../types'
|
||||
import { GoogleSheetsToolParams, GoogleSheetsWriteResponse } from './types'
|
||||
|
||||
export const writeTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsWriteResponse> = {
|
||||
id: 'google_sheets_write',
|
||||
name: 'Write to Google Sheets',
|
||||
description: 'Write data to a Google Sheets spreadsheet',
|
||||
version: '1.0',
|
||||
oauth: {
|
||||
required: true,
|
||||
provider: 'google-sheets',
|
||||
additionalScopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
||||
},
|
||||
params: {
|
||||
accessToken: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The access token for the Google Sheets API',
|
||||
},
|
||||
spreadsheetId: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The ID of the spreadsheet to write to',
|
||||
},
|
||||
range: { type: 'string', required: false, description: 'The range of cells to write to' },
|
||||
values: { type: 'array', required: true, description: 'The data to write to the spreadsheet' },
|
||||
valueInputOption: {
|
||||
type: 'string',
|
||||
required: false,
|
||||
description: 'The format of the data to write',
|
||||
},
|
||||
includeValuesInResponse: {
|
||||
type: 'boolean',
|
||||
required: false,
|
||||
description: 'Whether to include the written values in the response',
|
||||
},
|
||||
},
|
||||
request: {
|
||||
url: (params) => {
|
||||
// If range is not provided, use a default range for the first sheet, second row to preserve headers
|
||||
const range = params.range || 'Sheet1!A2'
|
||||
|
||||
const url = new URL(
|
||||
`https://sheets.googleapis.com/v4/spreadsheets/${params.spreadsheetId}/values/${encodeURIComponent(range)}`
|
||||
)
|
||||
|
||||
// Default to USER_ENTERED if not specified
|
||||
const valueInputOption = params.valueInputOption || 'USER_ENTERED'
|
||||
url.searchParams.append('valueInputOption', valueInputOption)
|
||||
|
||||
if (params.includeValuesInResponse) {
|
||||
url.searchParams.append('includeValuesInResponse', 'true')
|
||||
}
|
||||
|
||||
return url.toString()
|
||||
},
|
||||
method: 'PUT',
|
||||
headers: (params) => ({
|
||||
Authorization: `Bearer ${params.accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
body: (params) => {
|
||||
let processedValues: any = params.values || []
|
||||
|
||||
// Handle array of objects
|
||||
if (
|
||||
Array.isArray(processedValues) &&
|
||||
processedValues.length > 0 &&
|
||||
typeof processedValues[0] === 'object' &&
|
||||
!Array.isArray(processedValues[0])
|
||||
) {
|
||||
// It's an array of objects
|
||||
|
||||
// First, extract all unique keys from all objects to create headers
|
||||
const allKeys = new Set<string>()
|
||||
processedValues.forEach((obj: any) => {
|
||||
if (obj && typeof obj === 'object') {
|
||||
Object.keys(obj).forEach((key) => allKeys.add(key))
|
||||
}
|
||||
})
|
||||
const headers = Array.from(allKeys)
|
||||
|
||||
// Then create rows with object values in the order of headers
|
||||
const rows = processedValues.map((obj: any) => {
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
// Handle non-object items by creating an array with empty values
|
||||
return Array(headers.length).fill('')
|
||||
}
|
||||
return headers.map((key) => {
|
||||
const value = obj[key]
|
||||
// Handle nested objects/arrays by converting to JSON string
|
||||
if (value !== null && typeof value === 'object') {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
return value === undefined ? '' : value
|
||||
})
|
||||
})
|
||||
|
||||
// Add headers as the first row, then add data rows
|
||||
processedValues = [headers, ...rows]
|
||||
}
|
||||
|
||||
const body: Record<string, any> = {
|
||||
majorDimension: params.majorDimension || 'ROWS',
|
||||
values: processedValues,
|
||||
}
|
||||
|
||||
// Only include range if it's provided
|
||||
if (params.range) {
|
||||
body.range = params.range
|
||||
}
|
||||
|
||||
return body
|
||||
},
|
||||
},
|
||||
transformResponse: async (response: Response) => {
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text()
|
||||
throw new Error(`Failed to write data to Google Sheets: ${errorText}`)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
// Extract spreadsheet ID from the URL
|
||||
const urlParts = response.url.split('/spreadsheets/')
|
||||
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
|
||||
|
||||
// Create a simple metadata object with just the ID and URL
|
||||
const metadata = {
|
||||
spreadsheetId,
|
||||
properties: {},
|
||||
spreadsheetUrl: `https://docs.google.com/spreadsheets/d/${spreadsheetId}`,
|
||||
}
|
||||
|
||||
const result = {
|
||||
success: true,
|
||||
output: {
|
||||
updatedRange: data.updatedRange,
|
||||
updatedRows: data.updatedRows,
|
||||
updatedColumns: data.updatedColumns,
|
||||
updatedCells: data.updatedCells,
|
||||
metadata: {
|
||||
spreadsheetId: metadata.spreadsheetId,
|
||||
spreadsheetUrl: metadata.spreadsheetUrl,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return result
|
||||
},
|
||||
transformError: (error) => {
|
||||
// If it's an Error instance with a message, use that
|
||||
if (error instanceof Error) {
|
||||
return error.message
|
||||
}
|
||||
|
||||
// If it's an object with an error or message property
|
||||
if (typeof error === 'object' && error !== null) {
|
||||
if (error.error) {
|
||||
return typeof error.error === 'string' ? error.error : JSON.stringify(error.error)
|
||||
}
|
||||
if (error.message) {
|
||||
return error.message
|
||||
}
|
||||
}
|
||||
|
||||
// Default fallback message
|
||||
return 'An error occurred while writing to Google Sheets'
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user