Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor hasHeaders prop to common file, fix behavior with custom expressions for Google Sheets #14452

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
key: "google_sheets-add-multiple-rows",
name: "Add Multiple Rows",
description: "Add multiple rows of data to a Google Sheet. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append)",
version: "0.2.8",
version: "0.2.9",
type: "action",
props: {
googleSheets,
Expand Down
100 changes: 73 additions & 27 deletions components/google_sheets/actions/add-single-row/add-single-row.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import common from "../common/worksheet.mjs";
import { ConfigurationError } from "@pipedream/platform";
import { parseArray } from "../../common/utils.mjs";
import { isDynamicExpression } from "../common/worksheet.mjs";

const { googleSheets } = common.props;

Expand All @@ -9,7 +10,7 @@ export default {
key: "google_sheets-add-single-row",
name: "Add Single Row",
description: "Add a single row of data to Google Sheets. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append)",
version: "2.1.10",
version: "2.1.11",
type: "action",
props: {
googleSheets,
Expand All @@ -27,6 +28,7 @@ export default {
driveId: googleSheets.methods.getDriveId(c.drive),
}),
],
reloadProps: true,
},
worksheetId: {
propDefinition: [
Expand All @@ -36,42 +38,81 @@ export default {
sheetId: c.sheetId?.value || c.sheetId,
}),
],
type: "string",
label: "Worksheet ID",
},
hasHeaders: {
type: "boolean",
label: "Does the first row of the sheet have headers?",
description: "If the first row of your document has headers, we'll retrieve them to make it easy to enter the value for each column. Please note, that if you are referencing a worksheet using a custom expression referencing data from another step, e.g. `{{steps.my_step.$return_value}}` this prop cannot be used. If you want to retrieve the header row, select both **Spreadsheet** and **Worksheet ID** from the dropdowns above.",
description: "Select a worksheet or enter a custom expression. When referencing a spreadsheet dynamically, you must provide a custom expression for the worksheet.",
async options({ sheetId }) {
// If sheetId is a dynamic reference, don't load options
if (isDynamicExpression(sheetId)) {
return [];
}

// Otherwise, call the original options function with the correct context
const origOptions = googleSheets.propDefinitions.worksheetIDs.options;
return origOptions.call(this, {
sheetId,
});
},
reloadProps: true,
},
hasHeaders: common.props.hasHeaders,
},
async additionalProps() {
const {
sheetId,
worksheetId,
hasHeaders,
} = this;

// If using dynamic expressions for either sheetId or worksheetId, return only array input
if (isDynamicExpression(sheetId) || isDynamicExpression(worksheetId)) {
return {
myColumnData: {
type: "string[]",
label: "Values",
description: "Provide a value for each cell of the row. Google Sheets accepts strings, numbers and boolean values for each cell. To set a cell to an empty value, pass an empty string.",
},
};
}

const props = {};
if (this.hasHeaders) {
const worksheet = await this.getWorksheetById(sheetId, worksheetId);
if (hasHeaders) {
try {
const worksheet = await this.getWorksheetById(sheetId, worksheetId);
const { values } = await this.googleSheets.getSpreadsheetValues(sheetId, `${worksheet?.properties?.title}!1:1`);

const { values } = await this.googleSheets.getSpreadsheetValues(sheetId, `${worksheet?.properties?.title}!1:1`);
if (!values[0]?.length) {
throw new ConfigurationError("Could not find a header row. Please either add headers and click \"Refresh fields\" or adjust the action configuration to continue.");
}
for (let i = 0; i < values[0]?.length; i++) {
props[`col_${i.toString().padStart(4, "0")}`] = {
if (!values?.[0]?.length) {
throw new ConfigurationError("Could not find a header row. Please either add headers and click \"Refresh fields\" or set 'Does the first row of the sheet have headers?' to false.");
}

for (let i = 0; i < values[0]?.length; i++) {
props[`col_${i.toString().padStart(4, "0")}`] = {
type: "string",
label: values[0][i],
optional: true,
};
}
props.allColumns = {
type: "string",
label: values[0][i],
optional: true,
hidden: true,
default: JSON.stringify(values),
};
} catch (err) {
console.error("Error fetching headers:", err);
// Fallback to basic column input if headers can't be fetched
return {
headerError: {
type: "string",
label: "Header Fetch Error",
description: `Unable to fetch headers: ${err.message}. Using simple column input instead.`,
optional: true,
hidden: true,
},
myColumnData: {
type: "string[]",
label: "Values",
description: "Provide a value for each cell of the row. Google Sheets accepts strings, numbers and boolean values for each cell. To set a cell to an empty value, pass an empty string.",
},
};
jcortes marked this conversation as resolved.
Show resolved Hide resolved
}
props.allColumns = {
type: "string",
hidden: true,
default: JSON.stringify(values),
};
} else {
props.myColumnData = {
type: "string[]",
Expand All @@ -94,7 +135,11 @@ export default {
const worksheet = await this.getWorksheetById(sheetId, worksheetId);

let cells;
if (this.hasHeaders) {
if (this.hasHeaders
&& !isDynamicExpression(sheetId)
&& !isDynamicExpression(worksheetId)
&& this.allColumns
) {
const rows = JSON.parse(this.allColumns);
const [
headers,
Expand All @@ -103,10 +148,11 @@ export default {
.map((_, i) => `col_${i.toString().padStart(4, "0")}`)
.map((column) => this[column] ?? "");
} else {
// For dynamic references or no headers, use the array input
cells = this.googleSheets.sanitizedArray(this.myColumnData);
}

// validate input
// Validate input
if (!cells || !cells.length) {
throw new ConfigurationError("Please enter an array of elements in `Cells / Column Values`.");
}
Expand All @@ -118,15 +164,15 @@ export default {
}

const {
arr,
arr: sanitizedCells,
convertedIndexes,
} = this.googleSheets.arrayValuesToString(cells);

const data = await this.googleSheets.addRowsToSheet({
spreadsheetId: sheetId,
range: worksheet?.properties?.title,
rows: [
arr,
sanitizedCells,
],
});

Expand Down
2 changes: 1 addition & 1 deletion components/google_sheets/actions/clear-cell/clear-cell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
key: "google_sheets-clear-cell",
name: "Clear Cell",
description: "Delete the content of a specific cell in a spreadsheet. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear)",
version: "0.1.9",
version: "0.1.10",
type: "action",
props: {
googleSheets,
Expand Down
2 changes: 1 addition & 1 deletion components/google_sheets/actions/clear-rows/clear-rows.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
key: "google_sheets-clear-rows",
name: "Clear Rows",
description: "Delete the content of a row or rows in a spreadsheet. Deleted rows will appear as blank rows. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear)",
version: "0.1.7",
version: "0.1.8",
type: "action",
props: {
googleSheets,
Expand Down
12 changes: 12 additions & 0 deletions components/google_sheets/actions/common/worksheet.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import googleSheets from "../../google_sheets.app.mjs";

// Helper function to check if a value is a dynamic expression
export const isDynamicExpression = (value) => {
if (!value || typeof value !== "string") return false;
return value.trim().startsWith("{{") && value.trim().endsWith("}}");
};

export default {
props: {
googleSheets,
hasHeaders: {
type: "boolean",
label: "Does the first row of the sheet have headers?",
description: "If the first row of your document has headers, we'll retrieve them to make it easy to enter the value for each column. Note: When using a dynamic reference for the worksheet ID (e.g. `{{steps.foo.$return_value}}`), this setting is ignored.",
reloadProps: true,
},
jcortes marked this conversation as resolved.
Show resolved Hide resolved
},
methods: {
async getWorksheetById(sheetId, worksheetId) {
Expand Down
2 changes: 1 addition & 1 deletion components/google_sheets/actions/find-row/find-row.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
key: "google_sheets-find-row",
name: "Find Row",
description: "Find one or more rows by a column and value. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get)",
version: "0.2.7",
version: "0.2.8",
type: "action",
props: {
googleSheets,
Expand Down
2 changes: 1 addition & 1 deletion components/google_sheets/actions/get-cell/get-cell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
key: "google_sheets-get-cell",
name: "Get Cell",
description: "Fetch the contents of a specific cell in a spreadsheet. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get)",
version: "0.1.7",
version: "0.1.8",
type: "action",
props: {
googleSheets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
key: "google_sheets-get-values-in-range",
name: "Get Values in Range",
description: "Get all values or values from a range of cells using A1 notation. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get)",
version: "0.1.7",
version: "0.1.8",
type: "action",
props: {
googleSheets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
key: "google_sheets-update-cell",
name: "Update Cell",
description: "Update a cell in a spreadsheet. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update)",
version: "0.1.7",
version: "0.1.8",
type: "action",
props: {
googleSheets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
key: "google_sheets-update-multiple-rows",
name: "Update Multiple Rows",
description: "Update multiple rows in a spreadsheet defined by a range. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update)",
version: "0.1.7",
version: "0.1.8",
type: "action",
props: {
googleSheets,
Expand Down
Loading
Loading