I’m trying to send data from a pandas DataFrame to a Google Apps Script web application, but I keep getting a 400 Bad Request error. Here’s my current Python setup:
import pandas as pd
import requests
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
request_headers = {'User-Agent': user_agent}
data_frame = pd.DataFrame({
'Numbers': [10, 20, 30, 40, 50],
'Letters': ['X', 'Y', 'Z', 'W', 'V']
})
converted_list = data_frame.values.tolist()
target_url = 'https://example.com'
gas_endpoint = "https://script.google.com/macros/s/YOUR_ID/exec"
request_data = {
'data_array': converted_list,
'target_site': target_url
}
response = requests.get(
gas_endpoint,
headers=request_headers,
params=request_data,
timeout=300
)
And here’s my Google Apps Script function:
function doGet(event) {
var dataArray = event.parameter.data_array;
var worksheet = SpreadsheetApp.getActive().getSheetByName('Python Data');
worksheet.getRange(1, 1, dataArray.length, dataArray[0].length).setValues(dataArray);
}
I want the data to appear in my Google Sheet like this:
| Numbers | Letters |
|---|---|
| 10 | X |
| 20 | Y |
| 30 | Z |
| 40 | W |
| 50 | V |
What’s the right way to format the list data so it gets processed correctly by the web app?