I had done that first step, and I added those next two lines, but I think the HTTP request might’ve been the issue. And that’s not something I’m confident I know how to do correctly. Here’s an updated version of the script, minus the bits that I’m confident aren’t relevant:
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from googleapiclient import discovery
import requests
spreadsheet_name = “NAME REDACTED”
credentials_file = “credentials.json”
scope = [‘https://www.googleapis.com/auth/spreadsheets’]
credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_file, scope)
client = gspread.authorize(credentials)
spreadsheet = client.open(spreadsheet_name)
worksheet = spreadsheet.sheet1
df = pd.DataFrame(worksheet.get_all_records())
SKIPPED OVER A BUNCH OF CODE RELATED TO UPDATING THE SPREADSHEET
worksheet_values = worksheet.get_all_values()
values_range = f’{worksheet.title}!{worksheet_values[0][10]}:{worksheet_values[-1][10]}’
url = f"https://sheets.googleapis.com/v4/spreadsheets/637219848/values/{values_range}?valueInputOption=USER_ENTERED"
access_token = credentials.get_access_token().access_token
payload = {
“values”: worksheet_values[1:],
}
headers = {
“Authorization”: f"Bearer {access_token}",
“Content-Type”: “application/json”
}
response = requests.put(url, headers=headers, json=payload)
print(“Response Status Code:”, response.status_code)
print(“Response Content:”, response.json())
print(df)
print(“Messages have been inserted into the ‘Email text’ column of the Google Sheets spreadsheet.”)