Example #1
0
    def test_df(self):
        df = self.spread.sheet_to_df(header_rows=2, start_row=2)

        assert isinstance(df.columns, pd.MultiIndex)
        assert df.shape == (3, 9)

        self.spread.df_to_sheet(
            df,
            start="A2",
            replace=True,
            sheet="Test df_to_sheet",
            freeze_index=True,
            freeze_headers=True,
            add_filter=True,
            merge_headers=True,
        )

        # ensre values are the same
        assert (self.spread.sheets[1].get_all_values() ==
                self.spread.sheets[2].get_all_values())

        sheets_metadata = self.spread._spread_metadata["sheets"]

        # ensure merged cells match
        assert util.remove_keys_from_list(
            sheets_metadata[1]["merges"],
            ["sheetId"]) == util.remove_keys_from_list(
                sheets_metadata[2]["merges"], ["sheetId"])

        # ensure basic filter matches
        assert util.remove_keys(sheets_metadata[1]["basicFilter"]["range"],
                                ["sheetId"]) == util.remove_keys(
                                    sheets_metadata[2]["basicFilter"]["range"],
                                    ["sheetId"])

        # ensure frozen cols/rows and dims match
        assert (sheets_metadata[1]["properties"]["gridProperties"] ==
                sheets_metadata[2]["properties"]["gridProperties"])

        self.spread.unmerge_cells()
        # sometimes it fetches the data too quickly and it hasn't
        # updated
        sleep(1)
        self.spread.refresh_spread_metadata()
        sheets_metadata = self.spread._spread_metadata["sheets"]

        # ensure merged cells don't match
        assert util.remove_keys_from_list(
            sheets_metadata[1]["merges"],
            ["sheetId"]) != util.remove_keys_from_list(
                sheets_metadata[2].get("merges", {}), ["sheetId"])

        self.spread.delete_sheet("Test df_to_sheet")
Example #2
0
def test_remove_keys_from_list():
    tests = [
        (
            [[{
                "foo": "bar",
                "bar": "foo"
            }, {
                "foo": "fighter"
            }], ["foo"]],
            [{
                "bar": "foo"
            }, {}],
        ),
        (
            [[{
                "foo": "bar",
                "bar": "foo"
            }, {
                "foo": "fighter"
            }], ["bar"]],
            [{
                "foo": "bar"
            }, {
                "foo": "fighter"
            }],
        ),
        ([[], ["foo", "bar"]], []),
        ([[]], []),
    ]

    for test in tests:
        assert util.remove_keys_from_list(*test[TEST]) == test[ANSWER]
Example #3
0
    def _list_spreadsheet_files(self, q):
        """Helper function to actually run a query, add paths if needed, and remove
        unwanted keys from results."""
        files = self._query_drive(q)

        if self._load_dirs:
            self._add_path_to_files(files)

        return remove_keys_from_list(files, ["parents"])
Example #4
0
    def _get_dirs(self, strip_parents=True):
        """Helper function to fetch directories if they haven't been yet. It will strip
        the parents by default for the `directories` property"""
        if not self._load_dirs:
            self.refresh_directories()

        if strip_parents:
            # this will make a copy, if we intend to modify the values
            # internally, pass strip_parents = False
            return remove_keys_from_list(self._dirs, ["parents"])
        else:
            return self._dirs
Example #5
0
    def test_df(self):
        df = self.spread.sheet_to_df(
            header_rows=2, start_row=2, formula_columns=["Total"]
        )

        df_to_sheet_name = "Test df_to_sheet"

        assert isinstance(df.columns, pd.MultiIndex)
        assert df.shape == (3, 9)
        assert df["Total"][0].startswith("=")

        self.spread.df_to_sheet(
            df,
            start="A2",
            replace=True,
            sheet=df_to_sheet_name,
            freeze_index=True,
            freeze_headers=True,
            add_filter=True,
            merge_headers=True,
        )

        # ensre values are the same
        assert (
            self.spread.sheets[1].get_all_values()
            == self.spread.sheets[2].get_all_values()
        )

        sheets_metadata = self.spread._spread_metadata["sheets"]

        # ensure merged cells match
        assert util.remove_keys_from_list(
            sheets_metadata[1]["merges"], ["sheetId"]
        ) == util.remove_keys_from_list(sheets_metadata[2]["merges"], ["sheetId"])

        # ensure basic filter matches
        assert util.remove_keys(
            sheets_metadata[1]["basicFilter"]["range"], ["sheetId"]
        ) == util.remove_keys(sheets_metadata[2]["basicFilter"]["range"], ["sheetId"])

        # ensure frozen cols/rows and dims match
        assert (
            sheets_metadata[1]["properties"]["gridProperties"]
            == sheets_metadata[2]["properties"]["gridProperties"]
        )

        self.spread.open_sheet(df_to_sheet_name)

        self.spread.unmerge_cells()
        # sometimes it fetches the data too quickly and it hasn't
        # updated
        sheets_metadata = self.spread._spread_metadata["sheets"]

        # ensure merged cells don't match
        assert util.remove_keys_from_list(
            sheets_metadata[1]["merges"], ["sheetId"]
        ) != util.remove_keys_from_list(
            sheets_metadata[2].get("merges", {}), ["sheetId"]
        )

        raw_sheet = "Raw"
        self.spread.df_to_sheet(
            df[["Total"]], index=False, sheet=raw_sheet, raw_columns=["Total"]
        )

        assert any(row[0].startswith("=") for row in self.spread.sheet.get_all_values())

        self.spread.delete_sheet(df_to_sheet_name)
        self.spread.delete_sheet(raw_sheet)