Skip to content

Commit 82a9457

Browse files
authored
Merge pull request #1 from GreatScott/dev
First full implementation
2 parents a2561a9 + 03dc3f2 commit 82a9457

File tree

10 files changed

+560
-1
lines changed

10 files changed

+560
-1
lines changed

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# General Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# Virtual Environment
7+
.env/
8+
venv/
9+
10+
# Environment Variables
11+
.env
12+
13+
# IDEs and editors
14+
.idea/
15+
.vscode/
16+
*.swp
17+
*.swo
18+
19+
20+
# Build
21+
build/
22+
dist/
23+
*.egg-info/
24+
25+
# Unit test / coverage reports
26+
htmlcov/
27+
.tox/
28+
.nox/
29+
30+
# PyPI upload artifacts
31+
*.egg-info/
32+
*.eggs
33+
MANIFEST
34+
35+
36+
# Logs and Debugging
37+
*.log
38+
39+
# IDEs and Editors
40+
.idea/
41+
.vscode/
42+
43+
# Testing Cache
44+
.tox/
45+
htmlcov/
46+

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Exclude examples and tests from the PyPI package
2+
exclude examples/*
3+
exclude tests/*
4+

README.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,100 @@
11
# py-appsheet
2-
A no frills Python library for interacting with Google Appsheet
2+
A no-frills Python library for interacting with Google AppSheet
3+
4+
## Background and Setup
5+
* To work with this you need to create an AppSheet App first (i.e. not just an AppSheet Database).
6+
* To enable working with the API, you must go into the app's settings (gear icon) and then the integrations sub-item on the left. There you will find the App ID (make sure it's switched to enabled) and can create an Application Access key.
7+
* Be sure to not write these explicitly into your code. Instead it's better to store these in a .env file (make sure .gitignore lists the .env if working with a repo) and use `os.environ.get()` to pull in the secret values to work with them.
8+
* Some basic troubleshooting tips:
9+
* Make sure that you have your key column set correctly and that your schema is up-to-date (can be regenerated in the data view for the application)
10+
* Leverage Appsheet's Log Analyzer to get in-depth error messages. Can be access under your Appsheet App -> icon that looks like a pulse -> "Monitor" submenu -> "Audit History" -> "Launch log analyzer"
11+
12+
13+
14+
## Available Methods
15+
16+
### Find Items
17+
1. Search for a Specific Value in Any Column
18+
`result = client.find_item("Table Name", "ABC123")
19+
`
20+
2. Search for a Specific Vaue in a Specific Column
21+
`result = client.find_item("Table Name", "ABC123", target_column="column name")`
22+
23+
### Add Items (New Rows)
24+
25+
```
26+
rows_to_add = [
27+
{
28+
"Generation Date": "1700000000",
29+
"UserID": "[email protected]",
30+
"Serial Number Hex": "ABC123",
31+
"SKU": "SKU123",
32+
"Batch": "Batch01",
33+
},
34+
{
35+
"Generation Date": "1700000001",
36+
"UserID": "[email protected]",
37+
"Serial Number Hex": "DEF456",
38+
"SKU": "SKU456",
39+
"Batch": "Batch02",
40+
}
41+
]
42+
43+
44+
# Add rows to the AppSheet table
45+
response = client.add_item("Inventory Table", rows_to_add)
46+
47+
# Process the response
48+
print("Response from AppSheet API:", response)
49+
50+
51+
```
52+
53+
### Edit Item
54+
55+
Note: when updating an entry, the dictionary's first entry in the row data should be the designated key column (as defined in the AppSheet app settings for that table)
56+
57+
```
58+
# Example usage of edit_item
59+
serial_number = "ABC123"
60+
sku = "SKU456"
61+
62+
row_data = {
63+
"Serial Number Hex": serial_number, # Key column for the table
64+
"Bar Code": f"Inventory_Images/{serial_number}_serial_barcode.svg",
65+
"QR Code": f"Inventory_Images/{serial_number}_serial_qr.svg",
66+
"SKU Bar Code": f"Inventory_Images/{sku}_sku_barcode.svg"
67+
}
68+
69+
response = client.edit_item("Inventory Table", "Serial Number Hex", row_data)
70+
71+
if response.get("status") == "OK":
72+
print("Row updated successfully with image paths.")
73+
else:
74+
print(f"Failed to update row. API response: {response}")
75+
76+
```
77+
78+
### Delete Row by Key
79+
80+
```
81+
# Example: Delete a row by its key
82+
# "Serial Number Hex" is key col name
83+
84+
response = client.delete_row("Inventory Table", "Serial Number Hex", "ABC123")
85+
86+
```
87+
88+
89+
## Known Limitations and Important Notes
90+
*(Contributions Welcome!)*
91+
92+
* Querying for specific rows that contain an item of interest currently pulls all rows and filters locally.
93+
* Finding items currently pulls all rows and returns it in whatever, but the API does appear to have support for filtering and ordering. [See here](https://support.google.com/appsheet/answer/10105770?hl=en&ref_topic=10105767&sjid=1506075158107162628-NC)
94+
* Appsheet table names, which are used in URL-encoding, are assumed to not contain any special characters other than spaces. I.e. you can supply a table name like `"my table"` and the library will convert this to `"my%20table"` as needed under the hood, but does not handle other special characters that may mess with URL-encoding.
95+
96+
## Additional Credits
97+
Credit where credit is due. ChatGPT was leveraged extensively to put this together quickly.
98+
99+
## Contributing
100+
Contributions are welcome. Please submit pull requests to the dev branch.

examples/example.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
from dotenv import load_dotenv
3+
from py_appsheet.client import AppSheetClient
4+
5+
# Step 1: Load environment variables from .env file
6+
load_dotenv()
7+
8+
APPSHEET_APP_ID = os.getenv("APPSHEET_APP_ID")
9+
APPSHEET_API_KEY = os.getenv("APPSHEET_API_KEY")
10+
11+
if not APPSHEET_APP_ID or not APPSHEET_API_KEY:
12+
raise Exception("AppSheet App ID or API Key is missing. Check your .env file.")
13+
14+
# Step 2: Initialize the AppSheetClient
15+
client = AppSheetClient(app_id=APPSHEET_APP_ID, api_key=APPSHEET_API_KEY)
16+
17+
# Step 3: Test table name and key column
18+
TABLE_NAME = "Example Table Name Here"
19+
KEY_COLUMN = "Title Example"
20+
21+
# Step 4: Add a new row to the table
22+
print("Adding a new row...")
23+
new_row = {
24+
"Title Example": "Test Row 1",
25+
"Another Column": "Value 1"
26+
}
27+
28+
response = client.add_items(TABLE_NAME, [new_row])
29+
print("Add response:", response)
30+
31+
input("\nPress Enter to continue to the next step...")
32+
33+
# Step 5: Find the newly added row
34+
print("\nFinding the new row...")
35+
find_response = client.find_items(TABLE_NAME, "Test Row 1", KEY_COLUMN)
36+
print("Find response:", find_response)
37+
38+
input("\nPress Enter to continue to the next step...")
39+
40+
# Step 6: Edit the newly added row
41+
print("\nEditing the new row...")
42+
updated_row = {
43+
"Title Example": "Test Row 1", # Must include the key column and its value
44+
"Another Column": "Updated Value"
45+
}
46+
47+
edit_response = client.edit_item(TABLE_NAME, KEY_COLUMN, updated_row)
48+
print("Edit response:", edit_response)
49+
50+
input("\nPress Enter to continue to the next step...")
51+
52+
# Step 7: Delete the row
53+
print("\nDeleting the new row...")
54+
delete_response = client.delete_row(TABLE_NAME, KEY_COLUMN, "Test Row 1")
55+
print("Delete response:", delete_response)

py_appsheet/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .client import AppSheetClient
2+
3+
__all__ = ["AppSheetClient"]

0 commit comments

Comments
 (0)