|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "colab_type": "text", |
| 7 | + "id": "view-in-github" |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "<a href=\"https://colab.research.google.com/github/HTTPArchive/almanac.httparchive.org/blob/main/src/tools/scripts/chart-adjustments.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "metadata": { |
| 17 | + "id": "cOcbpC6qRou_" |
| 18 | + }, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "from google.colab import auth\n", |
| 22 | + "from googleapiclient.discovery import build\n", |
| 23 | + "\n", |
| 24 | + "def update_chart_size(spreadsheet_id, is_dry_run=False, target_width=600, target_height=None):\n", |
| 25 | + " response = sheets_service.spreadsheets().get(spreadsheetId=spreadsheet_id, includeGridData=False).execute()\n", |
| 26 | + " sheets = response.get('sheets', [])\n", |
| 27 | + "\n", |
| 28 | + " for sheet in sheets:\n", |
| 29 | + " charts = sheet.get('charts', [])\n", |
| 30 | + "\n", |
| 31 | + " for chart in charts:\n", |
| 32 | + " chart_id = chart['chartId']\n", |
| 33 | + " update_request = {\n", |
| 34 | + " 'updateEmbeddedObjectPosition': {\n", |
| 35 | + " 'objectId': chart_id,\n", |
| 36 | + " 'newPosition': {\n", |
| 37 | + " 'overlayPosition': {\n", |
| 38 | + " 'widthPixels': target_width\n", |
| 39 | + " }\n", |
| 40 | + " },\n", |
| 41 | + " 'fields': 'widthPixels'\n", |
| 42 | + " }\n", |
| 43 | + " }\n", |
| 44 | + "\n", |
| 45 | + " if target_height is not None:\n", |
| 46 | + " update_request['updateEmbeddedObjectPosition']['newPosition']['overlayPosition']['heightPixels'] = target_height\n", |
| 47 | + " update_request['updateEmbeddedObjectPosition']['fields'] += ',heightPixels'\n", |
| 48 | + "\n", |
| 49 | + " if not is_dry_run and \\\n", |
| 50 | + " (chart['position']['overlayPosition']['widthPixels'] != target_width or\n", |
| 51 | + " (target_height is not None and chart['position']['overlayPosition']['heightPixels'] != target_height)):\n", |
| 52 | + " sheets_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body={'requests': [update_request]}).execute()\n", |
| 53 | + "\n", |
| 54 | + " print(f\"\"\"sheet: {sheet['properties']['title']},\n", |
| 55 | + "chart: {chart['spec']['title']},\n", |
| 56 | + "dimensions: {chart['position']['overlayPosition']['widthPixels']} x {chart['position']['overlayPosition'].get('heightPixels', 'N/A')}\n", |
| 57 | + " \"\"\")\n", |
| 58 | + "\n", |
| 59 | + "# Authenticate the user\n", |
| 60 | + "auth.authenticate_user()\n", |
| 61 | + "sheets_service = build('sheets', 'v4', cache_discovery=False)" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "metadata": { |
| 68 | + "id": "vp1izUBSLxp9" |
| 69 | + }, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "# Replace this with the ID of your Google Sheets file\n", |
| 73 | + "SPREADSHEET_ID = '18r8cT6x9lPdM-rXvXjsqx84W7ZDdTDYGD59xr0UGOwg'\n", |
| 74 | + "\n", |
| 75 | + "# Call the function to update the chart width\n", |
| 76 | + "update_chart_size(SPREADSHEET_ID, is_dry_run=True)" |
| 77 | + ] |
| 78 | + } |
| 79 | + ], |
| 80 | + "metadata": { |
| 81 | + "colab": { |
| 82 | + "authorship_tag": "ABX9TyPVUT2KvU+kgVkDQrz8dLoZ", |
| 83 | + "include_colab_link": true, |
| 84 | + "private_outputs": true, |
| 85 | + "provenance": [] |
| 86 | + }, |
| 87 | + "kernelspec": { |
| 88 | + "display_name": "Python 3", |
| 89 | + "name": "python3" |
| 90 | + }, |
| 91 | + "language_info": { |
| 92 | + "name": "python" |
| 93 | + } |
| 94 | + }, |
| 95 | + "nbformat": 4, |
| 96 | + "nbformat_minor": 0 |
| 97 | +} |
0 commit comments