|
1 | 1 | # 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 | + |
| 30 | + "Serial Number Hex": "ABC123", |
| 31 | + "SKU": "SKU123", |
| 32 | + "Batch": "Batch01", |
| 33 | + }, |
| 34 | + { |
| 35 | + "Generation Date": "1700000001", |
| 36 | + |
| 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. |
0 commit comments