Skip to content

Commit

Permalink
Merge branch 'activepieces:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Rowe-FS authored Dec 14, 2024
2 parents 392fa14 + 301294b commit 89415f8
Show file tree
Hide file tree
Showing 23 changed files with 519 additions and 45 deletions.
4 changes: 2 additions & 2 deletions packages/pieces/community/google-sheets/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-google-sheets",
"version": "0.11.7"
}
"version": "0.11.9"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
OAuth2PropertyValue,
Property,
} from '@activepieces/pieces-framework';
import { Dimension, googleSheetsCommon, objectToArray, ValueInputOption } from '../common/common';
import { Dimension, googleSheetsCommon, objectToArray, ValueInputOption,columnToLabel } from '../common/common';
import { getAccessTokenOrThrow } from '@activepieces/pieces-common';
import { getWorkSheetName, getWorkSheetGridSize } from '../triggers/helpers';
import { google, sheets_v4 } from 'googleapis';
Expand Down Expand Up @@ -244,13 +244,15 @@ export const insertMultipleRowsAction = createAction({

const sheetHeaders = rowHeaders[0]?.values ?? {};

const formattedValues = formatInputRows(valuesInputType, rowValuesInput, sheetHeaders);
const valueInputOption = asString ? ValueInputOption.RAW : ValueInputOption.USER_ENTERED;

const authClient = new OAuth2Client();
authClient.setCredentials(context.auth);
const sheets = google.sheets({ version: 'v4', auth: authClient });

const formattedValues = await formatInputRows(sheets,spreadSheetId, sheetName,valuesInputType, rowValuesInput, sheetHeaders);

const valueInputOption = asString ? ValueInputOption.RAW : ValueInputOption.USER_ENTERED;


if (overwriteValues) {
const sheetGridRange = await getWorkSheetGridSize(context.auth, spreadSheetId, sheetId);
const existingGridRowCount = sheetGridRange.rowCount ?? 0;
Expand Down Expand Up @@ -379,19 +381,22 @@ async function normalInsert(
return response.data;
}

function formatInputRows(
async function formatInputRows(
sheets: sheets_v4.Sheets,
spreadSheetId: string,
sheetName: string,
valuesInputType: string,
rowValuesInput: any,
sheetHeaders: RowValueType
): RowValueType[] {
): Promise<RowValueType[]> {
let formattedInputRows: any[] = [];

switch (valuesInputType) {
case 'csv':
formattedInputRows = convertCsvToRawValues(rowValuesInput as string, ',', sheetHeaders);
break;
case 'json':
formattedInputRows = convertJsonToRawValues(rowValuesInput as string, sheetHeaders);
formattedInputRows = await convertJsonToRawValues(sheets,spreadSheetId, sheetName, rowValuesInput as string, sheetHeaders);
break;
case 'column_names':
formattedInputRows = rowValuesInput as RowValueType[];
Expand All @@ -401,7 +406,13 @@ function formatInputRows(
return formattedInputRows;
}

function convertJsonToRawValues(json: string | Record<string, any>[], labelHeaders: RowValueType): RowValueType[] {
async function convertJsonToRawValues(
sheets: sheets_v4.Sheets,
spreadSheetId: string,
sheetName: string,
json: string | Record<string, any>[],
labelHeaders: RowValueType
): Promise<RowValueType[]> {

let data: RowValueType[];

Expand All @@ -422,6 +433,35 @@ function convertJsonToRawValues(json: string | Record<string, any>[], labelHeade
throw new Error('Input must be an array of objects or a valid JSON string representing it.');
}

// Collect all possible headers from the data
const allHeaders = new Set<string>();
data.forEach((row) => {
Object.keys(row).forEach((key) => allHeaders.add(key));
});

// Identify headers not present in labelHeaders
const additionalHeaders = Array.from(allHeaders).filter(
(header) => !Object.values(labelHeaders).includes(header)
);

//add missing headers to labelHeaders
additionalHeaders.forEach((header) => {
labelHeaders[columnToLabel(Object.keys(labelHeaders).length)] = header;
});

// update sheets with new headers
if (additionalHeaders.length > 0) {
await sheets.spreadsheets.values.update({
range: `${sheetName}!A1:ZZZ1`,
spreadsheetId: spreadSheetId,
valueInputOption: ValueInputOption.USER_ENTERED,
requestBody: {
majorDimension:Dimension.ROWS,
values: [objectToArray(labelHeaders)]
}
});
}

return data.map((row: RowValueType) => {
return Object.entries(labelHeaders).reduce((acc, [labelColumn, csvHeader]) => {
acc[labelColumn] = row[csvHeader] ?? "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ export const updateRowAction = createAction({
});


console.log('formattedValues', formattedValues);
console.log('sheetName', sheetName);
console.log('rowValuesInput', rowValuesInput);
console.log('objectToArray', objectToArray(rowValuesInput));
if (formattedValues.length > 0) {
const response = await sheets.spreadsheets.values.update({
range: `${sheetName}!A${rowId}:ZZZ${rowId}`,
Expand Down
30 changes: 16 additions & 14 deletions packages/pieces/community/google-sheets/src/lib/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { isNil, isString } from '@activepieces/shared';
import { google } from 'googleapis';
import { OAuth2Client } from 'googleapis-common';
import { googleSheetsAuth } from '../../';
import { transformWorkSheetValues } from '../triggers/helpers';

export const googleSheetsCommon = {
baseUrl: 'https://sheets.googleapis.com/v4/spreadsheets',
Expand Down Expand Up @@ -306,7 +307,7 @@ async function getGoogleSheetRows({
if (rowIndex_s !== undefined && rowIndex_e !== undefined) {
range = `!A${rowIndex_s}:ZZZ${rowIndex_e}`;
}
const response = await httpClient.sendRequest<{ values: [string[]][] }>({
const rowsResponse = await httpClient.sendRequest<{ values: [string[]][] }>({
method: HttpMethod.GET,
url: `${googleSheetsCommon.baseUrl}/${spreadsheetId}/values/${sheetName}${range}`,
authentication: {
Expand All @@ -315,22 +316,23 @@ async function getGoogleSheetRows({
},
}
);
if (response.body.values === undefined) return [];
if (rowsResponse.body.values === undefined) return [];

const res = [];
for (let i = 0; i < response.body.values.length; i++) {
const values: any = {};
for (let j = 0; j < response.body.values[i].length; j++) {
values[columnToLabel(j)] = response.body.values[i][j];
}
const headerResponse = await httpClient.sendRequest<{ values: [string[]][] }>({
method: HttpMethod.GET,
url: `${googleSheetsCommon.baseUrl}/${spreadsheetId}/values/${sheetName}!A1:ZZZ1`,
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: accessToken,
},
});

res.push({
row: i + 1,
values,
});
}
const headers = headerResponse.body.values[0]??[];
const headerCount = headers.length;

const labeledRowValues = transformWorkSheetValues(rowsResponse.body.values,0,headerCount);

return res;
return labeledRowValues;
}

type GetHeaderRowProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ export function hashObject(obj: Record<string, unknown>): string {
return hash.digest('hex');
}

export function transformWorkSheetValues(rowValues: any[][], oldRowCount: number) {
export function transformWorkSheetValues(rowValues: any[][], oldRowCount: number,headerCount: number) {
const result = [];
for (let i = 0; i < rowValues.length; i++) {
const values: any = {};
for (let j = 0; j < rowValues[i].length; j++) {
values[columnToLabel(j)] = rowValues[i][j];
for (let j = 0; j < headerCount ; j++) {
values[columnToLabel(j)] = rowValues[i][j] ?? "";
}
result.push({
row: oldRowCount + i + 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil } from '@activepieces/shared';
import { isEmpty, isNil } from '@activepieces/shared';
import { googleSheetsAuth } from '../../';
import { columnToLabel, googleSheetsCommon, labelToColumn } from '../common/common';
import {
Expand Down Expand Up @@ -65,7 +65,10 @@ export const newOrUpdatedRowTrigger = createTrigger({
spreadSheetId,
`${sheetName}!1:1`,
);
const labeledRowValues = transformWorkSheetValues(firstRowValues, 0);

const headers = firstRowValues[0] ?? [];
const headerCount = headers.length;
const labeledRowValues = transformWorkSheetValues(firstRowValues, 0,headerCount);

const options: DropdownOption<string>[] = [{ label: 'All Columns', value: ALL_COLUMNS }];

Expand Down Expand Up @@ -163,6 +166,9 @@ export const newOrUpdatedRowTrigger = createTrigger({
*/
const currentValues = await getWorkSheetValues(context.auth, spreadSheetId, sheetName);

const headers = currentValues[0] ?? [];
const headerCount = headers.length;

// const rowCount = Math.max(oldValuesHashes.length, currentValues.length);

const changedValues = [];
Expand Down Expand Up @@ -199,13 +205,13 @@ export const newOrUpdatedRowTrigger = createTrigger({
continue;
}

const oldRowHash = oldValuesHashes[row];
const oldRowHash = row < oldValuesHashes.length ? oldValuesHashes[row] : undefined;

if (oldRowHash === undefined || oldRowHash != currentRowHash) {
const formattedValues: any = {};

for (let column = 0; column < currentValues[row].length; column++) {
formattedValues[columnToLabel(column)] = currentValues[row][column];
for (let column = 0; column < headerCount; column++) {
formattedValues[columnToLabel(column)] = currentValues[row][column] ?? "";
}

changedValues.push({
Expand Down Expand Up @@ -233,8 +239,11 @@ export const newOrUpdatedRowTrigger = createTrigger({
const sheetName = await getWorkSheetName(context.auth, spreadSheetId, sheetId);
const currentSheetValues = await getWorkSheetValues(context.auth, spreadSheetId, sheetName);

const headers = currentSheetValues[0] ?? [];
const headerCount = headers.length;

// transform row values
const transformedRowValues = transformWorkSheetValues(currentSheetValues, 0)
const transformedRowValues = transformWorkSheetValues(currentSheetValues, 0,headerCount)
.slice(-5)
.reverse();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export const newRowAddedTrigger = createTrigger({
const currentRowValues = await getWorkSheetValues(context.auth, spreadsheet_id, sheetName);
const currentRowCount = currentRowValues.length;

const headers = currentRowValues[0] ?? [];
const headerCount = headers.length;

// if no new rows return
if (oldRowCount >= currentRowCount) {
if (oldRowCount > currentRowCount) {
Expand All @@ -110,7 +113,7 @@ export const newRowAddedTrigger = createTrigger({
await context.store.put(`${sheet_id}`, currentRowCount);

// transform row values
const transformedRowValues = transformWorkSheetValues(newRowValues, oldRowCount);
const transformedRowValues = transformWorkSheetValues(newRowValues, oldRowCount,headerCount);
return transformedRowValues.map((row) => {
return {
...row,
Expand Down Expand Up @@ -142,8 +145,11 @@ export const newRowAddedTrigger = createTrigger({
const sheetName = await getWorkSheetName(context.auth, spreadsheet_id, sheet_id);
const currentSheetValues = await getWorkSheetValues(context.auth, spreadsheet_id, sheetName);

const headers = currentSheetValues[0] ?? [];
const headerCount = headers.length;

// transform row values
const transformedRowValues = transformWorkSheetValues(currentSheetValues, 0)
const transformedRowValues = transformWorkSheetValues(currentSheetValues, 0,headerCount)
.slice(-5)
.reverse();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"extends": [
"../../../../.eslintrc.base.json"
],
"ignorePatterns": [
"!**/*"
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"rules": {}
},
{
"files": [
"*.ts",
"*.tsx"
],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"rules": {}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# pieces-microsoft-outlook-calendar

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build pieces-microsoft-outlook-calendar` to build the library.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@activepieces/piece-microsoft-outlook-calendar",
"version": "0.0.1"
}
45 changes: 45 additions & 0 deletions packages/pieces/community/microsoft-outlook-calendar/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "pieces-microsoft-outlook-calendar",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/pieces/community/microsoft-outlook-calendar/src",
"projectType": "library",
"release": {
"version": {
"generatorOptions": {
"packageRoot": "dist/{projectRoot}",
"currentVersionResolver": "git-tag"
}
}
},
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": [
"{options.outputPath}"
],
"options": {
"outputPath": "dist/packages/pieces/community/microsoft-outlook-calendar",
"tsConfig": "packages/pieces/community/microsoft-outlook-calendar/tsconfig.lib.json",
"packageJson": "packages/pieces/community/microsoft-outlook-calendar/package.json",
"main": "packages/pieces/community/microsoft-outlook-calendar/src/index.ts",
"assets": [
"packages/pieces/community/microsoft-outlook-calendar/*.md"
],
"buildableProjectDepsInPackageJsonType": "dependencies",
"updateBuildableProjectDepsInPackageJson": true
}
},
"nx-release-publish": {
"options": {
"packageRoot": "dist/{projectRoot}"
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": [
"{options.outputFile}"
]
}
}
}
Loading

0 comments on commit 89415f8

Please sign in to comment.