Skip to content

Commit

Permalink
gsheet: append rows without header
Browse files Browse the repository at this point in the history
  • Loading branch information
Juarez Rudsatz committed Mar 11, 2022
1 parent 6a08ceb commit a11c7bb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
17 changes: 12 additions & 5 deletions petl/io/gsheet.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -72,15 +72,15 @@ 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.
The `cell_range` argument can be used to provide a range string
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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
"""
Expand All @@ -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


Expand Down
11 changes: 4 additions & 7 deletions petl/test/io/test_gsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

0 comments on commit a11c7bb

Please sign in to comment.