diff --git a/components/google_sheets/actions/add-multiple-rows/add-multiple-rows.mjs b/components/google_sheets/actions/add-multiple-rows/add-multiple-rows.mjs index b6f4bbe41bf25..e2650908f369f 100644 --- a/components/google_sheets/actions/add-multiple-rows/add-multiple-rows.mjs +++ b/components/google_sheets/actions/add-multiple-rows/add-multiple-rows.mjs @@ -1,14 +1,17 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; import { ConfigurationError } from "@pipedream/platform"; import { parseArray, getWorksheetHeaders, } from "../../common/utils.mjs"; +const { googleSheets } = common.props; + export default { + ...common, 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.6", + version: "0.2.7", type: "action", props: { googleSheets, @@ -37,7 +40,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, reloadProps: true, }, headersDisplay: { @@ -70,7 +72,8 @@ export default { if (!this.sheetId || !this.worksheetId) { return props; } - const rowHeaders = await getWorksheetHeaders(this, this.sheetId, this.worksheetId.label); + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); + const rowHeaders = await getWorksheetHeaders(this, this.sheetId, worksheet?.properties?.title); if (rowHeaders.length) { return { headersDisplay: { @@ -100,9 +103,10 @@ export default { throw new ConfigurationError("Rows data is not an array of arrays. Please enter an array of arrays in the `Rows` parameter above. If you're trying to send a single rows to Google Sheets, search for the action to add a single row to Sheets or try modifying the code for this step."); } + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const addRowsResponse = await this.googleSheets.addRowsToSheet({ spreadsheetId: this.sheetId, - range: this.worksheetId.label, + range: worksheet?.properties?.title, rows, }); diff --git a/components/google_sheets/actions/add-single-row/add-single-row.mjs b/components/google_sheets/actions/add-single-row/add-single-row.mjs index b28518bd771c0..b1b91b8c952fa 100644 --- a/components/google_sheets/actions/add-single-row/add-single-row.mjs +++ b/components/google_sheets/actions/add-single-row/add-single-row.mjs @@ -1,12 +1,15 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; import { ConfigurationError } from "@pipedream/platform"; import { parseArray } from "../../common/utils.mjs"; +const { googleSheets } = common.props; + export default { + ...common, 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.7", + version: "2.1.8", type: "action", props: { googleSheets, @@ -24,7 +27,6 @@ export default { driveId: googleSheets.methods.getDriveId(c.drive), }), ], - withLabel: true, }, worksheetId: { propDefinition: [ @@ -36,21 +38,26 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, 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.", + 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 headers use the `Select an option` instead in both **Spreadsheet** and **Worksheet Id** props.", reloadProps: true, + optional: true, }, }, async additionalProps() { - const sheetId = this.sheetId?.value || this.sheetId; - const worksheetName = this.worksheetId?.label; + const { + sheetId, + worksheetId, + } = this; + const props = {}; if (this.hasHeaders) { - const { values } = await this.googleSheets.getSpreadsheetValues(sheetId, `${worksheetName}!1:1`); + const worksheet = await this.getWorksheetById(sheetId, worksheetId); + + 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."); } @@ -76,8 +83,17 @@ export default { return props; }, async run({ $ }) { - const sheetId = this.sheetId?.value || this.sheetId; - const worksheetName = this.worksheetId.label; + const { + sheetId, + worksheetId, + } = this; + + const { name: sheetName } = await this.googleSheets.getFile(sheetId, { + fields: "name", + }); + + const worksheet = await this.getWorksheetById(sheetId, worksheetId); + let cells; if (this.hasHeaders) { const rows = JSON.parse(this.allColumns); @@ -109,13 +125,13 @@ export default { const data = await this.googleSheets.addRowsToSheet({ spreadsheetId: sheetId, - range: worksheetName, + range: worksheet?.properties?.title, rows: [ arr, ], }); - let summary = `Added 1 row to [${this.sheetId?.label || sheetId} (${data.updatedRange})](https://docs.google.com/spreadsheets/d/${sheetId}).`; + let summary = `Added 1 row to [${sheetName || sheetId} (${data.updatedRange})](https://docs.google.com/spreadsheets/d/${sheetId}).`; if (convertedIndexes.length > 0) { summary += " We detected something other than a string/number/boolean in at least one of the fields and automatically converted it to a string."; } diff --git a/components/google_sheets/actions/clear-cell/clear-cell.mjs b/components/google_sheets/actions/clear-cell/clear-cell.mjs index dc13b93747985..281028a57ee0d 100644 --- a/components/google_sheets/actions/clear-cell/clear-cell.mjs +++ b/components/google_sheets/actions/clear-cell/clear-cell.mjs @@ -1,10 +1,13 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; + +const { googleSheets } = common.props; export default { + ...common, 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.7", + version: "0.1.8", type: "action", props: { googleSheets, @@ -34,7 +37,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, cell: { type: "string", @@ -43,9 +45,10 @@ export default { }, }, async run() { + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const request = { spreadsheetId: this.sheetId, - range: `${this.worksheetId.label}!${this.cell}`, + range: `${worksheet?.properties?.title}!${this.cell}`, }; return await this.googleSheets.clearSheetValues(request); }, diff --git a/components/google_sheets/actions/clear-rows/clear-rows.mjs b/components/google_sheets/actions/clear-rows/clear-rows.mjs index b4862515daa6b..c8edee61cf0a3 100644 --- a/components/google_sheets/actions/clear-rows/clear-rows.mjs +++ b/components/google_sheets/actions/clear-rows/clear-rows.mjs @@ -1,10 +1,13 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; + +const { googleSheets } = common.props; export default { + ...common, 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.5", + version: "0.1.6", type: "action", props: { googleSheets, @@ -34,7 +37,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, startIndex: { type: "integer", @@ -49,9 +51,10 @@ export default { }, }, async run() { + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const request = { spreadsheetId: this.sheetId, - range: `${this.worksheetId.label}!${this.startIndex}:${this.endIndex || this.startIndex}`, + range: `${worksheet?.properties?.title}!${this.startIndex}:${this.endIndex || this.startIndex}`, }; return await this.googleSheets.clearSheetValues(request); }, diff --git a/components/google_sheets/actions/common/worksheet.mjs b/components/google_sheets/actions/common/worksheet.mjs new file mode 100644 index 0000000000000..dc368d2158ce2 --- /dev/null +++ b/components/google_sheets/actions/common/worksheet.mjs @@ -0,0 +1,20 @@ +import googleSheets from "../../google_sheets.app.mjs"; + +export default { + props: { + googleSheets, + }, + methods: { + async getWorksheetById(sheetId, worksheetId) { + try { + const { sheets } = await this.googleSheets.getSpreadsheet(sheetId); + return sheets + .find(({ properties: { sheetId } }) => String(sheetId) === String(worksheetId)); + } catch (error) { + const errorMsg = `Error fetching worksheet with ID \`${worksheetId}\` - ${error.message}`; + console.log(error); + throw new Error(errorMsg); + } + }, + }, +}; diff --git a/components/google_sheets/actions/find-row/find-row.mjs b/components/google_sheets/actions/find-row/find-row.mjs index 6bdedba773e45..4ef9d4cd43ed8 100644 --- a/components/google_sheets/actions/find-row/find-row.mjs +++ b/components/google_sheets/actions/find-row/find-row.mjs @@ -1,10 +1,13 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; + +const { googleSheets } = common.props; export default { + ...common, 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.5", + version: "0.2.6", type: "action", props: { googleSheets, @@ -33,7 +36,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, column: { propDefinition: [ @@ -54,11 +56,12 @@ export default { }, }, async run() { + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const sheets = this.googleSheets.sheets(); const colValues = (await sheets.spreadsheets.values.get({ spreadsheetId: this.sheetId, - range: `${this.worksheetId.label}!${this.column}:${this.column}`, + range: `${worksheet?.properties?.title}!${this.column}:${this.column}`, })).data.values; const rows = []; @@ -81,7 +84,7 @@ export default { const { data: { values } } = await sheets.spreadsheets.values.get({ spreadsheetId: this.sheetId, - range: `${this.worksheetId.label}`, + range: `${worksheet?.properties?.title}`, }); return values.reduce((acc, row, index) => { if (indexes.includes(index)) { diff --git a/components/google_sheets/actions/get-cell/get-cell.mjs b/components/google_sheets/actions/get-cell/get-cell.mjs index 982be14579bd8..5802f232d7d96 100644 --- a/components/google_sheets/actions/get-cell/get-cell.mjs +++ b/components/google_sheets/actions/get-cell/get-cell.mjs @@ -1,10 +1,13 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; + +const { googleSheets } = common.props; export default { + ...common, 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.5", + version: "0.1.6", type: "action", props: { googleSheets, @@ -33,7 +36,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, cell: { propDefinition: [ @@ -43,11 +45,12 @@ export default { }, }, async run() { + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const sheets = this.googleSheets.sheets(); const values = (await sheets.spreadsheets.values.get({ spreadsheetId: this.sheetId, - range: `${this.worksheetId.label}!${this.cell}:${this.cell}`, + range: `${worksheet?.properties?.title}!${this.cell}:${this.cell}`, })).data.values; if (values?.length) { return values[0][0]; diff --git a/components/google_sheets/actions/get-values-in-range/get-values-in-range.mjs b/components/google_sheets/actions/get-values-in-range/get-values-in-range.mjs index 4febfdf2e00fb..f72364fcc5886 100644 --- a/components/google_sheets/actions/get-values-in-range/get-values-in-range.mjs +++ b/components/google_sheets/actions/get-values-in-range/get-values-in-range.mjs @@ -1,10 +1,13 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; + +const { googleSheets } = common.props; export default { + ...common, 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.5", + version: "0.1.6", type: "action", props: { googleSheets, @@ -33,7 +36,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, range: { propDefinition: [ @@ -44,13 +46,14 @@ export default { }, }, async run() { + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const sheets = this.googleSheets.sheets(); return (await sheets.spreadsheets.values.get({ spreadsheetId: this.sheetId, range: this.range - ? `${this.worksheetId.label}!${this.range}` - : `${this.worksheetId.label}`, + ? `${worksheet?.properties?.title}!${this.range}` + : `${worksheet?.properties?.title}`, })).data.values; }, }; diff --git a/components/google_sheets/actions/update-cell/update-cell.mjs b/components/google_sheets/actions/update-cell/update-cell.mjs index c4581da5d71ff..285b9d088f22b 100644 --- a/components/google_sheets/actions/update-cell/update-cell.mjs +++ b/components/google_sheets/actions/update-cell/update-cell.mjs @@ -1,10 +1,13 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; + +const { googleSheets } = common.props; export default { + ...common, 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.5", + version: "0.1.6", type: "action", props: { googleSheets, @@ -35,7 +38,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, cell: { propDefinition: [ @@ -54,9 +56,10 @@ export default { }, }, async run() { + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const request = { spreadsheetId: this.sheetId, - range: `${this.worksheetId.label}!${this.cell}:${this.cell}`, + range: `${worksheet?.properties?.title}!${this.cell}:${this.cell}`, valueInputOption: "USER_ENTERED", resource: { values: [ diff --git a/components/google_sheets/actions/update-multiple-rows/update-multiple-rows.mjs b/components/google_sheets/actions/update-multiple-rows/update-multiple-rows.mjs index 8affa4dbb4107..8a6350a50baa9 100644 --- a/components/google_sheets/actions/update-multiple-rows/update-multiple-rows.mjs +++ b/components/google_sheets/actions/update-multiple-rows/update-multiple-rows.mjs @@ -1,13 +1,15 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; import { parseArray, getWorksheetHeaders, } from "../../common/utils.mjs"; +const { googleSheets } = common.props; export default { + ...common, 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.5", + version: "0.1.6", type: "action", props: { googleSheets, @@ -39,7 +41,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, reloadProps: true, }, headersDisplay: { @@ -72,7 +73,8 @@ export default { if (!this.sheetId || !this.worksheetId) { return props; } - const rowHeaders = await getWorksheetHeaders(this, this.sheetId, this.worksheetId.label); + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); + const rowHeaders = await getWorksheetHeaders(this, this.sheetId, worksheet?.properties?.title); if (rowHeaders.length) { return { headersDisplay: { @@ -108,9 +110,10 @@ export default { ); } + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const request = { spreadsheetId: this.sheetId, - range: `${this.worksheetId.label}!${this.range}`, + range: `${worksheet?.properties?.title}!${this.range}`, valueInputOption: "USER_ENTERED", resource: { values: rows, diff --git a/components/google_sheets/actions/update-row/update-row.mjs b/components/google_sheets/actions/update-row/update-row.mjs index 946a266f51f70..e2ac08478a1c1 100644 --- a/components/google_sheets/actions/update-row/update-row.mjs +++ b/components/google_sheets/actions/update-row/update-row.mjs @@ -1,12 +1,15 @@ -import googleSheets from "../../google_sheets.app.mjs"; +import common from "../common/worksheet.mjs"; import { ConfigurationError } from "@pipedream/platform"; import { parseArray } from "../../common/utils.mjs"; +const { googleSheets } = common.props; + export default { + ...common, key: "google_sheets-update-row", name: "Update Row", description: "Update a row in a spreadsheet. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update)", - version: "0.1.5", + version: "0.1.6", type: "action", props: { googleSheets, @@ -37,7 +40,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, row: { propDefinition: [ @@ -65,9 +67,10 @@ export default { if (Array.isArray(cells[0])) { throw new ConfigurationError("Row Values is a multi-dimensional array. A one-dimensional is expected."); } + const worksheet = await this.getWorksheetById(this.sheetId, this.worksheetId); const request = { spreadsheetId: this.sheetId, - range: `${this.worksheetId.label}!${this.row}:${this.row}`, + range: `${worksheet?.properties?.title}!${this.row}:${this.row}`, valueInputOption: "USER_ENTERED", resource: { values: [ diff --git a/components/google_sheets/actions/upsert-row/upsert-row.mjs b/components/google_sheets/actions/upsert-row/upsert-row.mjs index 50466a22a31e4..2ef691fe08abf 100644 --- a/components/google_sheets/actions/upsert-row/upsert-row.mjs +++ b/components/google_sheets/actions/upsert-row/upsert-row.mjs @@ -1,10 +1,12 @@ import { v4 as uuid } from "uuid"; +import common from "../common/worksheet.mjs"; import { VALUE_RENDER_OPTION } from "../../common/constants.mjs"; -import googleSheets from "../../google_sheets.app.mjs"; import { omitEmptyKey, toSingleLineString, } from "../../common/utils.mjs"; +const { googleSheets } = common.props; + /** * This action performs an upsert operation, similar to the MySQL `INSERT INTO ... ON DUPLICATE KEY * UPDATE` operation, on a Google Spreadsheet. If a row in the spreadsheet has `value` in `column` @@ -18,10 +20,11 @@ import { * comment](https://github.com/PipedreamHQ/pipedream/issues/1824#issuecomment-949940177). */ export default { + ...common, key: "google_sheets-upsert-row", name: "Upsert Row", description: "Upsert a row of data in a Google Sheet. [See the documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append)", - version: "0.1.7", + version: "0.1.8", type: "action", props: { googleSheets, @@ -50,7 +53,6 @@ export default { ], type: "string", label: "Worksheet Id", - withLabel: true, }, insert: { propDefinition: [ @@ -103,6 +105,7 @@ export default { value, updates, } = this; + const worksheet = await this.getWorksheetById(sheetId, worksheetId); const colIndex = this.googleSheets._getColumnIndex(column) - 1; const keyValue = value ? value @@ -129,7 +132,7 @@ export default { keyValue, // A1 ], [ - this.googleSheets.buildMatchFormula("A1", worksheetId.label, { + this.googleSheets.buildMatchFormula("A1", worksheet?.properties?.title, { column, searchType: 0, }), // A2 @@ -149,7 +152,7 @@ export default { // INSERT ROW const result = await this.googleSheets.addRowsToSheet({ spreadsheetId: sheetId, - range: worksheetId.label, + range: worksheet?.properties?.title, rows: [ insert, ], @@ -161,7 +164,7 @@ export default { // UPDATE ROW const updateParams = [ sheetId, - worksheetId.label, + worksheet?.properties?.title, matchedRow, ]; const sanitizedUpdates = omitEmptyKey(updates); diff --git a/components/google_sheets/package.json b/components/google_sheets/package.json index e7fa1ed295509..4a6c032c5dfe8 100644 --- a/components/google_sheets/package.json +++ b/components/google_sheets/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/google_sheets", - "version": "0.7.4", + "version": "0.7.5", "description": "Pipedream Google_sheets Components", "main": "google_sheets.app.mjs", "keywords": [