Category: Datasources
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

How Do I Pass Data Into a Google Sheets API Call?

Issue

I am having trouble passing data from an async JS object into a Google Sheets API call named write_data_3C. I have tried using a JS object to make the request, but it gives me the same error. The only error I receive is "write_data_3C failed to execute". I am unsure of how to pass variables into the API call, and I have been able to write things if they come from a table widget, but this data is coming from async JS objects. I am wondering if it's even possible to pass data into a GSheets API call or if it has to be stored in a UI widget.

Resolution

When working with Google Sheets in Appsmith, a common use case is dynamically sending data from your app to a sheet. This can be done efficiently using parameterized queries. With this approach, you can structure your data in a widget or JavaScript object and pass it directly to your query using the .run() method.

For instance, if you have a query named write_data_3C, you can pass data to it by calling write_data_3C.run(data), where data is a JavaScript object containing the values you want to send to Google Sheets. This object might include fields like a user's name, email, and the current date, collected from input widgets or other sources in your app.

const rowData = {
  name: Input1.text,
  email: Input2.text,
  date: moment().format("YYYY-MM-DD")
};

Inside the query configuration, you can access this data using the this.params object. For example, if your data object includes name, email, and date, you can reference them as {{this.params.name}}, {{this.params.email}}, and {{this.params.date}} directly in the Row object(s) of your GSheet API call.

{
"Name": "{{this.params.name}}",
"Email": "{{this.params.email}}",
"Date": "{{this.params.date}}"
}

This pattern keeps your queries clean and reusable, while also allowing for flexible, dynamic data submission. It's especially useful when working with complex forms or multiple user inputs.

To explore more about how this.params works and other best practices for using queries securely and efficiently, check out the official documentation on Parameterized Queries in Appsmith.