Module datasae.converter.gsheet

Google Spreadsheet library.

Classes

class GSheet (name: str, file_path: str, client_secret_file: str, gsheet_id: str = None)

Represents a data source that connects to an Google Spreadsheet.

Args

client_secret_file : str
path location credential google spreadsheet.
gsheet_id : str, optional
The ID of the Google Spreadsheet.
Expand source code
class GSheet(DataSource):
    """
    Represents a data source that connects to an Google Spreadsheet.

    Args:
        client_secret_file (str): path location credential google spreadsheet.
        gsheet_id (str, optional): The ID of the Google Spreadsheet.
    """

    client_secret_file: str
    gsheet_id: str = None

    @property
    def connection(self) -> Credentials:
        """
        Returns a credential for the Google Spreadsheet.

        Returns:
            Credentials: Creds from googleservice account.
        """
        return Credentials.from_service_account_file(
            self.client_secret_file,
            scopes=['https://www.googleapis.com/auth/spreadsheets']
        )

    def __call__(
        self, sheet_name: str, gsheet_id: str = None
    ) -> DataFrame:
        """
        __call__ method.

        Converts the data from google spreadsheet into a
        Pandas DataFrame.

        Args:
            sheet_name (str): The name a sheet will get data.
            gsheet_id (str, optional): The ID of the Google Spreadsheet.

        Returns:
            DataFrame: A Pandas DataFrame.
        """
        with warnings.catch_warnings(record=True):
            warnings.simplefilter('always')
            data: gspread.Worksheet = gspread.authorize(
                self.connection
            ).open_by_key(
                gsheet_id if gsheet_id
                else self.gsheet_id
            ).worksheet(sheet_name)

        # default index 0 jadi kolom
        return DataFrame(data.get_all_records())

Ancestors

Class variables

var client_secret_file : str
var gsheet_id : str

Instance variables

prop connection : Credentials

Returns a credential for the Google Spreadsheet.

Returns

Credentials
Creds from googleservice account.
Expand source code
@property
def connection(self) -> Credentials:
    """
    Returns a credential for the Google Spreadsheet.

    Returns:
        Credentials: Creds from googleservice account.
    """
    return Credentials.from_service_account_file(
        self.client_secret_file,
        scopes=['https://www.googleapis.com/auth/spreadsheets']
    )

Inherited members