Append multiple rows in Google Sheets (#120829)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
307ae53066
commit
c89de2e6a6
3 changed files with 40 additions and 10 deletions
|
@ -41,7 +41,7 @@ SHEET_SERVICE_SCHEMA = vol.All(
|
||||||
{
|
{
|
||||||
vol.Required(DATA_CONFIG_ENTRY): ConfigEntrySelector(),
|
vol.Required(DATA_CONFIG_ENTRY): ConfigEntrySelector(),
|
||||||
vol.Optional(WORKSHEET): cv.string,
|
vol.Optional(WORKSHEET): cv.string,
|
||||||
vol.Required(DATA): dict,
|
vol.Required(DATA): vol.Any(cv.ensure_list, [dict]),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -108,15 +108,19 @@ async def async_setup_service(hass: HomeAssistant) -> None:
|
||||||
raise HomeAssistantError("Failed to write data") from ex
|
raise HomeAssistantError("Failed to write data") from ex
|
||||||
|
|
||||||
worksheet = sheet.worksheet(call.data.get(WORKSHEET, sheet.sheet1.title))
|
worksheet = sheet.worksheet(call.data.get(WORKSHEET, sheet.sheet1.title))
|
||||||
row_data = {"created": str(datetime.now())} | call.data[DATA]
|
|
||||||
columns: list[str] = next(iter(worksheet.get_values("A1:ZZ1")), [])
|
columns: list[str] = next(iter(worksheet.get_values("A1:ZZ1")), [])
|
||||||
row = [row_data.get(column, "") for column in columns]
|
now = str(datetime.now())
|
||||||
for key, value in row_data.items():
|
rows = []
|
||||||
if key not in columns:
|
for d in call.data[DATA]:
|
||||||
columns.append(key)
|
row_data = {"created": now} | d
|
||||||
worksheet.update_cell(1, len(columns), key)
|
row = [row_data.get(column, "") for column in columns]
|
||||||
row.append(value)
|
for key, value in row_data.items():
|
||||||
worksheet.append_row(row, value_input_option=ValueInputOption.user_entered)
|
if key not in columns:
|
||||||
|
columns.append(key)
|
||||||
|
worksheet.update_cell(1, len(columns), key)
|
||||||
|
row.append(value)
|
||||||
|
rows.append(row)
|
||||||
|
worksheet.append_rows(rows, value_input_option=ValueInputOption.user_entered)
|
||||||
|
|
||||||
async def append_to_sheet(call: ServiceCall) -> None:
|
async def append_to_sheet(call: ServiceCall) -> None:
|
||||||
"""Append new line of data to a Google Sheets document."""
|
"""Append new line of data to a Google Sheets document."""
|
||||||
|
|
|
@ -48,7 +48,7 @@
|
||||||
},
|
},
|
||||||
"data": {
|
"data": {
|
||||||
"name": "Data",
|
"name": "Data",
|
||||||
"description": "Data to be appended to the worksheet. This puts the values on a new row underneath the matching column (key). Any new key is placed on the top of a new column."
|
"description": "Data to be appended to the worksheet. This puts the values on new rows underneath the matching column (key). Any new key is placed on the top of a new column."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,6 +214,32 @@ async def test_append_sheet(
|
||||||
assert len(mock_client.mock_calls) == 8
|
assert len(mock_client.mock_calls) == 8
|
||||||
|
|
||||||
|
|
||||||
|
async def test_append_sheet_multiple_rows(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
setup_integration: ComponentSetup,
|
||||||
|
config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test service call appending to a sheet."""
|
||||||
|
await setup_integration()
|
||||||
|
|
||||||
|
entries = hass.config_entries.async_entries(DOMAIN)
|
||||||
|
assert len(entries) == 1
|
||||||
|
assert entries[0].state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
with patch("homeassistant.components.google_sheets.Client") as mock_client:
|
||||||
|
await hass.services.async_call(
|
||||||
|
DOMAIN,
|
||||||
|
"append_sheet",
|
||||||
|
{
|
||||||
|
"config_entry": config_entry.entry_id,
|
||||||
|
"worksheet": "Sheet1",
|
||||||
|
"data": [{"foo": "bar"}, {"foo": "bar2"}],
|
||||||
|
},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
assert len(mock_client.mock_calls) == 8
|
||||||
|
|
||||||
|
|
||||||
async def test_append_sheet_api_error(
|
async def test_append_sheet_api_error(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: ComponentSetup,
|
setup_integration: ComponentSetup,
|
||||||
|
|
Loading…
Add table
Reference in a new issue