[PYTHON] Read the setting value from Google Spread Sheet gssetting

It is the 20th day of aratana Advent Calendar 2019. aratana

Recently, I am running a script that writes site information in the configuration file and goes to check it, but every time the number of sites to be checked increases, the work of rewriting the configuration file by myself is occurring, so it is improved I've been thinking that I want to.

Currently, the flow is as follows.

Manager does his best->Site open->The site has opened
 ->Fill in the site information on the sheet
 ->Contact me->I entered the server and reflected in the config file

My work takes a few minutes, but I've been wondering how troublesome it is. The solution is to use the site information written in the spreadsheet as a master and get it from the script! I thought, so I made a package to easily get the setting values from the spreadsheet. In Python.

gssetting It's easy, but it's an abbreviation for Google SpreadSheet Setting, and I named it gssetting. It is an image of creating a model of setting information and fetching information from the sheet based on it. It depends on gspread.

As of December 20, 2019, I made it in a hurry, so it is treated as alpha, and the degree of completion is low, but I will improve it in the future. (I have to have a test file

How to Use

Introduction

First, create a service account and set it on the target sheet. Please refer to the following site. .. .. Edit Google Sheets in Python

Installation

I'm registered on PyPi, so pip! To do. (Currently, Python 3.7 and above are targeted.

pip install gssetting

Modeling

I would like to extract only the name and value columns from the sheet below.

スクリーンショット 2019-12-19 16.48.28.png
from dataclasses import dataclass

from gssetting import GSSetting


@dataclass
class Setting(GSSetting):
    username: str
    value: str

    headers = ["name", "value"]

Run

Loading is completed by executing the following code. After that, it's okay if you process it in a nice way!

from dataclasses import dataclass

from gssetting import GoogleSpreadSheetSetting, GSSetting


@dataclass
class Setting(GSSetting):
    username: str
    value: str

    headers = ["name", "value"]


if __name__ == "__main__":
    gs_setting = GoogleSpreadSheetSetting(
        "./service_account.json", "document_id"
    )
    settings = gs_setting.load("sheet_name", "A1:C3", Setting)
    print(settings)

    for setting in settings:
        print(setting.username.value)

Execution result

[Setting(name=<Cell R2C1 'I'>, value=<Cell R2C3 'Hiragana'>), Setting(name=<Cell R3C1 'I'>, value=<Cell R3C3 'Chinese characters'>)]
I
I

Summary

Now that we have a package that allows you to easily get settings from the sheet, I'm thinking of reusing it in various places.

Recommended Posts

Read the setting value from Google Spread Sheet gssetting
Get the value from the [Django] Form
Access Google Calendar from the iOS app
Read the dissertation Deep Self-Learning From Noisy Labels