diff --git a/petl/io/gsheet.py b/petl/io/gsheet.py index b10f4a67..9c8293d6 100644 --- a/petl/io/gsheet.py +++ b/petl/io/gsheet.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function, division -from petl.util.base import Table +from petl.util.base import Table, iterdata from petl.compat import text_type from petl.errors import ArgumentError as PetlArgError @@ -72,8 +72,6 @@ def fromgsheet( The `spreadsheet` can either be the key of the spreadsheet or its name. - Set `open_by_key` to `True` in order to treat `spreadsheet` as spreadsheet key. - The `worksheet` argument can be omitted, in which case the first sheet in the workbook is used by default. @@ -81,6 +79,8 @@ def fromgsheet( specifying the top left and bottom right corners of a set of cells to extract. (i.e. 'A1:C7'). + Set `open_by_key` to `True` in order to treat `spreadsheet` as spreadsheet key. + .. note:: - Only the top level of google drive will be searched for the spreadsheet filename due to API limitations. @@ -198,7 +198,8 @@ def togsheet( def appendgsheet( - table, credentials_or_client, spreadsheet, worksheet=None, open_by_key=False + table, credentials_or_client, spreadsheet, worksheet=None, + open_by_key=False, include_header=False ): """ Append a table to an existing google shoot at either a new worksheet @@ -212,6 +213,11 @@ def appendgsheet( The `worksheet` is the title of the worksheet to append to or create when it does not exist yet. + Set `open_by_key` to `True` in order to treat `spreadsheet` as spreadsheet key. + + Set `include_header` to `True` if you don't want omit fieldnames as the + first row appended. + .. note:: The sheet index cannot be used, and None is not an option. """ @@ -220,7 +226,8 @@ def appendgsheet( wb = _open_spreadsheet(gspread_client, spreadsheet, open_by_key) # check to see if worksheet exists, if so append, otherwise create ws = _select_worksheet(wb, worksheet, True) - ws.append_rows(table) + rows = table if include_header else list(iterdata(table)) + ws.append_rows(rows) return wb.id diff --git a/petl/test/io/test_gsheet.py b/petl/test/io/test_gsheet.py index 69cffef1..cf14e9eb 100644 --- a/petl/test/io/test_gsheet.py +++ b/petl/test/io/test_gsheet.py @@ -230,7 +230,7 @@ def test_tofromgsheet_08_recreate(): def _get_testcase_for_append(): table_list = [TEST_TABLE[:], TEST_TABLE[:]] - expected = TEST_TABLE[:] + TEST_TABLE[:] + expected = TEST_TABLE[:] + TEST_TABLE[1:] return table_list, expected @@ -251,20 +251,17 @@ def test_appendgsheet_12_other_sheet(): filename, gspread_client, emails = _get_gspread_test_params() # test to append gsheet table = TEST_TABLE[:] + table2 = TEST_TABLE[1:] spread_id = togsheet(table, gspread_client, filename, share_emails=emails) try: appendgsheet(table, gspread_client, filename, worksheet="petl") + # get the results from the 2 sheets result1 = fromgsheet(gspread_client, filename, worksheet=None) ieq(result1, table) result2 = fromgsheet(gspread_client, filename, worksheet="petl") - ieq(result2, table) + ieq(result2, table2) finally: gspread_client.del_spreadsheet(spread_id) -@pytest.fixture() -def setup_emails(monkeypatch): - monkeypatch.setenv("PETL_GSHEET_EMAIL_J", "juarezr@gmail.com") - - # endregion