Example #1
0
    def upsert_rows(
        self,
        rows: List[List[Cell]],
        key_columns: List[Union[str, Column]] = None,
    ) -> Dict:
        """
        Upsert multiple Table rows optionally updating existing rows.

        Works similar to Table.upsert_row() but uses 1 POST request for multiple rows.
        Input is a list of lists of Cells.

        :param rows: list of lists of `Cell` objects, one list for each row.
        :param key_columns: list of `Column` objects, column IDs, URLs, or names
            specifying columns to be used as upsert keys.
        """
        data = {
            "rows": [
                {
                    "cells": [
                        {"column": cell.column_id_or_name, "value": cell.value}
                        for cell in row
                    ]
                }
                for row in rows
            ]
        }

        if key_columns:
            if not isinstance(key_columns, list):
                raise err.ColumnNotFound(
                    f"key_columns parameter '{key_columns}' is not a list."
                )

            data["keyColumns"] = []

            for key_column in key_columns:
                if isinstance(key_column, Column):
                    data["keyColumns"].append(key_column.id)
                elif isinstance(key_column, str):
                    data["keyColumns"].append(key_column)
                else:
                    raise err.ColumnNotFound(
                        f"Invalid parameter: '{key_column}' in key_columns."
                    )

        return self.document.coda.upsert_row(self.document.id, self.id, data)
Example #2
0
    def get_column_by_id(self, column_id) -> Column:
        """
        Gets a Column by id.

        :param column_id: ID of the column. Example: "c-tuVwxYz"

        :return:
        """
        try:
            return next(filter(lambda x: x.id == column_id, self.columns()))
        except StopIteration:
            raise err.ColumnNotFound(f"No column with id {column_id}")
Example #3
0
    def get_column_by_name(self, column_name) -> Column:
        """
        Gets a Column by id.

        :param column_name: Name of the column. Discouraged in case using column_id is possible. Example: "Column 1"

        :return:
        """
        res = list(filter(lambda x: x.name == column_name, self.columns()))
        if not res:
            raise err.ColumnNotFound(f"No column with name: {column_name}")
        if len(res) > 1:
            raise err.AmbiguousName(
                f"More than 1 column found. Try using ID instead of Name")
        return res[0]