How to Add a Pivot Table to a Google Sheets Spreadsheet Based on Newly Added Data?

This topic was automatically generated from Slack. You can find the original thread here.

, can you show me how to add a pivot table to a Google Sheets spreadsheet based on data that I’ve just added to it?

To add a pivot table to a Google Sheets spreadsheet based on data that you’ve just added to it, you can use the Google Sheets API. Below is an example Pipedream component that demonstrates how to create a pivot table in a Google Sheets spreadsheet. This example assumes you have already added data to your Google Sheets spreadsheet and you have the necessary permissions to access and modify the spreadsheet. ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { google: { type: “app”, app: “google”, }, spreadsheetId: { type: “string”, label: “Spreadsheet ID”, description: “The ID of the Google Sheets spreadsheet”, }, sheetId: { type: “integer”, label: “Sheet ID”, description: “The ID of the sheet where the data is located”, }, pivotSheetId: { type: “integer”, label: “Pivot Sheet ID”, description: “The ID of the sheet where the pivot table will be created”, }, }, async run({ steps, $ }) { const url = https://sheets.googleapis.com/v4/spreadsheets/${this.spreadsheetId}:batchUpdate; const headers = { Authorization: Bearer ${this.google.$auth.oauth_access_token}, “Content-Type”: “application/json”, }; const requestBody = { requests: [ { addSheet: { properties: { title: “Pivot Table”, sheetId: this.pivotSheetId, }, }, }, { updateCells: { range: { sheetId: this.pivotSheetId, startRowIndex: 0, startColumnIndex: 0, }, rows: [ { values: [ { pivotTable: { source: { sheetId: this.sheetId, startRowIndex: 0, startColumnIndex: 0, endRowIndex: 1000, // Adjust based on your data range endColumnIndex: 10, // Adjust based on your data range }, rows: [ { sourceColumnOffset: 0, showTotals: true, }, ], columns: [ { sourceColumnOffset: 1, showTotals: true, },

, the example is incomplete. Please show me the rest of the code after ‘columns: [’.

It looks like your question got cut off. Could you please provide more details or clarify what you’re looking for? Specifically, if you could share the context or the part of the code you’re referring to, I can help you complete it.

, can you show me how code to add a pivot table to a Google Sheets spreadsheet? Split the response into two chunks; one just for the 'const requestBody = ’ and whatever that should be and then a second chunk that shows how it’s used, with 'const requestBody = “defined previously” to keep it short.

Sure, I can help you with that. Below is the code split into two chunks as requested. ### Chunk 1: Define the requestBody const requestBody = { "requests": [ { "updateCells": { "range": { "sheetId": 0, "startRowIndex": 0, "startColumnIndex": 0, "endRowIndex": 1, "endColumnIndex": 1 }, "rows": [ { "values": [ { "pivotTable": { "source": { "sheetId": 0, "startRowIndex": 1, "startColumnIndex": 0, "endRowIndex": 100, "endColumnIndex": 5 }, "rows": [ { "sourceColumnOffset": 0, "showTotals": true, "sortOrder": "ASCENDING" } ], "columns": [ { "sourceColumnOffset": 1, "showTotals": true, "sortOrder": "ASCENDING" } ], "values": [ { "sourceColumnOffset": 2, "summarizeFunction": "SUM" } ] } } ] } ], "fields": "pivotTable" } } ] }; ### Chunk 2: Use the requestBody import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, spreadsheetId: { type: "string", label: "Spreadsheet ID", }, }, async run({ steps, $ }) { const requestBody = "defined previously"; return await axios($, { url: `https://sheets.googleapis.com/v4/spreadsheets/${this.spreadsheetId}:batchUpdate`, method: "POST", headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, "Content-Type": "application/json", }, data: requestBody, }); }, }); This code defines the requestBody for creating a pivot table in Google Sheets and then uses it in a Pipedream