Skip to content
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
@@ -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,
Expand Down Expand Up @@ -37,7 +40,6 @@ export default {
],
type: "string",
label: "Worksheet Id",
withLabel: true,
reloadProps: true,
},
headersDisplay: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
});

Expand Down
40 changes: 28 additions & 12 deletions components/google_sheets/actions/add-single-row/add-single-row.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,7 +27,6 @@ export default {
driveId: googleSheets.methods.getDriveId(c.drive),
}),
],
withLabel: true,
},
worksheetId: {
propDefinition: [
Expand All @@ -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.");
}
Expand All @@ -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);
Expand Down Expand Up @@ -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.";
}
Expand Down
11 changes: 7 additions & 4 deletions components/google_sheets/actions/clear-cell/clear-cell.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -34,7 +37,6 @@ export default {
],
type: "string",
label: "Worksheet Id",
withLabel: true,
},
cell: {
type: "string",
Expand All @@ -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);
},
Expand Down
11 changes: 7 additions & 4 deletions components/google_sheets/actions/clear-rows/clear-rows.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -34,7 +37,6 @@ export default {
],
type: "string",
label: "Worksheet Id",
withLabel: true,
},
startIndex: {
type: "integer",
Expand All @@ -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);
},
Expand Down
20 changes: 20 additions & 0 deletions components/google_sheets/actions/common/worksheet.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
},
},
};
13 changes: 8 additions & 5 deletions components/google_sheets/actions/find-row/find-row.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -33,7 +36,6 @@ export default {
],
type: "string",
label: "Worksheet Id",
withLabel: true,
},
column: {
propDefinition: [
Expand All @@ -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 = [];
Expand All @@ -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)) {
Expand Down
11 changes: 7 additions & 4 deletions components/google_sheets/actions/get-cell/get-cell.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -33,7 +36,6 @@ export default {
],
type: "string",
label: "Worksheet Id",
withLabel: true,
},
cell: {
propDefinition: [
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -33,7 +36,6 @@ export default {
],
type: "string",
label: "Worksheet Id",
withLabel: true,
},
range: {
propDefinition: [
Expand All @@ -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;
},
};
11 changes: 7 additions & 4 deletions components/google_sheets/actions/update-cell/update-cell.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -35,7 +38,6 @@ export default {
],
type: "string",
label: "Worksheet Id",
withLabel: true,
},
cell: {
propDefinition: [
Expand All @@ -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: [
Expand Down
Loading