# This program requires the following libraries: # - googleapiclient.discovery # - google.oauth2.service_account # Install them using pip: # pip install googleapiclient.discovery google-auth-oauthlib google-auth-httplib2 from googleapiclient.discovery import build from googleapiclient.errors import HttpError from google.oauth2.service_account import Credentials # Replace these variables with your values SPREADSHEET_ID = "YOUR_SPREADSHEET_ID" # Replace with your spreadsheet ID SHEET_RANGE = "Sheet1!A:H" # Replace with the sheet name and desired range CREDENTIALS_FILE = "credentials.json" # Replace with your credentials file path OUTPUT_FILE = "data.json" # Replace with your desired output file name def main(): """Reads a Google Spreadsheet and creates a JSON file.""" # Define the service scope scopes = ["https://www.googleapis.com/auth/spreadsheets.readonly"] # Load credentials from the JSON file credentials = Credentials.from_service_account_file(CREDENTIALS_FILE, scopes=scopes) # Build the Sheets service object service = build("sheets", "v4", credentials=credentials) try: # Get spreadsheet data sheet = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID, range=SHEET_RANGE).execute() values = sheet.get("values", []) # Skip the header row data = [dict(zip(values[0], row)) for row in values[1:]] # Write data to JSON file with open(OUTPUT_FILE, "w") as outfile: json.dump(data, outfile, indent=4) print(f"Data written to {OUTPUT_FILE}") except HttpError as error: print(f"An error occurred: {error}") if __name__ == "__main__": main() Use code with caution. content_copy Instructions: Replace placeholders: YOUR_SPREADSHEET_ID: Replace with the ID of your Google Spreadsheet. You can find the ID in the URL of your spreadsheet. SHEET_RANGE: Replace with the name of the sheet you want to read and the desired range of columns (e.g., Sheet1!A:H). CREDENTIALS_FILE: Replace with the path to your Google Cloud credentials file in JSON format. You can follow the instructions here [invalid URL removed] to create one. OUTPUT_FILE: Replace with your desired output file name for the JSON data. Install libraries: Bash pip install googleapiclient.discovery google-auth-oauthlib google-auth-httplib2 Use code with caution. content_copy Run the program: Save the code as a Python file (e.g., read_spreadsheet.py) and run it from your terminal: Bash python read_spreadsheet.py Use code with caution. content_copy This program will read the specified sheet and columns from your Google Spreadsheet and create a JSON file containing the data as a list of dictionaries. Each dictionary represents a row in the spreadsheet, with keys corresponding to the column headers and values being the corresponding cell values