This topic was automatically generated from Slack. You can find the original thread here.
can you write a code step that inserts the previous steps values into a googlesheet. There are multiple rows to insert.
This topic was automatically generated from Slack. You can find the original thread here.
can you write a code step that inserts the previous steps values into a googlesheet. There are multiple rows to insert.
To insert multiple rows into a Google Sheet, you can use the following code step in your Pipedream workflow. Make sure to replace YOUR_SPREADSHEET_ID
and YOUR_SHEET_NAME
with the appropriate values for your Google Sheet. import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { const spreadsheetId = "YOUR_SPREADSHEET_ID"; const sheetName = "YOUR_SHEET_NAME"; // Replace this example data with the data you want to insert const rows = [ ["Timestamp1", "Message1", "Sentiment Score1"], ["Timestamp2", "Message2", "Sentiment Score2"], ]; const data = rows.map((row) => ({ values: row.map((value) => ({ userEnteredValue: { stringValue: value } })), })); await axios($, { method: "POST", url: `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${sheetName}:append`, params: { valueInputOption: "USER_ENTERED", insertDataOption: "INSERT_ROWS", }, data: { range: sheetName, majorDimension: "ROWS", values: data, }, headers: { Authorization: `Bearer ${steps.google_sheets_oauth_token}`, }, }); }, });
This code assumes you have a connected Google Sheets account in a previous step, and the OAuth token is available at steps.google_sheets_oauth_token
. Make sure to update this reference based on your workflow.
I need to amend this so it loops through the previous steps values
To loop through the previous step’s values and insert them into a Google Sheet, you can modify the code like this: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { const spreadsheetId = "YOUR_SPREADSHEET_ID"; const sheetName = "YOUR_SHEET_NAME"; // Assuming the previous step's data is an array of arrays const previousStepData = steps.previous_step_name.result; // Replace 'previous_step_name' with the actual name of the previous step const data = previousStepData.map((row) => ({ values: row.map((value) => ({ userEnteredValue: { stringValue: value } })), })); await axios($, { method: "POST", url: `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${sheetName}:append`, params: { valueInputOption: "USER_ENTERED", insertDataOption: "INSERT_ROWS", }, data: { range: sheetName, majorDimension: "ROWS", values: data, }, headers: { Authorization: `Bearer ${steps.google_sheets_oauth_token}`, // Update this reference based on your workflow }, }); }, });
Make sure to replace previous_step_name
with the actual name of the previous step containing the data you want to insert into the Google Sheet. Also, update the steps.google_sheets_oauth_token
reference based on your workflow.